()
| 906 | } |
| 907 | |
| 908 | function loadPrefs(): Prefs { |
| 909 | if (cachedInitialPrefs) return cachedInitialPrefs |
| 910 | |
| 911 | let base: Partial<Prefs> = {} |
| 912 | let hadLocalStorage = false |
| 913 | try { |
| 914 | const raw = localStorage.getItem(PREFS_KEY) |
| 915 | if (raw) { |
| 916 | base = JSON.parse(raw) as Partial<Prefs> |
| 917 | hadLocalStorage = true |
| 918 | } |
| 919 | } catch { |
| 920 | /* ignore */ |
| 921 | } |
| 922 | |
| 923 | const fileConfig = readConfigFromBridge() |
| 924 | configFileEnabled = fileConfig !== null |
| 925 | configFileHadContent = !!fileConfig && Object.keys(fileConfig).length > 0 |
| 926 | |
| 927 | // The file wins for portable keys; localStorage supplies machine-local keys. |
| 928 | const merged: Partial<Prefs> = configFileHadContent |
| 929 | ? { ...base, ...(fileConfig as Partial<Prefs>) } |
| 930 | : base |
| 931 | |
| 932 | const normalized = normalizePrefs(merged) |
| 933 | |
| 934 | // Don't greet returning users with the onboarding wizard: an existing prefs |
| 935 | // blob or a populated config file both mean they've been here before. |
| 936 | if ( |
| 937 | (hadLocalStorage && typeof base.hasCompletedOnboarding !== 'boolean') || |
| 938 | configFileHadContent |
| 939 | ) { |
| 940 | normalized.hasCompletedOnboarding = true |
| 941 | } |
| 942 | |
| 943 | // When the config file is authoritative, refresh the localStorage cache so |
| 944 | // other same-origin renderers (e.g. the quick-capture window) and the next |
| 945 | // launch see the synced values immediately. |
| 946 | if (configFileHadContent) { |
| 947 | try { |
| 948 | localStorage.setItem(PREFS_KEY, JSON.stringify(normalized)) |
| 949 | } catch { |
| 950 | /* ignore */ |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | cachedInitialPrefs = hadLocalStorage || configFileHadContent ? normalized : DEFAULT_PREFS |
| 955 | return cachedInitialPrefs |
| 956 | } |
| 957 | |
| 958 | let configPushTimer: ReturnType<typeof setTimeout> | null = null |
| 959 | const CONFIG_PUSH_DEBOUNCE_MS = 400 |
no test coverage detected