@lemonriver/core 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -4
- package/dist/{build-Bqo69DWP.js → build-CS1goZxt.js} +1 -1
- package/dist/cli/bin.js +4 -4
- package/dist/{config-DJ5rkMMg.js → config-BpbcCbUv.js} +3835 -1532
- package/dist/{config-teo3kWz7.d.ts → config-CYNOR93W.d.ts} +1 -1
- package/dist/{dev-B2FJD5bJ.js → dev-BpYnfgLL.js} +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/locale/index.d.ts +1 -1
- package/dist/locale/index.js +1 -1
- package/dist/{preview-C93BOZL1.js → preview-HSzPPkSs.js} +1 -1
- package/dist/{types-DFK02XVv.d.ts → types-tdaXSyah.d.ts} +1 -0
- package/dist/{update-DsxZBtwv.js → update-CMRxtTc9.js} +18 -8
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DwTAZKa6.js → zh-tw-DcrDeKxr.js} +5 -0
- package/package.json +2 -2
- package/src/agent/prompt.ts +2 -0
- package/src/agent/types.ts +14 -2
- package/src/app/components/agent/agent-panel.tsx +19 -5
- package/src/app/components/agent/connect-view.tsx +1 -1
- package/src/app/components/agent/provider-logo.tsx +10 -1
- package/src/app/components/player.tsx +105 -23
- package/src/app/components/present/curtain.tsx +52 -0
- package/src/app/components/present/last-slide-hint.tsx +36 -0
- package/src/app/components/sidebar/sidebar-footer.tsx +6 -1
- package/src/app/components/thumbnail-rail.tsx +1 -0
- package/src/app/lemonriver/agent/lemon.tsx +16 -1
- package/src/app/lemonriver/agent/provider-marks.tsx +53 -0
- package/src/app/lemonriver/agent-prefs/prefs.test.ts +173 -0
- package/src/app/lemonriver/agent-prefs/prefs.ts +137 -0
- package/src/app/lemonriver/agent-prefs/use-prefs.ts +36 -0
- package/src/app/lemonriver/agent-providers/section.tsx +75 -0
- package/src/app/lemonriver/agent-providers/use-providers.ts +61 -0
- package/src/app/lemonriver/board/blocks/block-panel.tsx +666 -0
- package/src/app/lemonriver/board/blocks/cache.ts +28 -0
- package/src/app/lemonriver/board/blocks/change-list.tsx +189 -0
- package/src/app/lemonriver/board/blocks/classify.test.ts +204 -0
- package/src/app/lemonriver/board/blocks/classify.ts +125 -0
- package/src/app/lemonriver/board/blocks/snapshot.ts +86 -0
- package/src/app/lemonriver/board/board-canvas.tsx +627 -104
- package/src/app/lemonriver/board/board-page.tsx +1687 -91
- package/src/app/lemonriver/board/board.css +58 -7
- package/src/app/lemonriver/board/layout.test.ts +84 -0
- package/src/app/lemonriver/board/layout.ts +112 -11
- package/src/app/lemonriver/board/model/agent-tasks.test.ts +200 -0
- package/src/app/lemonriver/board/model/agent-tasks.ts +191 -0
- package/src/app/lemonriver/board/model/apply-prompt.test.ts +121 -0
- package/src/app/lemonriver/board/model/apply-prompt.ts +115 -0
- package/src/app/lemonriver/board/model/apply.ts +71 -0
- package/src/app/lemonriver/board/model/cards.test.ts +95 -2
- package/src/app/lemonriver/board/model/cards.ts +88 -20
- package/src/app/lemonriver/board/model/changes.test.ts +244 -0
- package/src/app/lemonriver/board/model/changes.ts +268 -0
- package/src/app/lemonriver/board/model/filter.test.ts +50 -0
- package/src/app/lemonriver/board/model/filter.ts +42 -0
- package/src/app/lemonriver/board/model/history.test.ts +71 -0
- package/src/app/lemonriver/board/model/history.ts +51 -0
- package/src/app/lemonriver/board/model/restructure.test.ts +205 -0
- package/src/app/lemonriver/board/model/restructure.ts +169 -0
- package/src/app/lemonriver/board/model/structure.test.ts +249 -0
- package/src/app/lemonriver/board/model/structure.ts +209 -0
- package/src/app/lemonriver/board/model/timing.test.ts +113 -0
- package/src/app/lemonriver/board/model/timing.ts +117 -0
- package/src/app/lemonriver/board/page-node.tsx +236 -55
- package/src/app/lemonriver/board/status-style.ts +25 -0
- package/src/app/lemonriver/board/use-board-apply.ts +206 -0
- package/src/app/lemonriver/board/use-board-cards.ts +108 -5
- package/src/app/lemonriver/board/use-board-changes.ts +108 -0
- package/src/app/lemonriver/board/use-board-columns.ts +37 -56
- package/src/app/lemonriver/board/use-board-comments.ts +36 -0
- package/src/app/lemonriver/board/use-board-restructures.ts +107 -0
- package/src/app/lemonriver/board/use-history.ts +86 -0
- package/src/app/lemonriver/board/use-page-order.ts +65 -38
- package/src/app/lemonriver/board/use-target-minutes.ts +47 -0
- package/src/app/lemonriver/board/views/block-readings.tsx +46 -0
- package/src/app/lemonriver/board/views/list-view.tsx +356 -0
- package/src/app/lemonriver/board/views/progress.tsx +32 -0
- package/src/app/lemonriver/board/views/timeline-view.tsx +341 -0
- package/src/app/lemonriver/brand/mascot.tsx +86 -0
- package/src/app/lemonriver/brand/mascots/design-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-working.png +0 -0
- package/src/app/lemonriver/chrome/app-chrome.tsx +2 -0
- package/src/app/lemonriver/features.test.ts +5 -0
- package/src/app/lemonriver/features.ts +2 -1
- package/src/app/lemonriver/home/brief-card.tsx +8 -1
- package/src/app/lemonriver/home/home-page.tsx +11 -7
- package/src/app/lemonriver/onboarding/onboarding.tsx +182 -0
- package/src/app/lemonriver/outline/build-team.tsx +278 -0
- package/src/app/lemonriver/outline/describe-page.tsx +380 -0
- package/src/app/lemonriver/outline/files.test.ts +90 -0
- package/src/app/lemonriver/outline/files.ts +69 -0
- package/src/app/lemonriver/outline/model.test.ts +672 -0
- package/src/app/lemonriver/outline/model.ts +455 -0
- package/src/app/lemonriver/outline/outline-page.tsx +537 -0
- package/src/app/lemonriver/outline/pending-pages.tsx +39 -0
- package/src/app/lemonriver/outline/rule.ts +3 -0
- package/src/app/lemonriver/outline/steps.tsx +43 -0
- package/src/app/lemonriver/outline/use-build-driver.ts +258 -0
- package/src/app/lemonriver/routes.test.tsx +6 -6
- package/src/app/lemonriver/routes.tsx +28 -0
- package/src/app/lemonriver/settings/settings-page.tsx +214 -0
- package/src/app/lemonriver/slide-extensions.tsx +45 -26
- package/src/app/lemonriver/text/en.ts +320 -0
- package/src/app/lemonriver/text/es.ts +311 -0
- package/src/app/lemonriver/text/ja.ts +313 -0
- package/src/app/lemonriver/text/text.test.ts +33 -0
- package/src/app/lemonriver/text/types.ts +339 -0
- package/src/app/lemonriver/text/zh-cn.ts +307 -0
- package/src/app/lemonriver/text/zh-tw.ts +307 -0
- package/src/app/lemonriver/theme/app.css +12 -1
- package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
- package/src/app/lemonriver/theme/pages.test.ts +4 -0
- package/src/app/lemonriver/theme/pages.ts +11 -2
- package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
- package/src/app/lemonriver/theme/use-theme.tsx +2 -2
- package/src/app/lemonriver/update/beta-badge.tsx +26 -0
- package/src/app/lemonriver/update/prompt.test.ts +84 -0
- package/src/app/lemonriver/update/prompt.ts +48 -0
- package/src/app/lemonriver/update/update-prompt.tsx +113 -0
- package/src/app/lib/agent/use-agent.ts +4 -3
- package/src/app/routes/home.tsx +13 -5
- package/src/app/routes/slide.tsx +71 -14
- package/src/locale/en.ts +1 -0
- package/src/locale/es.ts +1 -0
- package/src/locale/ja.ts +1 -0
- package/src/locale/types.ts +1 -0
- package/src/locale/zh-cn.ts +1 -0
- package/src/locale/zh-tw.ts +1 -0
- package/src/app/assets/open-slide.svg +0 -39
- package/src/app/favicon.ico +0 -0
- package/src/app/favicon.svg +0 -39
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { toast } from 'sonner';
|
|
3
|
+
import {
|
|
4
|
+
createSession,
|
|
5
|
+
sendMessage,
|
|
6
|
+
setActiveSession,
|
|
7
|
+
setDockOpen,
|
|
8
|
+
useAgentSessions,
|
|
9
|
+
useAgentStatus,
|
|
10
|
+
} from '@/lib/agent/use-agent';
|
|
11
|
+
import { format, useLocale } from '@/lib/use-locale';
|
|
12
|
+
import { useLrText } from '../text';
|
|
13
|
+
import type { BoardTask } from './model/agent-tasks';
|
|
14
|
+
import {
|
|
15
|
+
type BoardApplying,
|
|
16
|
+
type BoardLastApply,
|
|
17
|
+
type ChapterPlan,
|
|
18
|
+
sanitizeApplying,
|
|
19
|
+
sanitizeChapterPlan,
|
|
20
|
+
sanitizeLastApply,
|
|
21
|
+
} from './model/apply';
|
|
22
|
+
import { type BoardColumn, sanitizeStructure } from './model/structure';
|
|
23
|
+
|
|
24
|
+
const POLL_MS = 1500;
|
|
25
|
+
const STALL_MS = 10 * 60_000;
|
|
26
|
+
/** Long enough for the turn to start; a session that is idle after this never got it. */
|
|
27
|
+
const IDLE_STALL_MS = 60_000;
|
|
28
|
+
|
|
29
|
+
type BoardApplyState = {
|
|
30
|
+
applying: BoardApplying | null;
|
|
31
|
+
lastApply: BoardLastApply | null;
|
|
32
|
+
chapterPlan: ChapterPlan | null;
|
|
33
|
+
/** The chapters as the server has them now, read with every poll. */
|
|
34
|
+
columns: BoardColumn[] | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
async function readBoardState(slideId: string): Promise<BoardApplyState | null> {
|
|
38
|
+
try {
|
|
39
|
+
const res = await fetch(`/__board?slideId=${encodeURIComponent(slideId)}`);
|
|
40
|
+
if (!res.ok) return null;
|
|
41
|
+
const body = (await res.json()) as {
|
|
42
|
+
applying?: unknown;
|
|
43
|
+
lastApply?: unknown;
|
|
44
|
+
chapterPlan?: unknown;
|
|
45
|
+
columns?: unknown;
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
applying: sanitizeApplying(body.applying),
|
|
49
|
+
lastApply: sanitizeLastApply(body.lastApply),
|
|
50
|
+
chapterPlan: sanitizeChapterPlan(body.chapterPlan),
|
|
51
|
+
columns: sanitizeStructure({ columns: body.columns })?.columns ?? null,
|
|
52
|
+
};
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function post(path: string, slideId: string, body: Record<string, unknown> = {}) {
|
|
59
|
+
try {
|
|
60
|
+
return await fetch(path, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: { 'content-type': 'application/json' },
|
|
63
|
+
body: JSON.stringify({ slideId, ...body }),
|
|
64
|
+
});
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Hands one task to the agent in a single message and follows the board record
|
|
72
|
+
* until the turn ends: running, settled, or given up on.
|
|
73
|
+
*/
|
|
74
|
+
export function useBoardApply(
|
|
75
|
+
slideId: string,
|
|
76
|
+
onSettled: (last: BoardLastApply | null, was: BoardApplying | null) => void,
|
|
77
|
+
) {
|
|
78
|
+
const lr = useLrText();
|
|
79
|
+
const locale = useLocale();
|
|
80
|
+
const { sessions } = useAgentSessions();
|
|
81
|
+
const { connected, provider } = useAgentStatus();
|
|
82
|
+
const [state, setState] = useState<BoardApplyState>({
|
|
83
|
+
applying: null,
|
|
84
|
+
lastApply: null,
|
|
85
|
+
chapterPlan: null,
|
|
86
|
+
columns: null,
|
|
87
|
+
});
|
|
88
|
+
const [starting, setStarting] = useState(false);
|
|
89
|
+
const [now, setNow] = useState(() => Date.now());
|
|
90
|
+
const previous = useRef<BoardApplying | null>(null);
|
|
91
|
+
const settled = useRef(onSettled);
|
|
92
|
+
settled.current = onSettled;
|
|
93
|
+
|
|
94
|
+
const refresh = useCallback(async () => {
|
|
95
|
+
const next = await readBoardState(slideId);
|
|
96
|
+
if (!next) return;
|
|
97
|
+
const was = previous.current;
|
|
98
|
+
const wasRunning = was !== null;
|
|
99
|
+
previous.current = next.applying;
|
|
100
|
+
setState(next);
|
|
101
|
+
setNow(Date.now());
|
|
102
|
+
if (wasRunning && !next.applying) settled.current(next.lastApply, was);
|
|
103
|
+
}, [slideId]);
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
previous.current = null;
|
|
107
|
+
setState({ applying: null, lastApply: null, chapterPlan: null, columns: null });
|
|
108
|
+
void refresh();
|
|
109
|
+
}, [refresh]);
|
|
110
|
+
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (!state.applying) return;
|
|
113
|
+
const timer = window.setInterval(() => void refresh(), POLL_MS);
|
|
114
|
+
return () => window.clearInterval(timer);
|
|
115
|
+
}, [state.applying, refresh]);
|
|
116
|
+
|
|
117
|
+
const start = useCallback(
|
|
118
|
+
async (task: BoardTask) => {
|
|
119
|
+
if (starting) return;
|
|
120
|
+
setStarting(true);
|
|
121
|
+
try {
|
|
122
|
+
const res = await post('/__board/apply', slideId, { task, lang: locale.id });
|
|
123
|
+
if (!res?.ok) {
|
|
124
|
+
const body = res ? ((await res.json().catch(() => null)) as { error?: string }) : null;
|
|
125
|
+
throw new Error(body?.error ?? `HTTP ${res?.status ?? 0}`);
|
|
126
|
+
}
|
|
127
|
+
const { prompt } = (await res.json()) as { prompt: string };
|
|
128
|
+
|
|
129
|
+
const existing = sessions.find((session) => session.slideId === slideId);
|
|
130
|
+
let sessionId = existing?.id ?? null;
|
|
131
|
+
if (!sessionId) {
|
|
132
|
+
if (!provider) throw new Error('no agent');
|
|
133
|
+
sessionId = (await createSession(provider.id, slideId)).id;
|
|
134
|
+
}
|
|
135
|
+
setActiveSession(sessionId);
|
|
136
|
+
await sendMessage(sessionId, prompt, { slideId });
|
|
137
|
+
// The message id is the server's to make; the session is enough to keep
|
|
138
|
+
// another conversation on this deck from settling the task.
|
|
139
|
+
await fetch('/__board/apply', {
|
|
140
|
+
method: 'PATCH',
|
|
141
|
+
headers: { 'content-type': 'application/json' },
|
|
142
|
+
body: JSON.stringify({ slideId, sessionId }),
|
|
143
|
+
});
|
|
144
|
+
setDockOpen(true);
|
|
145
|
+
await refresh();
|
|
146
|
+
} catch (err) {
|
|
147
|
+
toast.error(
|
|
148
|
+
format(lr.board.applyFailed, { reason: String((err as Error).message ?? err) }),
|
|
149
|
+
);
|
|
150
|
+
await refresh();
|
|
151
|
+
} finally {
|
|
152
|
+
setStarting(false);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[lr.board.applyFailed, locale.id, provider, refresh, sessions, slideId, starting],
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const savePlan = useCallback(
|
|
159
|
+
async (plan: ChapterPlan | null) => {
|
|
160
|
+
try {
|
|
161
|
+
await fetch('/__board/plan', {
|
|
162
|
+
method: 'PUT',
|
|
163
|
+
headers: { 'content-type': 'application/json' },
|
|
164
|
+
body: JSON.stringify({ slideId, chapterPlan: plan }),
|
|
165
|
+
});
|
|
166
|
+
} catch {}
|
|
167
|
+
await refresh();
|
|
168
|
+
},
|
|
169
|
+
[refresh, slideId],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const cancel = useCallback(async () => {
|
|
173
|
+
await post('/__board/apply/cancel', slideId);
|
|
174
|
+
await refresh();
|
|
175
|
+
}, [refresh, slideId]);
|
|
176
|
+
|
|
177
|
+
const undoLast = useCallback(async () => {
|
|
178
|
+
const res = await post('/__board/apply/undo', slideId);
|
|
179
|
+
if (!res?.ok) return false;
|
|
180
|
+
await refresh();
|
|
181
|
+
return true;
|
|
182
|
+
}, [refresh, slideId]);
|
|
183
|
+
|
|
184
|
+
const running = sessions.some(
|
|
185
|
+
(session) => session.slideId === slideId && session.status === 'running',
|
|
186
|
+
);
|
|
187
|
+
const { applying, lastApply, chapterPlan, columns } = state;
|
|
188
|
+
const elapsed = applying ? now - applying.startedAt : 0;
|
|
189
|
+
const stalled =
|
|
190
|
+
applying !== null && (elapsed > STALL_MS || (!running && elapsed > IDLE_STALL_MS));
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
applying,
|
|
194
|
+
lastApply,
|
|
195
|
+
chapterPlan,
|
|
196
|
+
columns,
|
|
197
|
+
stalled,
|
|
198
|
+
starting,
|
|
199
|
+
connected,
|
|
200
|
+
start,
|
|
201
|
+
savePlan,
|
|
202
|
+
cancel,
|
|
203
|
+
undoLast,
|
|
204
|
+
refresh,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -10,15 +10,19 @@ import {
|
|
|
10
10
|
type CardPatch,
|
|
11
11
|
carryCards,
|
|
12
12
|
entriesAfter,
|
|
13
|
+
hasLink,
|
|
13
14
|
isSettledKey,
|
|
15
|
+
type LinkKind,
|
|
14
16
|
linkCards,
|
|
15
17
|
linkedPairs,
|
|
16
18
|
originAfter,
|
|
17
19
|
type PageOp,
|
|
18
20
|
pageKeys,
|
|
19
21
|
patchCard,
|
|
22
|
+
patchCards,
|
|
20
23
|
sanitizeCards,
|
|
21
24
|
sanitizeLinks,
|
|
25
|
+
setLinkKind,
|
|
22
26
|
unlinkCards,
|
|
23
27
|
} from './model/cards';
|
|
24
28
|
|
|
@@ -32,8 +36,22 @@ type BoardState = {
|
|
|
32
36
|
|
|
33
37
|
const INITIAL: BoardState = { cards: {}, links: [], entries: undefined, loaded: false };
|
|
34
38
|
|
|
35
|
-
const countAfter = (op: PageOp, count: number) =>
|
|
36
|
-
|
|
39
|
+
export const countAfter = (op: PageOp, count: number) => {
|
|
40
|
+
switch (op.kind) {
|
|
41
|
+
case 'delete':
|
|
42
|
+
return count - 1;
|
|
43
|
+
case 'deleteMany':
|
|
44
|
+
return count - op.indices.length;
|
|
45
|
+
case 'move':
|
|
46
|
+
case 'moveMany':
|
|
47
|
+
case 'reorder':
|
|
48
|
+
return count;
|
|
49
|
+
case 'restore':
|
|
50
|
+
return count + 1;
|
|
51
|
+
default:
|
|
52
|
+
return count + 1;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
37
55
|
|
|
38
56
|
/**
|
|
39
57
|
* Titles, statuses, work memos and links: board-only data that never reaches
|
|
@@ -132,15 +150,33 @@ export function useBoardCards(slideId: string, modulePages: Page[], count: numbe
|
|
|
132
150
|
if (modulePages.length > 0) void refreshEntries();
|
|
133
151
|
}, [modulePages, refreshEntries]);
|
|
134
152
|
|
|
153
|
+
/** Cards and links as the server has them; a task may have written titles. */
|
|
154
|
+
const refreshCards = useCallback(async () => {
|
|
155
|
+
try {
|
|
156
|
+
const res = await fetch(`/__board/cards?slideId=${encodeURIComponent(slideId)}`);
|
|
157
|
+
if (!res.ok) return;
|
|
158
|
+
const body = (await res.json()) as { cards?: unknown; links?: unknown };
|
|
159
|
+
const current = boardRef.current;
|
|
160
|
+
commit(
|
|
161
|
+
{
|
|
162
|
+
...current,
|
|
163
|
+
cards: sanitizeCards(body.cards),
|
|
164
|
+
links: sanitizeLinks(body.links),
|
|
165
|
+
},
|
|
166
|
+
false,
|
|
167
|
+
);
|
|
168
|
+
} catch {}
|
|
169
|
+
}, [slideId, commit]);
|
|
170
|
+
|
|
135
171
|
/**
|
|
136
172
|
* Moves the board's data along with a page operation, before the deck itself
|
|
137
173
|
* changes. Call the returned function once the deck write settles; a refused
|
|
138
174
|
* write puts the cards back as they were.
|
|
139
175
|
*/
|
|
140
176
|
const beginOp = useCallback(
|
|
141
|
-
(op: PageOp) => {
|
|
177
|
+
(op: PageOp, countOverride?: number) => {
|
|
142
178
|
const snapshot = boardRef.current;
|
|
143
|
-
const count = countRef.current;
|
|
179
|
+
const count = countOverride ?? countRef.current;
|
|
144
180
|
opsInFlight.current += 1;
|
|
145
181
|
entrySeq.current += 1;
|
|
146
182
|
|
|
@@ -149,6 +185,9 @@ export function useBoardCards(slideId: string, modulePages: Page[], count: numbe
|
|
|
149
185
|
if (before.every(isSettledKey)) {
|
|
150
186
|
const after = pageKeys(entries, countAfter(op, count));
|
|
151
187
|
const carried = carryCards(snapshot, before, after, originAfter(op, count));
|
|
188
|
+
// A restored page brings its card back in the same write: written later,
|
|
189
|
+
// it could be wiped by the deck reload the restore itself triggers.
|
|
190
|
+
if (op.kind === 'restore' && op.card) carried.cards[op.entry] = op.card;
|
|
152
191
|
commit({ ...snapshot, ...carried, entries }, true);
|
|
153
192
|
} else {
|
|
154
193
|
commit({ ...snapshot, entries }, false);
|
|
@@ -181,6 +220,39 @@ export function useBoardCards(slideId: string, modulePages: Page[], count: numbe
|
|
|
181
220
|
[keyAt, commit],
|
|
182
221
|
);
|
|
183
222
|
|
|
223
|
+
/** One change across several cards, written once. */
|
|
224
|
+
const patchMany = useCallback(
|
|
225
|
+
(indices: number[], change: CardPatch) => {
|
|
226
|
+
const keys = indices
|
|
227
|
+
.map((index) => keyAt(index))
|
|
228
|
+
.filter((key): key is string => key !== null);
|
|
229
|
+
if (keys.length === 0) return;
|
|
230
|
+
const current = boardRef.current;
|
|
231
|
+
commit({ ...current, cards: patchCards(current.cards, keys, change) }, true);
|
|
232
|
+
},
|
|
233
|
+
[keyAt, commit],
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
/** Puts links back, ignoring any that are already there. */
|
|
237
|
+
const addLinks = useCallback(
|
|
238
|
+
(pairs: BoardLink[]) => {
|
|
239
|
+
if (pairs.length === 0) return;
|
|
240
|
+
const current = boardRef.current;
|
|
241
|
+
if (!current.loaded) return;
|
|
242
|
+
const links = [...current.links];
|
|
243
|
+
for (const pair of pairs) if (!hasLink(links, pair.a, pair.b)) links.push(pair);
|
|
244
|
+
if (links.length !== current.links.length) commit({ ...current, links }, true);
|
|
245
|
+
},
|
|
246
|
+
[commit],
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
/** The deck source a page is written as, for putting the page back later. */
|
|
250
|
+
const entryAt = useCallback((index: number): string | null => {
|
|
251
|
+
const entries = boardRef.current.entries;
|
|
252
|
+
if (!entries || entries.length !== countRef.current) return null;
|
|
253
|
+
return entries[index] ?? null;
|
|
254
|
+
}, []);
|
|
255
|
+
|
|
184
256
|
const setLink = useCallback(
|
|
185
257
|
(a: number, b: number, linked: boolean) => {
|
|
186
258
|
const from = keyAt(a);
|
|
@@ -198,6 +270,19 @@ export function useBoardCards(slideId: string, modulePages: Page[], count: numbe
|
|
|
198
270
|
const link = useCallback((a: number, b: number) => setLink(a, b, true), [setLink]);
|
|
199
271
|
const unlink = useCallback((a: number, b: number) => setLink(a, b, false), [setLink]);
|
|
200
272
|
|
|
273
|
+
/** Names what links two pages, or clears it back to a plain association. */
|
|
274
|
+
const kindLink = useCallback(
|
|
275
|
+
(a: number, b: number, kind: LinkKind | null) => {
|
|
276
|
+
const from = keyAt(a);
|
|
277
|
+
const to = keyAt(b);
|
|
278
|
+
if (!from || !to) return;
|
|
279
|
+
const current = boardRef.current;
|
|
280
|
+
const links = setLinkKind(current.links, from, to, kind);
|
|
281
|
+
if (links !== current.links) commit({ ...current, links }, true);
|
|
282
|
+
},
|
|
283
|
+
[keyAt, commit],
|
|
284
|
+
);
|
|
285
|
+
|
|
201
286
|
const keys = useMemo(() => pageKeys(board.entries, count), [board.entries, count]);
|
|
202
287
|
const ready = board.loaded && keys.every(isSettledKey);
|
|
203
288
|
const cards = useMemo<(BoardCard | undefined)[]>(
|
|
@@ -206,5 +291,23 @@ export function useBoardCards(slideId: string, modulePages: Page[], count: numbe
|
|
|
206
291
|
);
|
|
207
292
|
const pairs = useMemo(() => linkedPairs(keys, board.links), [keys, board.links]);
|
|
208
293
|
|
|
209
|
-
return {
|
|
294
|
+
return {
|
|
295
|
+
ready,
|
|
296
|
+
keys,
|
|
297
|
+
entries: board.entries,
|
|
298
|
+
cards,
|
|
299
|
+
/** The same cards as a record, for code that looks a page up by its key. */
|
|
300
|
+
byKey: board.cards,
|
|
301
|
+
pairs,
|
|
302
|
+
links: board.links,
|
|
303
|
+
patch,
|
|
304
|
+
patchMany,
|
|
305
|
+
link,
|
|
306
|
+
unlink,
|
|
307
|
+
kindLink,
|
|
308
|
+
beginOp,
|
|
309
|
+
entryAt,
|
|
310
|
+
addLinks,
|
|
311
|
+
refreshCards,
|
|
312
|
+
};
|
|
210
313
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { toast } from 'sonner';
|
|
3
|
+
import { useLrText } from '../text';
|
|
4
|
+
import { type Placement, sanitizeChanges } from './model/changes';
|
|
5
|
+
|
|
6
|
+
const SAVE_DELAY = 150;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The list of blocks waiting to be moved. It belongs to the board, not the
|
|
10
|
+
* deck: nothing here is applied until the change list is handed to the agent.
|
|
11
|
+
*/
|
|
12
|
+
export function useBoardChanges(slideId: string) {
|
|
13
|
+
const lr = useLrText();
|
|
14
|
+
const [changes, setChanges] = useState<Placement[]>([]);
|
|
15
|
+
const [ready, setReady] = useState(false);
|
|
16
|
+
const changesRef = useRef(changes);
|
|
17
|
+
changesRef.current = changes;
|
|
18
|
+
const readyRef = useRef(false);
|
|
19
|
+
const timer = useRef<number | null>(null);
|
|
20
|
+
const queue = useRef<Promise<void>>(Promise.resolve());
|
|
21
|
+
|
|
22
|
+
const save = useCallback(
|
|
23
|
+
(next: Placement[]) => {
|
|
24
|
+
const body = JSON.stringify({ slideId, changes: next });
|
|
25
|
+
queue.current = queue.current.then(async () => {
|
|
26
|
+
try {
|
|
27
|
+
const res = await fetch('/__board/changes', {
|
|
28
|
+
method: 'PUT',
|
|
29
|
+
headers: { 'content-type': 'application/json' },
|
|
30
|
+
body,
|
|
31
|
+
});
|
|
32
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
toast.error(`${lr.board.saveFailed}: ${String((err as Error).message ?? err)}`);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
[slideId, lr.board],
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const flush = useCallback(() => {
|
|
42
|
+
if (timer.current === null) return;
|
|
43
|
+
window.clearTimeout(timer.current);
|
|
44
|
+
timer.current = null;
|
|
45
|
+
save(changesRef.current);
|
|
46
|
+
}, [save]);
|
|
47
|
+
|
|
48
|
+
const commit = useCallback(
|
|
49
|
+
(next: Placement[]) => {
|
|
50
|
+
changesRef.current = next;
|
|
51
|
+
setChanges(next);
|
|
52
|
+
if (!readyRef.current) return;
|
|
53
|
+
if (timer.current !== null) window.clearTimeout(timer.current);
|
|
54
|
+
timer.current = window.setTimeout(() => {
|
|
55
|
+
timer.current = null;
|
|
56
|
+
save(changesRef.current);
|
|
57
|
+
}, SAVE_DELAY);
|
|
58
|
+
},
|
|
59
|
+
[save],
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
/** Takes what the server already holds, without writing it back. */
|
|
63
|
+
const replace = useCallback((next: Placement[]) => {
|
|
64
|
+
changesRef.current = next;
|
|
65
|
+
setChanges(next);
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
let cancelled = false;
|
|
70
|
+
readyRef.current = false;
|
|
71
|
+
setReady(false);
|
|
72
|
+
setChanges([]);
|
|
73
|
+
changesRef.current = [];
|
|
74
|
+
(async () => {
|
|
75
|
+
try {
|
|
76
|
+
const res = await fetch(`/__board/changes?slideId=${encodeURIComponent(slideId)}`);
|
|
77
|
+
if (!res.ok || cancelled) return;
|
|
78
|
+
const body = (await res.json()) as { changes?: unknown };
|
|
79
|
+
if (cancelled) return;
|
|
80
|
+
const next = sanitizeChanges(body.changes);
|
|
81
|
+
changesRef.current = next;
|
|
82
|
+
setChanges(next);
|
|
83
|
+
readyRef.current = true;
|
|
84
|
+
setReady(true);
|
|
85
|
+
} catch {
|
|
86
|
+
// Left unloaded, so nothing written from here can overwrite changes
|
|
87
|
+
// that were never read.
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
return () => {
|
|
91
|
+
cancelled = true;
|
|
92
|
+
flush();
|
|
93
|
+
};
|
|
94
|
+
}, [slideId, flush]);
|
|
95
|
+
|
|
96
|
+
// A reload during the debounce window would otherwise drop the last move.
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
const onLeave = () => flush();
|
|
99
|
+
window.addEventListener('pagehide', onLeave);
|
|
100
|
+
window.addEventListener('beforeunload', onLeave);
|
|
101
|
+
return () => {
|
|
102
|
+
window.removeEventListener('pagehide', onLeave);
|
|
103
|
+
window.removeEventListener('beforeunload', onLeave);
|
|
104
|
+
};
|
|
105
|
+
}, [flush]);
|
|
106
|
+
|
|
107
|
+
return { changes, ready, setChanges: commit, replace };
|
|
108
|
+
}
|
|
@@ -3,17 +3,16 @@ import { toast } from 'sonner';
|
|
|
3
3
|
import type { Page } from '../../lib/sdk';
|
|
4
4
|
import { useLrText } from '../text';
|
|
5
5
|
import { seedColumns } from './layout';
|
|
6
|
-
import {
|
|
7
|
-
type BoardColumn,
|
|
8
|
-
columnOfPosition,
|
|
9
|
-
columnsAfterMove,
|
|
10
|
-
reconcileColumns,
|
|
11
|
-
} from './model/structure';
|
|
6
|
+
import { type BoardColumn, reconcileColumns } from './model/structure';
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
9
|
* The board's columns live beside the project, never in the deck file. The deck
|
|
15
10
|
* only ever seeds them: from then on a chapter page is an ordinary card, so
|
|
16
11
|
* moving it cannot re-group the pages it used to head.
|
|
12
|
+
*
|
|
13
|
+
* Every change to the structure is worked out as a whole new list by a pure
|
|
14
|
+
* function and handed here, so an undo can replay the list it took away instead
|
|
15
|
+
* of re-deriving it from a board that has already moved on.
|
|
17
16
|
*/
|
|
18
17
|
export function useBoardColumns(
|
|
19
18
|
slideId: string,
|
|
@@ -28,6 +27,9 @@ export function useBoardColumns(
|
|
|
28
27
|
});
|
|
29
28
|
seedRef.current = { pages, title: columnTitle };
|
|
30
29
|
|
|
30
|
+
/** Chapters a task just wrote, waiting for the deck to reload with them. */
|
|
31
|
+
const waiting = useRef<{ columns: BoardColumn[]; count: number } | null>(null);
|
|
32
|
+
|
|
31
33
|
const save = useCallback(
|
|
32
34
|
async (next: BoardColumn[]) => {
|
|
33
35
|
try {
|
|
@@ -79,6 +81,15 @@ export function useBoardColumns(
|
|
|
79
81
|
// A deck edited outside the board (or by the agent) changes the page count;
|
|
80
82
|
// the structure follows so the board never renders half a deck.
|
|
81
83
|
useEffect(() => {
|
|
84
|
+
const pending = waiting.current;
|
|
85
|
+
if (pending) {
|
|
86
|
+
// The chapters belong to a deck that has not reloaded yet; reconciling
|
|
87
|
+
// now would spread the new pages into the wrong chapter.
|
|
88
|
+
if (pages.length !== pending.count) return;
|
|
89
|
+
waiting.current = null;
|
|
90
|
+
setColumns(pending.columns);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
82
93
|
setColumns((current) => {
|
|
83
94
|
if (!current) return current;
|
|
84
95
|
const total = current.reduce((sum, column) => sum + column.size, 0);
|
|
@@ -89,61 +100,31 @@ export function useBoardColumns(
|
|
|
89
100
|
});
|
|
90
101
|
}, [pages.length, save]);
|
|
91
102
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
setColumns(
|
|
96
|
-
|
|
97
|
-
const next = columnsAfterMove(current, from, to);
|
|
98
|
-
if (next !== current) void save(next);
|
|
99
|
-
return next;
|
|
100
|
-
});
|
|
101
|
-
},
|
|
102
|
-
[save],
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
/** A page added or removed at `index` grows or shrinks the column holding it. */
|
|
106
|
-
const resizeColumnAt = useCallback(
|
|
107
|
-
(index: number, delta: number) => {
|
|
108
|
-
setColumns((current) => {
|
|
109
|
-
if (!current) return current;
|
|
110
|
-
const column = columnOfPosition(current, index);
|
|
111
|
-
const next = current.map((c, i) =>
|
|
112
|
-
i === column ? { ...c, size: Math.max(0, c.size + delta) } : c,
|
|
113
|
-
);
|
|
114
|
-
void save(next);
|
|
115
|
-
return next;
|
|
116
|
-
});
|
|
117
|
-
},
|
|
118
|
-
[save],
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
/** Grows one column by `delta`, for a page added straight into that column. */
|
|
122
|
-
const growColumn = useCallback(
|
|
123
|
-
(index: number, delta: number) => {
|
|
124
|
-
setColumns((current) => {
|
|
125
|
-
if (!current?.[index]) return current;
|
|
126
|
-
const next = current.map((column, i) =>
|
|
127
|
-
i === index ? { ...column, size: Math.max(0, column.size + delta) } : column,
|
|
128
|
-
);
|
|
129
|
-
void save(next);
|
|
130
|
-
return next;
|
|
131
|
-
});
|
|
103
|
+
const applyColumns = useCallback(
|
|
104
|
+
(next: BoardColumn[]) => {
|
|
105
|
+
waiting.current = null;
|
|
106
|
+
setColumns(next);
|
|
107
|
+
void save(next);
|
|
132
108
|
},
|
|
133
109
|
[save],
|
|
134
110
|
);
|
|
135
111
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
112
|
+
/** Chapters the server just wrote; held until the deck has that many pages. */
|
|
113
|
+
const adoptColumns = useCallback(
|
|
114
|
+
(next: BoardColumn[]) => {
|
|
115
|
+
const count = next.reduce((sum, column) => sum + column.size, 0);
|
|
116
|
+
// Fewer pages than the deck has: this is the record from before the task,
|
|
117
|
+
// and the deck's own reconcile already knows better.
|
|
118
|
+
if (count === 0 || count < pages.length) return;
|
|
119
|
+
if (pages.length === count) {
|
|
120
|
+
waiting.current = null;
|
|
121
|
+
setColumns(next);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
waiting.current = { columns: next, count };
|
|
144
125
|
},
|
|
145
|
-
[
|
|
126
|
+
[pages.length],
|
|
146
127
|
);
|
|
147
128
|
|
|
148
|
-
return { columns,
|
|
129
|
+
return { columns, applyColumns, adoptColumns };
|
|
149
130
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Which pages carry a comment. Comments are read-only here: the board only
|
|
5
|
+
* needs to know that a page has one, so a filter can pick those pages out.
|
|
6
|
+
*/
|
|
7
|
+
export function useBoardComments(slideId: string, count: number): boolean[] {
|
|
8
|
+
const [pages, setPages] = useState<boolean[]>([]);
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
let cancelled = false;
|
|
12
|
+
setPages([]);
|
|
13
|
+
if (!slideId) return;
|
|
14
|
+
(async () => {
|
|
15
|
+
try {
|
|
16
|
+
const res = await fetch(`/__comments?slideId=${encodeURIComponent(slideId)}`);
|
|
17
|
+
if (!res.ok) return;
|
|
18
|
+
const body = (await res.json()) as { comments?: { page?: unknown }[] };
|
|
19
|
+
if (cancelled) return;
|
|
20
|
+
const next = Array.from({ length: count }, () => false);
|
|
21
|
+
for (const comment of body.comments ?? []) {
|
|
22
|
+
const page = comment?.page;
|
|
23
|
+
if (typeof page === 'number' && page >= 0 && page < next.length) next[page] = true;
|
|
24
|
+
}
|
|
25
|
+
setPages(next);
|
|
26
|
+
} catch {
|
|
27
|
+
// No comments read: every card reads as having none.
|
|
28
|
+
}
|
|
29
|
+
})();
|
|
30
|
+
return () => {
|
|
31
|
+
cancelled = true;
|
|
32
|
+
};
|
|
33
|
+
}, [slideId, count]);
|
|
34
|
+
|
|
35
|
+
return pages;
|
|
36
|
+
}
|