|
| 1 | +// One-way Tauri → Electron migration snapshot plumbing. |
| 2 | +// |
| 3 | +// The Tauri shell exports localStorage keys the user actively depends on |
| 4 | +// (workspace list, selected workspace, per-workspace last-session, server |
| 5 | +// list) into a JSON file in app_data_dir just before launching the |
| 6 | +// Electron installer. Electron reads that file on first launch, hydrates |
| 7 | +// localStorage for the keys that are still empty, then marks the file as |
| 8 | +// acknowledged. |
| 9 | +// |
| 10 | +// Scope decision: we migrate *workspace* keys only. Everything else |
| 11 | +// (theme, font zoom, sidebar widths, feature flags) is cheap to redo and |
| 12 | +// not worth the complexity of a cross-origin localStorage transfer. |
| 13 | + |
| 14 | +import { invoke } from "@tauri-apps/api/core"; |
| 15 | + |
| 16 | +export const MIGRATION_SNAPSHOT_VERSION = 1; |
| 17 | + |
| 18 | +// Keep this list tiny and strict. Adding keys here expands blast radius |
| 19 | +// if a later release renames them. |
| 20 | +export const MIGRATION_KEY_PATTERNS: Array<RegExp> = [ |
| 21 | + /^openwork\.react\.activeWorkspace$/, |
| 22 | + /^openwork\.react\.sessionByWorkspace$/, |
| 23 | + /^openwork\.server\.list$/, |
| 24 | + /^openwork\.server\.active$/, |
| 25 | + /^openwork\.server\.urlOverride$/, |
| 26 | + /^openwork\.server\.token$/, |
| 27 | +]; |
| 28 | + |
| 29 | +export type MigrationSnapshot = { |
| 30 | + version: typeof MIGRATION_SNAPSHOT_VERSION; |
| 31 | + writtenAt: number; |
| 32 | + source: "tauri"; |
| 33 | + keys: Record<string, string>; |
| 34 | +}; |
| 35 | + |
| 36 | +function matchesMigrationKey(key: string) { |
| 37 | + return MIGRATION_KEY_PATTERNS.some((pattern) => pattern.test(key)); |
| 38 | +} |
| 39 | + |
| 40 | +function collectMigrationKeysFromLocalStorage(): Record<string, string> { |
| 41 | + const out: Record<string, string> = {}; |
| 42 | + if (typeof window === "undefined") return out; |
| 43 | + for (let i = 0; i < window.localStorage.length; i++) { |
| 44 | + const key = window.localStorage.key(i); |
| 45 | + if (!key || !matchesMigrationKey(key)) continue; |
| 46 | + const value = window.localStorage.getItem(key); |
| 47 | + if (value != null) out[key] = value; |
| 48 | + } |
| 49 | + return out; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Tauri-only. Called by the last Tauri release right before it kicks off |
| 54 | + * the Electron installer. Snapshots the workspace-related localStorage |
| 55 | + * keys to <app_data_dir>/migration-snapshot.v1.json via a Rust command |
| 56 | + * that does the actual disk write (renderer can't write outside the |
| 57 | + * sandbox on its own). |
| 58 | + */ |
| 59 | +export async function writeMigrationSnapshotFromTauri(): Promise<{ |
| 60 | + ok: boolean; |
| 61 | + keyCount: number; |
| 62 | + reason?: string; |
| 63 | +}> { |
| 64 | + try { |
| 65 | + const keys = collectMigrationKeysFromLocalStorage(); |
| 66 | + const snapshot: MigrationSnapshot = { |
| 67 | + version: MIGRATION_SNAPSHOT_VERSION, |
| 68 | + writtenAt: Date.now(), |
| 69 | + source: "tauri", |
| 70 | + keys, |
| 71 | + }; |
| 72 | + await invoke("write_migration_snapshot", { snapshot }); |
| 73 | + return { ok: true, keyCount: Object.keys(keys).length }; |
| 74 | + } catch (error) { |
| 75 | + return { |
| 76 | + ok: false, |
| 77 | + keyCount: 0, |
| 78 | + reason: error instanceof Error ? error.message : String(error), |
| 79 | + }; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type ElectronMigrationBridge = { |
| 84 | + readSnapshot: () => Promise<MigrationSnapshot | null>; |
| 85 | + ackSnapshot: () => Promise<{ ok: boolean; moved: boolean }>; |
| 86 | +}; |
| 87 | + |
| 88 | +function electronMigrationBridge(): ElectronMigrationBridge | null { |
| 89 | + if (typeof window === "undefined") return null; |
| 90 | + const bridge = (window as unknown as { |
| 91 | + __OPENWORK_ELECTRON__?: { migration?: ElectronMigrationBridge }; |
| 92 | + }).__OPENWORK_ELECTRON__; |
| 93 | + return bridge?.migration ?? null; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Electron-only. Called once during app boot. Reads the migration |
| 98 | + * snapshot (if any), hydrates localStorage for keys that aren't already |
| 99 | + * set on the Electron install, and acks the file so we don't re-ingest |
| 100 | + * on subsequent launches. |
| 101 | + * |
| 102 | + * Returns the number of keys hydrated. Returns 0 when there is no |
| 103 | + * snapshot, which is the steady-state case after the first launch. |
| 104 | + */ |
| 105 | +export async function ingestMigrationSnapshotOnElectronBoot(): Promise<number> { |
| 106 | + const bridge = electronMigrationBridge(); |
| 107 | + if (!bridge) return 0; |
| 108 | + |
| 109 | + let snapshot: MigrationSnapshot | null = null; |
| 110 | + try { |
| 111 | + snapshot = await bridge.readSnapshot(); |
| 112 | + } catch { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + if (!snapshot || snapshot.version !== MIGRATION_SNAPSHOT_VERSION) return 0; |
| 116 | + |
| 117 | + const entries = Object.entries(snapshot.keys ?? {}); |
| 118 | + let hydrated = 0; |
| 119 | + if (typeof window !== "undefined") { |
| 120 | + for (const [key, value] of entries) { |
| 121 | + if (!matchesMigrationKey(key)) continue; |
| 122 | + if (window.localStorage.getItem(key) != null) continue; |
| 123 | + try { |
| 124 | + window.localStorage.setItem(key, value); |
| 125 | + hydrated += 1; |
| 126 | + } catch { |
| 127 | + // localStorage write failures are non-fatal; the user just won't |
| 128 | + // see that key migrated this launch. |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + await bridge.ackSnapshot(); |
| 135 | + } catch { |
| 136 | + // A failed ack means we'll re-ingest on next launch, but the |
| 137 | + // "skip if already set" guard keeps that idempotent. |
| 138 | + } |
| 139 | + |
| 140 | + return hydrated; |
| 141 | +} |
0 commit comments