| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { useEffect, useRef } from "react"; |
| 3 | |
| 4 | import { primaryServerLegacyThreadMigrationAtom } from "../state/server"; |
| 5 | import { toastManager } from "./ui/toast"; |
| 6 | |
| 7 | type MigrationToastId = ReturnType<typeof toastManager.add>; |
| 8 | |
| 9 | export function LegacyThreadMigrationToast() { |
| 10 | const migration = useAtomValue(primaryServerLegacyThreadMigrationAtom); |
| 11 | const toastIdRef = useRef<MigrationToastId | null>(null); |
| 12 | |
| 13 | useEffect(() => { |
| 14 | if (migration?.status === "running") { |
| 15 | if (toastIdRef.current !== null) { |
| 16 | return; |
| 17 | } |
| 18 | toastIdRef.current = toastManager.add({ |
| 19 | type: "loading", |
| 20 | title: "Restoring your threads…", |
| 21 | description: `Migrating ${migration.totalThreadCount.toLocaleString()} ${ |
| 22 | migration.totalThreadCount === 1 ? "thread" : "threads" |
| 23 | } from the previous version. You can keep working while this finishes.`, |
| 24 | timeout: 0, |
| 25 | }); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | if (toastIdRef.current !== null) { |
| 30 | toastManager.close(toastIdRef.current); |
| 31 | toastIdRef.current = null; |
| 32 | } |
| 33 | }, [migration]); |
| 34 | |
| 35 | useEffect( |
| 36 | () => () => { |
| 37 | if (toastIdRef.current !== null) { |
| 38 | toastManager.close(toastIdRef.current); |
| 39 | } |
| 40 | }, |
| 41 | [], |
| 42 | ); |
| 43 | |
| 44 | return null; |
| 45 | } |