/** * KCD2 Dice Solver. * * Enter what dice and badges you own; get the best six to bring and the best * badge for each tier you hold. Owning the inventory — where it came from and * when it is written back — is `useInventoryState`'s job; this file is the view. */ import { useMemo, useState } from "react"; import type { JSX } from "react"; import { BadgePicker } from "./components/BadgePicker.tsx"; import { DiceList } from "./components/DiceList.tsx"; import { InventoryIo } from "./components/InventoryIo.tsx"; import { ResultPanel } from "./components/ResultPanel.tsx"; import { SET_SIZE } from "./core/searchGroups.ts"; import type { SolveRequest } from "./core/solve.ts"; import { useInventoryState } from "./hooks/useInventoryState.ts"; import { useSolver } from "./hooks/useSolver.ts"; import type { SolverPort } from "./hooks/useSolver.ts"; import { EMPTY, browserClipboard, browserSaveFile, browserUrlHash, encodeInventoryHash, } from "./lib/inventoryIo.ts"; import type { ClipboardPort, SaveFilePort, UrlHashPort } from "./lib/inventoryIo.ts"; export interface AppProps { /** Overridden in tests to avoid depending on jsdom worker support. */ readonly createPort?: () => SolverPort; /** Overridden in tests to isolate persistence. */ readonly storage?: Pick; /** Overridden in tests; jsdom has no navigator.clipboard. */ readonly clipboard?: ClipboardPort; /** Overridden in tests; jsdom has no URL.createObjectURL. */ readonly saveFile?: SaveFilePort; /** Overridden in tests to keep location/history changes from leaking. */ readonly urlHash?: UrlHashPort; } /** * The whole application. * * @param props - Optional injection points for tests. * @returns The app element. */ export function App({ createPort, storage, clipboard = browserClipboard, saveFile = browserSaveFile, urlHash = browserUrlHash, }: AppProps = {}): JSX.Element { const store = storage ?? window.localStorage; const [query, setQuery] = useState(""); const { saved, stored, dropped, linkApplied, persist, total, keep, replace, setDieCount, toggleBadge, } = useInventoryState(store, urlHash); const request = useMemo( () => total < SET_SIZE ? null : { diceCounts: saved.diceCounts, badgeIds: saved.badgeIds }, [saved, total], ); const { result, error, solving } = useSolver(request, createPort); const ownedBadges = useMemo(() => new Set(saved.badgeIds), [saved.badgeIds]); return (
{/* * The left column is the dice list and nothing else. Everything that is * not "which dice do I own" — the title, the search box, the badges, the * results — lives on the right, where there is horizontal room going * spare, rather than eating the vertical budget the 43-row grid needs. */}

KCD2 Dice Solver

Pick what you own; get the best six dice and a badge for each tier.

{ setQuery(event.target.value); }} /> {total} {total === 1 ? "die" : "dice"}
{!persist && (

{linkApplied ? "Loaded from a shared link — not saved on this device yet." : "Some saved entries could not be read — not saved over yet."}{" "} {" "} {/* Without this, turning down a link means pressing Clear — which writes an empty inventory over dice the visitor never even saw, with no undo. */} {linkApplied && ( )}

)} {dropped.length > 0 && (

{`Ignored: ${dropped.join(" ")}`}

)}
{/* Collapsed by default: badges matter far less than the dice, and open they cost more vertical space than everything else here. */}
Badges
); }