# Character Screen

A full game character screen from Cozy UI parts: character sheet with equipment, a drag-and-drop backpack, item details with equip and sell, and a collection tab.

## Source

```tsx
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { CharacterInfo, type EquipmentSlot } from "@/components/ui/character-info";
import { Gallery } from "@/components/ui/gallery";
import type { GameItem } from "@/components/ui/game-core";
import { Inventory, type InventorySlots } from "@/components/ui/inventory";
import { ItemInfo } from "@/components/ui/item-info";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { BAG, CATEGORIES, COLLECTION, ITEMS, MAI, SORTS } from "./game-data";

/** Which equipment slot an item fits, by its type line. */
const slotFor = (item: GameItem) => (item.type === "Head" ? "head" : item.type === "Tool" ? "tool" : item.type === "Charm" ? "charm" : null);

/** A character screen: sheet, bag, item details and a collection tab.
 *  Double-click (or Enter) an item, or press Equip, to wear it; the old
 *  piece goes back into the bag. Selling adds coins. */
export default function CharacterScreen() {
  const [bag, setBag] = useState<InventorySlots>(() => [...BAG.slice(0, 15), ITEMS.can, null, null]);
  const [equipment, setEquipment] = useState<EquipmentSlot[]>(MAI.equipment ?? []);
  const [selected, setSelected] = useState<number | null>(6);
  const [coins, setCoins] = useState(1250);
  const item = selected != null ? bag[selected] : null;
  const target = item ? slotFor(item) : null;
  const worn = target ? equipment.find((s) => s.id === target)?.item ?? null : null;

  const equip = (index: number) => {
    const it = bag[index];
    const slotId = it ? slotFor(it) : null;
    if (!it || !slotId) { if (it) toast(`${it.name} can't be worn`); return; }
    const old = equipment.find((s) => s.id === slotId)?.item ?? null;
    setEquipment((eq) => eq.map((s) => (s.id === slotId ? { ...s, item: it } : s)));
    setBag((b) => b.map((x, i) => (i === index ? old : x)));
    toast.success(`Equipped ${it.name}`);
  };
  const sell = (index: number) => {
    const it = bag[index];
    if (!it) return;
    const total = (it.value ?? 0) * (it.quantity ?? 1);
    setCoins((c) => c + total);
    setBag((b) => b.map((x, i) => (i === index ? null : x)));
    setSelected(null);
    toast.success(`Sold ${it.name}`, { description: `+${total} coins` });
  };

  return (
    <div className="grid w-full gap-4">
      <div className="flex flex-wrap items-center gap-2">
        <span className="cz-badge cz-badge--outline gap-2 px-3 py-1 text-base">Coins <span className="tabular-nums">{coins}</span></span>
        <span className="text-sm text-text-muted">Double-click an item to equip it. Drag to rearrange.</span>
      </div>
      <Tabs defaultValue="bag">
        <TabsList aria-label="Character screen">
          <TabsTrigger value="bag">Character</TabsTrigger>
          <TabsTrigger value="book">Collection</TabsTrigger>
        </TabsList>
        <TabsContent value="bag">
          <div className="flex flex-wrap items-stretch gap-4">
            <CharacterInfo className="flex-[1_1_300px]" character={{ ...MAI, equipment }} selectedEquipment={target} />
            <div className="grid min-w-0 flex-[2_1_360px] content-start gap-4">
              <Inventory
                title="Backpack"
                items={bag}
                onItemsChange={(next) => setBag(next)}
                slots={24}
                lockedFrom={18}
                selected={selected}
                onSelect={(_, i) => setSelected(i)}
                onActivate={(_, i) => equip(i)}
                categories={CATEGORIES.slice(0, 4)}
                sorts={SORTS.slice(0, 1)}
                slotSize={60}
                formatValue={(v) => `${v} coins`}
              />
              {item && (
                <ItemInfo
                  item={item}
                  compareTo={worn}
                  variant="panel"
                  formatValue={(v) => `${v} coins`}
                  actions={[
                    ...(target ? [{ id: "equip", label: "Equip", variant: "reward" as const, onSelect: () => equip(selected!) }] : []),
                    { id: "sell", label: "Sell", variant: "secondary" as const, onSelect: () => sell(selected!) },
                  ]}
                />
              )}
            </div>
          </div>
        </TabsContent>
        <TabsContent value="book">
          <Gallery title="Collection" entries={COLLECTION} cardSize={116} detailActions={() => <Button size="sm" variant="secondary">Pin to board</Button>} />
        </TabsContent>
      </Tabs>
    </div>
  );
}
```

Docs: https://cozy-ui.solo.engineer/blocks/character-screen/
