import { useMemo } from "react";
import type { Bootstrap, Item } from "../types.js";
import { helpers, TODAY_STR, enrichItems } from "../types.js";
import { remainingShort } from "../stats.js";
import { fmt, TYPE_GLYPHS } from "../format.js";
import { Btn, Card, Icon } from "../components/primitives/index.js";
export function CustodyView({
data,
onSelectItem,
onCheckin,
onConsume,
onMarkGone,
}: {
data: Bootstrap;
onSelectItem: (i: Item) => void;
onCheckin: (i: Item) => void;
onConsume: (i: Item) => void;
onMarkGone: (i: Item) => void;
}) {
const items = useMemo(() => enrichItems(data), [data]);
const checkedOut = useMemo(
() =>
items
.filter((i) => i.status === "checked-out")
.sort(
(a, b) =>
+new Date(b.checkoutDate ?? b.purchaseDate) -
+new Date(a.checkoutDate ?? a.purchaseDate),
),
[items],
);
return (
My Custody
{checkedOut.length} item{checkedOut.length === 1 ? "" : "s"} checked out
{checkedOut.length === 0 ? (
Nothing checked out right now.
) : (
Item
Brand
Remaining
Checked out
{checkedOut.map((item) => (
onSelectItem(item)}
onCheckin={() => onCheckin(item)}
onConsume={() => onConsume(item)}
onMarkGone={() => onMarkGone(item)}
/>
))}
)}
);
}
function CustodyRow({
item,
data,
onSelect,
onCheckin,
onConsume,
onMarkGone,
}: {
item: Item;
data: Bootstrap;
onSelect: () => void;
onCheckin: () => void;
onConsume: () => void;
onMarkGone: () => void;
}) {
const glyph = TYPE_GLYPHS[item.type] ?? "ยท";
const pct = helpers.pctRemaining(item, TODAY_STR);
return (
{glyph}
{item.name}
{item.assetId}
{helpers.brandName(data, item.brandId)}
{remainingShort(item)}
{Math.round(pct * 100)}%
{fmt.daysAgo(item.checkoutDate)}
e.stopPropagation()}
>
Check in
Consume
);
}