Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useThreadVisitedMigration.ts

40 lines1.9 KB

1import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
4import type { AtomRegistry } from "effect/reactivity";
5​
6import { environmentThreadShells, threadEnvironment } from "../state/threads";
7import { useUiStateStore } from "../uiStateStore";
8​
9/**
10 * Hands visit times recorded locally (before servers tracked visits) to each
11 * server once. Subscribed outside render: it only acts on threads it has not
12 * seen, so a shell update must not re-render anything.
13 */
14export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry): () => void {
15 const migratedThreadKeys = new Set<string>();
16 const migrate = (threads: ReadonlyArray<EnvironmentThreadShell>) => {
17 for (const thread of threads) {
18 // Missing means unsupported; retain the local value until the server upgrades.
19 if (thread.lastVisitedAt === undefined) continue;
20 const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
21 if (migratedThreadKeys.has(threadKey)) continue;
22 migratedThreadKeys.add(threadKey);
23 const local = useUiStateStore.getState().threadLastVisitedAtById[threadKey];
24 if (!local) continue;
25 const localMs = Date.parse(local);
26 if (!Number.isFinite(localMs)) continue;
27 const serverMs = thread.lastVisitedAt === null ? null : Date.parse(thread.lastVisitedAt);
28 if (serverMs !== null && Number.isFinite(serverMs) && serverMs >= localMs) continue;
29 void runAtomCommand(
30 registry,
31 threadEnvironment.visit,
32 { environmentId: thread.environmentId, input: { threadId: thread.id, visitedAt: local } },
33 { reportFailure: false },
34 );
35 }
36 };
37 return registry.subscribe(environmentThreadShells.threadShellsAtom, migrate, {
38 immediate: true,
39 });