1import { appAtomRegistry } from "../rpc/atomRegistry";
2import { primaryServerLegacyThreadMigrationAtom } from "../state/server";
3import { toastManager } from "./ui/toast";
4
5type MigrationToastId = ReturnType<typeof toastManager.add>;
6
7/** A loading toast for as long as the primary server reports a legacy thread migration running. */
8export function startLegacyThreadMigrationToast(): () => void {
9 let toastId: MigrationToastId | null = null;
10
11 const unsubscribe = appAtomRegistry.subscribe(
12 primaryServerLegacyThreadMigrationAtom,
13 (migration) => {
14 if (migration?.status === "running") {
15 if (toastId !== null) return;
16 toastId = toastManager.add({
17 type: "loading",
18 title: "Restoring your threads…",
19 description: `Migrating ${migration.totalThreadCount.toLocaleString()} ${
20 migration.totalThreadCount === 1 ? "thread" : "threads"
21 } from the previous version. You can keep working while this finishes.`,
22 timeout: 0,
23 });
24 return;
25 }
26
27 if (toastId !== null) {
28 toastManager.close(toastId);
29 toastId = null;
30 }
31 },
32 { immediate: true },
33 );
34
35 return () => {
36 unsubscribe();
37 if (toastId !== null) toastManager.close(toastId);
38 };
39}