import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useCallback, useEffect, useState, useSyncExternalStore } from "react"; import { counterStore, stepStore } from "./lib/store"; const tabs = [ { id: "local", label: "本地优先" }, { id: "cross", label: "跨窗口" }, { id: "store", label: "30 行 store" }, ]; const role = new URLSearchParams(window.location.search).get("role") ?? "A"; const sourceText = { cache: "内存缓存", disk: "磁盘", network: "网络", broadcast: "跨窗口广播", }; function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } export default function App() { const [tab, setTab] = useState("local"); const [items, setItems] = useState([]); const [source, setSource] = useState("cache"); const [loadedAt, setLoadedAt] = useState(0); const [windows, setWindows] = useState(1); const [busy, setBusy] = useState(false); const counter = useSyncExternalStore(counterStore.subscribe, counterStore.get); const step = useSyncExternalStore(stepStore.subscribe, stepStore.get); const apply = useCallback((result) => { setItems(result.items); setSource(result.source); setLoadedAt(result.loadedAt); void window.api.windows().then(setWindows); }, []); useEffect(() => { void window.api.load().then(apply); const off = window.api.onChanged(apply); return off; }, [apply]); useEffect(() => { window.__setTab = (next) => setTab(next); window.__snapshot = () => ({ tab, items, source, windows }); window.__smoke = async () => { await window.api.reset(); const first = await window.api.load(); const second = await window.api.load(); await window.api.clearCache(); const disk = await window.api.load(); const refreshed = await window.api.refresh(); await window.api.openWindow(); await sleep(350); const updated = await window.api.update("note-1", "跨窗口同步的标题"); await sleep(450); const count = await window.api.windows(); apply(updated); setWindows(count); setTab("local"); return { first: first.source, second: second.source, disk: disk.source, refreshed: refreshed.source, updated: updated.source, windows: count, }; }; }, [apply, items, source, tab, windows]); const run = async (action) => { if (busy) return; setBusy(true); try { apply(await action()); } finally { setBusy(false); } }; return (_jsxs("div", { className: "min-h-screen bg-surface px-8 py-7 text-text", children: [_jsxs("header", { className: "flex items-end justify-between gap-6 border-b border-border pb-4", children: [_jsxs("div", { children: [_jsx("h1", { className: "text-2xl font-semibold", "data-testid": "app-title", children: "\u6570\u636E\u6D41\u5B9E\u9A8C\u5BA4" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "\u7F13\u5B58 \u2192 \u78C1\u76D8 \u2192 \u7F51\u7EDC \u00B7 \u4E3B\u8FDB\u7A0B\u5E7F\u64AD \u00B7 \u4E0D\u5F15\u72B6\u6001\u5E93" })] }), _jsxs("div", { className: "flex items-center gap-2", children: [_jsxs("span", { className: "rounded-full bg-primary-soft px-3 py-1 text-xs text-primary", children: ["\u7A97\u53E3 ", role] }), _jsxs("span", { className: "rounded-full border border-border px-3 py-1 text-xs text-muted", "data-testid": "window-count", children: ["\u5171 ", windows, " \u4E2A\u7A97\u53E3"] })] })] }), _jsx("nav", { className: "mt-5 flex gap-1", "data-testid": "tabs", children: tabs.map((item) => (_jsx("button", { className: `rounded-md border px-3 py-1.5 text-sm ${tab === item.id ? "border-border bg-panel font-semibold text-primary" : "border-transparent text-muted hover:bg-panel"}`, "data-testid": `tab-${item.id}`, onClick: () => setTab(item.id), children: item.label }, item.id))) }), _jsxs("main", { className: "mt-6", children: [tab === "local" && (_jsxs("section", { children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsxs("span", { className: "rounded-full bg-primary-soft px-3 py-1 text-xs text-primary", "data-testid": "source-badge", children: ["\u6765\u6E90\uFF1A", sourceText[source]] }), _jsxs("span", { className: "text-xs text-muted", children: ["\u52A0\u8F7D\u4E8E ", new Date(loadedAt).toLocaleTimeString("zh-CN")] })] }), _jsxs("div", { className: "mt-3 flex flex-wrap gap-2", children: [_jsx("button", { className: "rounded-card bg-primary px-3 py-1.5 text-sm text-primary-foreground", onClick: () => void run(() => window.api.refresh()), disabled: busy, "data-testid": "refresh-button", children: "\u5237\u65B0\uFF08\u7F51\u7EDC\uFF09" }), _jsx("button", { className: "rounded-card border border-border px-3 py-1.5 text-sm", onClick: () => void window.api.clearCache(), "data-testid": "clear-cache-button", children: "\u6E05\u5185\u5B58\u7F13\u5B58" }), _jsx("button", { className: "rounded-card border border-border px-3 py-1.5 text-sm", onClick: () => void run(() => window.api.update("note-1", "手动更新的标题")), "data-testid": "update-button", children: "\u66F4\u65B0\u7B2C\u4E00\u6761" })] }), _jsxs("table", { className: "mt-4 w-full border-collapse text-sm", "data-testid": "item-table", children: [_jsx("thead", { children: _jsxs("tr", { className: "bg-panel text-left text-xs text-muted", children: [_jsx("th", { className: "border border-border px-3 py-2", children: "id" }), _jsx("th", { className: "border border-border px-3 py-2", children: "\u6807\u9898" }), _jsx("th", { className: "border border-border px-3 py-2", children: "\u66F4\u65B0\u65F6\u95F4" })] }) }), _jsx("tbody", { children: items.map((item) => (_jsxs("tr", { "data-testid": "item-row", children: [_jsx("td", { className: "border border-border px-3 py-2 font-mono text-xs", children: item.id }), _jsx("td", { className: "border border-border px-3 py-2", children: item.title }), _jsx("td", { className: "border border-border px-3 py-2 font-mono text-xs", children: new Date(item.updatedAt).toLocaleTimeString("zh-CN") })] }, item.id))) })] }), _jsx("p", { className: "mt-3 text-xs text-muted", children: "\u52A0\u8F7D\u987A\u5E8F\uFF1A\u5185\u5B58\u7F13\u5B58 \u2192 \u78C1\u76D8 JSON \u2192 \u7F51\u7EDC\uFF1B\u7F51\u7EDC\u6210\u529F\u540E\u5199\u56DE\u78C1\u76D8\u4E0E\u7F13\u5B58\u3002" })] })), tab === "cross" && (_jsx("section", { children: _jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "\u4E3B\u8FDB\u7A0B\u662F\u552F\u4E00\u5199\u8005" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "\u4E24\u4E2A\u7A97\u53E3\u5171\u4EAB\u540C\u4E00\u4EFD\u5185\u5B58\u7F13\u5B58\u4E0E\u78C1\u76D8\u6587\u4EF6\uFF1B\u66F4\u65B0\u5148\u5199\u4E3B\u8FDB\u7A0B\uFF0C\u518D\u5E7F\u64AD\u3002" }), _jsx("button", { className: "mt-4 rounded-card bg-primary px-3 py-1.5 text-sm text-primary-foreground", onClick: () => void window.api.openWindow(), "data-testid": "open-window-button", children: "\u6253\u5F00\u7B2C\u4E8C\u4E2A\u7A97\u53E3" }), _jsxs("p", { className: "mt-3 text-xs text-muted", children: ["\u5F53\u524D\u7A97\u53E3\u6570\uFF1A", _jsx("code", { "data-testid": "cross-window-count", children: windows })] })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "\u540C\u6B65\u89C4\u5219" }), _jsxs("ul", { className: "mt-2 list-disc pl-5 text-sm text-muted", children: [_jsx("li", { children: "\u7A97\u53E3\u4E0D\u76F4\u63A5\u5199\u78C1\u76D8\uFF0C\u53EA\u53D1\u66F4\u65B0\u8BF7\u6C42" }), _jsx("li", { children: "\u4E3B\u8FDB\u7A0B\u5199\u6210\u529F\u540E\u5E7F\u64AD `data:changed`" }), _jsx("li", { children: "\u5E7F\u64AD\u4E2D\u7684\u6765\u6E90\u6807\u8BB0\u4E3A\u300C\u8DE8\u7A97\u53E3\u5E7F\u64AD\u300D" }), _jsx("li", { children: "\u7A97\u53E3\u5173\u95ED\u540E\u4ECE\u5E7F\u64AD\u5217\u8868\u79FB\u9664" })] })] })] }) })), tab === "store" && (_jsx("section", { children: _jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "30 \u884C\u8BA2\u9605\u5F0F store" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "`createStore` \u53EA\u6709 get/set/subscribe\uFF0C\u914D\u5408 `useSyncExternalStore`\u3002" }), _jsxs("div", { className: "mt-4 flex items-center gap-3", children: [_jsx("button", { className: "h-9 rounded-card border border-border px-3", onClick: () => counterStore.set({ count: counter.count - step.step }), "data-testid": "store-minus", children: "\u2212" }), _jsx("span", { className: "min-w-16 text-center font-mono text-2xl", "data-testid": "store-count", children: counter.count }), _jsx("button", { className: "h-9 rounded-card bg-primary px-3 text-primary-foreground", onClick: () => counterStore.set({ count: counter.count + step.step }), "data-testid": "store-plus", children: "+" })] }), _jsxs("div", { className: "mt-4 flex items-center gap-2 text-sm", children: [_jsx("span", { className: "text-muted", children: "\u6B65\u957F" }), _jsx("button", { className: "h-8 rounded border border-border px-2", onClick: () => stepStore.set({ step: 1 }), children: "1" }), _jsx("button", { className: "h-8 rounded border border-border px-2", onClick: () => stepStore.set({ step: 5 }), children: "5" }), _jsxs("code", { className: "text-xs text-muted", children: ["step=", step.step] })] })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "\u4E09\u79CD\u505A\u6CD5\u600E\u4E48\u9009" }), _jsxs("table", { className: "mt-2 w-full text-xs", children: [_jsx("thead", { children: _jsxs("tr", { className: "text-left text-muted", children: [_jsx("th", { className: "py-1", children: "\u505A\u6CD5" }), _jsx("th", { className: "py-1", children: "\u9002\u5408" }), _jsx("th", { className: "py-1", children: "\u4EE3\u4EF7" })] }) }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("td", { className: "py-1", children: "Context + reducer" }), _jsx("td", { children: "\u5355\u7A97\u53E3\u3001\u5C42\u7EA7\u6DF1" }), _jsx("td", { children: "\u8BA2\u9605\u7C92\u5EA6\u7C97" })] }), _jsxs("tr", { children: [_jsx("td", { className: "py-1", children: "\u8BA2\u9605 store" }), _jsx("td", { children: "\u8DE8\u7EC4\u4EF6\u3001\u8DE8\u7A97\u53E3" }), _jsx("td", { children: "\u8981\u81EA\u5DF1\u7BA1\u751F\u547D\u5468\u671F" })] }), _jsxs("tr", { children: [_jsx("td", { className: "py-1", children: "\u6309\u7A97\u53E3\u5206\u7247" }), _jsx("td", { children: "\u591A\u7A97\u53E3\u72B6\u6001\u9694\u79BB" }), _jsx("td", { children: "\u540C\u6B65\u8981\u4E3B\u8FDB\u7A0B\u53C2\u4E0E" })] })] })] })] })] }) }))] })] })); } //# sourceMappingURL=App.js.map