get ammo and gear tables working
This commit is contained in:
1 parent
49dec31761
commit
9298ebcd3e
23 files changed
+928
-391
No files matched your search
@@ -0,0 +1,135 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import type { Route } from "./+types/home";
|
||||
|
||||
export function meta({}: Route.MetaArgs) {
|
||||
return [
|
||||
{ title: "Collateral - Gear" },
|
||||
{ name: "description", content: "Gear" },
|
||||
];
|
||||
}
|
||||
|
||||
const baseUrl =
|
||||
typeof window !== "undefined"
|
||||
? window.location.origin
|
||||
: process.env.VITE_VERCEL_URL || "http://localhost:5173"; // Fallback for your server env
|
||||
|
||||
const g = await fetch(`${baseUrl}/characters/collateral/gear.json`).then((r) =>
|
||||
r.json(),
|
||||
);
|
||||
|
||||
type AmmunitionData = {
|
||||
caliber: string;
|
||||
type: string;
|
||||
count: number;
|
||||
};
|
||||
|
||||
type GearData = {
|
||||
name: string;
|
||||
description: string;
|
||||
count: number;
|
||||
contents: GearData[];
|
||||
};
|
||||
|
||||
function AmmunitionRow({ ammo }: { ammo: AmunitionData }) {
|
||||
return (
|
||||
<tr>
|
||||
<td>{ammo.caliber}</td>
|
||||
<td>{ammo.type}</td>
|
||||
<td>{ammo.count}x</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
function AmmunitionHeader() {
|
||||
return (
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Caliber</th>
|
||||
<th>Type</th>
|
||||
<th>Count</th>
|
||||
</tr>
|
||||
</thead>
|
||||
);
|
||||
}
|
||||
|
||||
function Ammunition({ ammo }: { ammo: AmmunitionData[] }) {
|
||||
const ammoRows: ReactElement[] = ammo.map((entry, index) => (
|
||||
<AmmunitionRow key={index} ammo={entry} />
|
||||
));
|
||||
|
||||
return (
|
||||
<table className="cyber-table">
|
||||
<AmmunitionHeader />
|
||||
<tbody>{ammoRows}</tbody>
|
||||
</table>
|
||||
);
|
||||
}
|
||||
|
||||
function InventoryHeader(): ReactElement {
|
||||
return (
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Description</th>
|
||||
<th>Count</th>
|
||||
</tr>
|
||||
</thead>
|
||||
);
|
||||
}
|
||||
|
||||
function InventoryWithDepth(name: string, depth: number): string {
|
||||
if (depth === 0) {
|
||||
return name;
|
||||
}
|
||||
|
||||
return "|" + "-".repeat(depth - 1) + "> " + name;
|
||||
}
|
||||
|
||||
function InventoryRow({
|
||||
gear,
|
||||
depth,
|
||||
}: {
|
||||
gear: GearData;
|
||||
depth: number;
|
||||
}): ReactElement {
|
||||
const contents = gear.contents
|
||||
? gear.contents.map((entry, index) => (
|
||||
<InventoryRow gear={entry} key={index} depth={depth + 1} />
|
||||
))
|
||||
: [];
|
||||
|
||||
return (
|
||||
<>
|
||||
<tr>
|
||||
<td>{InventoryWithDepth(gear.name, depth)}</td>
|
||||
<td>{InventoryWithDepth(gear.description, depth)}</td>
|
||||
<td>{gear.count}x</td>
|
||||
</tr>
|
||||
{contents}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function Inventory({ gear }: { gear: GearData[] }) {
|
||||
const inventoryRows = gear.map((entry, index) => (
|
||||
<InventoryRow key={index} gear={entry} depth={0} />
|
||||
));
|
||||
|
||||
return (
|
||||
<table className="cyber-table">
|
||||
<InventoryHeader />
|
||||
<tbody>{inventoryRows}</tbody>
|
||||
</table>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Gear() {
|
||||
const [gear] = useState(g);
|
||||
|
||||
return (
|
||||
<main>
|
||||
<Ammunition ammo={gear.ammo} />
|
||||
<Inventory gear={gear.gear} />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user