import { Database, Trash2 } from "lucide-react"; import { useCallback, useEffect, useOptimistic, useState } from "react"; import type { CachedSourceInfo } from "../adapters/bestiary-cache.js"; import * as bestiaryCache from "../adapters/bestiary-cache.js"; import { Button } from "./ui/button.js"; interface SourceManagerProps { onCacheCleared: () => void; } export function SourceManager({ onCacheCleared }: SourceManagerProps) { const [sources, setSources] = useState([]); const [optimisticSources, applyOptimistic] = useOptimistic( sources, ( state, action: { type: "remove"; sourceCode: string } | { type: "clear" }, ) => action.type === "clear" ? [] : state.filter((s) => s.sourceCode !== action.sourceCode), ); const loadSources = useCallback(async () => { const cached = await bestiaryCache.getCachedSources(); setSources(cached); }, []); useEffect(() => { loadSources(); }, [loadSources]); const handleClearSource = async (sourceCode: string) => { applyOptimistic({ type: "remove", sourceCode }); await bestiaryCache.clearSource(sourceCode); await loadSources(); onCacheCleared(); }; const handleClearAll = async () => { applyOptimistic({ type: "clear" }); await bestiaryCache.clearAll(); await loadSources(); onCacheCleared(); }; if (optimisticSources.length === 0) { return (

No cached sources

); } return (
Cached Sources
); }