Files
cpr-character-sheet/app/routes/gear.tsx
T

136 lines
2.6 KiB
TypeScript

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>
);
}