-
Notifications
You must be signed in to change notification settings - Fork 893
Persist desktop settings to settings.json across releases #1191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamesx0416
wants to merge
1
commit into
pingdotgg:main
Choose a base branch
from
jamesx0416:t3code/settings-persistence-across-versions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import type { DesktopSettings, DesktopSettingsInput, ThemePreference } from "@t3tools/contracts"; | ||
| import { | ||
| AppSettings as AppSettingsSchema, | ||
| DesktopSettingsSchemaVersion, | ||
| ThemePreference as ThemePreferenceSchema, | ||
| } from "@t3tools/contracts"; | ||
| import { Schema } from "effect"; | ||
| import { APP_VERSION } from "./branding"; | ||
| import { isElectron } from "./env"; | ||
| import { | ||
| dispatchLocalStorageChange, | ||
| getLocalStorageItem, | ||
| setLocalStorageItem, | ||
| } from "./hooks/useLocalStorage"; | ||
| import type { AppSettings } from "./appSettings"; | ||
|
|
||
| const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1"; | ||
| const THEME_STORAGE_KEY = "t3code:theme"; | ||
| const DEFAULT_APP_SETTINGS = AppSettingsSchema.makeUnsafe({}); | ||
| const DEFAULT_THEME: ThemePreference = "system"; | ||
| const INITIAL_DESKTOP_SETTINGS = isElectron | ||
| ? (window.desktopBridge?.initialSettings ?? null) | ||
| : null; | ||
|
|
||
| let desktopSettingsHydrated = false; | ||
| let lastPersistedSerialized: string | null = null; | ||
| let latestTheme: ThemePreference = DEFAULT_THEME; | ||
| let latestAppSettings: AppSettings = DEFAULT_APP_SETTINGS; | ||
|
|
||
| function readThemeFromLocalStorage(): ThemePreference { | ||
| const raw = localStorage.getItem(THEME_STORAGE_KEY); | ||
| if (raw === "light" || raw === "dark" || raw === "system") { | ||
| return raw; | ||
| } | ||
| return "system"; | ||
| } | ||
|
|
||
| function buildDesktopSettingsInput(): DesktopSettingsInput { | ||
| return { | ||
| theme: latestTheme, | ||
| appSettings: latestAppSettings, | ||
| }; | ||
| } | ||
|
|
||
| function serializeSettingsInput(input: DesktopSettingsInput): string { | ||
| return JSON.stringify(input); | ||
| } | ||
|
|
||
| function hasLegacySettingsInLocalStorage(): boolean { | ||
| return ( | ||
| localStorage.getItem(THEME_STORAGE_KEY) !== null || | ||
| localStorage.getItem(APP_SETTINGS_STORAGE_KEY) !== null | ||
| ); | ||
| } | ||
|
|
||
| function applyDesktopSettings(settings: DesktopSettings): void { | ||
| localStorage.setItem(THEME_STORAGE_KEY, settings.theme); | ||
| setLocalStorageItem(APP_SETTINGS_STORAGE_KEY, settings.appSettings, AppSettingsSchema); | ||
| dispatchLocalStorageChange(APP_SETTINGS_STORAGE_KEY); | ||
| window.dispatchEvent(new StorageEvent("storage", { key: THEME_STORAGE_KEY })); | ||
| latestTheme = settings.theme; | ||
| latestAppSettings = settings.appSettings; | ||
| lastPersistedSerialized = serializeSettingsInput({ | ||
| theme: settings.theme, | ||
| appSettings: settings.appSettings, | ||
| }); | ||
| } | ||
|
|
||
| if (INITIAL_DESKTOP_SETTINGS) { | ||
| applyDesktopSettings(INITIAL_DESKTOP_SETTINGS); | ||
| desktopSettingsHydrated = true; | ||
| } | ||
|
|
||
| export async function hydrateDesktopSettings(): Promise<void> { | ||
| if (!isElectron || !window.desktopBridge) { | ||
| return; | ||
| } | ||
|
|
||
| if (INITIAL_DESKTOP_SETTINGS) { | ||
| if ( | ||
| INITIAL_DESKTOP_SETTINGS.version !== APP_VERSION || | ||
| INITIAL_DESKTOP_SETTINGS.schemaVersion !== DesktopSettingsSchemaVersion | ||
| ) { | ||
| await persistDesktopSettings({ force: true }).catch(() => {}); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| desktopSettingsHydrated = true; | ||
| latestTheme = Schema.decodeSync(ThemePreferenceSchema)(readThemeFromLocalStorage()); | ||
| latestAppSettings = | ||
| getLocalStorageItem(APP_SETTINGS_STORAGE_KEY, AppSettingsSchema) ?? DEFAULT_APP_SETTINGS; | ||
| if (!hasLegacySettingsInLocalStorage()) { | ||
| lastPersistedSerialized = serializeSettingsInput({ | ||
| theme: DEFAULT_THEME, | ||
| appSettings: DEFAULT_APP_SETTINGS, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| await persistDesktopSettings({ force: true }).catch(() => {}); | ||
| } | ||
|
|
||
| export async function persistDesktopSettings(options?: { | ||
| force?: boolean; | ||
| theme?: ThemePreference; | ||
| appSettings?: AppSettings; | ||
| }): Promise<void> { | ||
| if (!isElectron || !window.desktopBridge || !desktopSettingsHydrated) { | ||
| return; | ||
| } | ||
|
|
||
| if (options?.theme) { | ||
| latestTheme = Schema.decodeSync(ThemePreferenceSchema)(options.theme); | ||
| } | ||
| if (options?.appSettings) { | ||
| latestAppSettings = options.appSettings; | ||
| } | ||
|
|
||
| const next = buildDesktopSettingsInput(); | ||
| const serialized = serializeSettingsInput(next); | ||
| if (!options?.force && serialized === lastPersistedSerialized) { | ||
| return; | ||
| } | ||
|
|
||
| const persisted = await window.desktopBridge.setSettings(next); | ||
| lastPersistedSerialized = serializeSettingsInput({ | ||
| theme: persisted.theme, | ||
| appSettings: persisted.appSettings, | ||
| }); | ||
| } | ||
|
|
||
| export function getBootDesktopTheme(): ThemePreference | null { | ||
| return desktopSettingsHydrated ? latestTheme : null; | ||
| } | ||
|
|
||
| export function getBootDesktopAppSettings(): AppSettings | null { | ||
| return desktopSettingsHydrated ? latestAppSettings : null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 Low
src/main.ts:151Using a fixed temp file name
${settingsPath}.tmpcreates a race condition when concurrent calls towriteSettingsFileoccur. If two IPCSET_SETTINGS_CHANNELcalls overlap, the second write can overwrite the temp file before the first rename, causing incorrect data to be persisted. The second call's rename may also fail with ENOENT if the first call already moved the file. Consider using a unique temp file name per call (e.g., withcrypto.randomUUID()orDate.now()) to ensure atomic writes.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: