# Village Shop

An in-game store from Cozy UI parts: a wallet with animated amounts, shelves with tabs, deals, stock and gem packs, and a purchase dialog with quantity, balances and "get more" when the player is short.

## Source

```tsx
import { useState } from "react";
import { toast } from "sonner";
import { CurrencyBar } from "@/components/ui/currency-bar";
import { isCurrencyPrice, spend, type Price, type Wallet } from "@/components/ui/economy-core";
import { PurchaseDialog } from "@/components/ui/purchase-dialog";
import { Shop, type ShopItem } from "@/components/ui/shop";
import { GEM_PACKS, STORE_CATEGORIES, storeItems, type StoreData } from "./shop-data";

type Item = ShopItem<StoreData>;
const times = (price: Price | Price[], q: number): Price[] =>
  (Array.isArray(price) ? price : [price]).map((p) => (isCurrencyPrice(p) ? { ...p, amount: p.amount * q } : p));

/** A village store from Cozy UI parts: the wallet (CurrencyBar), the shelves (Shop) and the
 *  confirm step (PurchaseDialog). Too expensive? "Get more" jumps to the gem packs and shakes
 *  the wallet. Gem packs are real-money prices: here they resolve after a short wait, in a game
 *  you call your store SDK from onConfirm. */
export default function VillageShop() {
  const [wallet, setWallet] = useState<Wallet>({ coin: 1250, gem: 40 });
  const [items, setItems] = useState<Item[]>(() => [...storeItems(Date.now()), ...(GEM_PACKS as Item[]).map((p) => ({ ...p, category: "gems", featured: false }))]);
  const [category, setCategory] = useState("all");
  const [picked, setPicked] = useState<Item | null>(null);
  const [open, setOpen] = useState(false);
  const [highlight, setHighlight] = useState<string | null>(null);
  const [refreshAt] = useState(() => Date.now() + 3 * 3_600_000 + 12 * 60_000);

  const choose = (item: Item) => { setPicked(item); setOpen(true); };
  const stackable = picked && !picked.data?.once && picked.kind !== "pack";
  const maxQuantity = stackable ? Math.min(picked!.stock ?? 20, 20) : 1;

  const confirm = (item: Item, quantity: number) =>
    new Promise<void>((resolve, reject) => {
      setTimeout(() => {
        if (item.kind === "pack") {
          setWallet((w) => ({ ...w, gem: (w.gem ?? 0) + (item.amount ?? 0) }));
          return resolve();
        }
        const next = spend(wallet, times(item.price, quantity));
        if (!next) return reject(new Error("Your wallet changed. Please try again."));
        setWallet(next);
        setItems((all) => all.map((i) => (i.id !== item.id ? i : { ...i, owned: i.data?.once ? true : i.owned, stock: i.stock != null ? Math.max(0, i.stock - quantity) : i.stock })));
        resolve();
      }, 700);
    });

  return (
    <div className="grid w-full gap-4">
      <Shop
        title="Village general store"
        items={items}
        wallet={wallet}
        categories={STORE_CATEGORIES}
        category={category}
        onCategoryChange={setCategory}
        refreshAt={refreshAt}
        onSelect={choose}
        onBuy={choose}
        headerEnd={
          <CurrencyBar
            wallet={wallet}
            size="sm"
            onAdd={(id) => { setCategory("gems"); if (id === "coin") toast("Sell crops at the market to earn coins, or trade gems here."); }}
            highlight={highlight}
            onHighlightEnd={() => setHighlight(null)}
          />
        }
      />
      {picked && (
        <PurchaseDialog
          open={open}
          onOpenChange={setOpen}
          item={{ id: picked.id, name: picked.name, label: picked.label, icon: picked.icon, description: picked.description, price: picked.price, rarity: picked.rarity, quantity: picked.amount, data: picked.data }}
          wallet={wallet}
          maxQuantity={maxQuantity}
          onConfirm={(_, quantity) => confirm(picked, quantity)}
          onGetMore={(id) => { setOpen(false); setCategory("gems"); setHighlight(id); }}
          realMoneyNote="Charged by your app store. This demo only pretends."
          onDone={() => toast.success("Added to your storage shed")}
        />
      )}
    </div>
  );
}
```

Docs: https://cozy-ui.solo.engineer/blocks/village-shop/
