From ea229c469fefd82ce769c47dccb297e9c4a206f8 Mon Sep 17 00:00:00 2001 From: "LAPTOP-O016UC3M\\Qi Chen" <16399091+binaricat@users.noreply.github.com> Date: Mon, 22 Dec 2025 16:03:33 +0800 Subject: [PATCH] Optimize settings sync and lazy load sync tab Refactors settings change handling to consolidate appearance-related updates into a single method, reducing redundant logic and improving maintainability. Adds lazy loading for the sync tab to improve performance and initial load times of the settings page. Ensures tabs are only mounted when accessed, saving resources. --- application/state/useSettingsState.ts | 75 ++++++++------- components/SettingsPage.tsx | 127 ++++++++++++++++---------- 2 files changed, 122 insertions(+), 80 deletions(-) diff --git a/application/state/useSettingsState.ts b/application/state/useSettingsState.ts index 136e1455b..5bff5a88e 100644 --- a/application/state/useSettingsState.ts +++ b/application/state/useSettingsState.ts @@ -163,6 +163,33 @@ export const useSettingsState = () => { } }, []); + const syncAppearanceFromStorage = useCallback(() => { + const storedTheme = readStoredString(STORAGE_KEY_THEME); + const nextTheme = storedTheme && isValidTheme(storedTheme) ? storedTheme : theme; + const storedLightId = readStoredString(STORAGE_KEY_UI_THEME_LIGHT); + const nextLightId = storedLightId && isValidUiThemeId('light', storedLightId) ? storedLightId : lightUiThemeId; + const storedDarkId = readStoredString(STORAGE_KEY_UI_THEME_DARK); + const nextDarkId = storedDarkId && isValidUiThemeId('dark', storedDarkId) ? storedDarkId : darkUiThemeId; + const storedAccentMode = readStoredString(STORAGE_KEY_ACCENT_MODE); + const nextAccentMode = storedAccentMode === 'theme' || storedAccentMode === 'custom' ? storedAccentMode : accentMode; + const storedAccent = readStoredString(STORAGE_KEY_COLOR); + const nextAccent = storedAccent && isValidHslToken(storedAccent) ? storedAccent.trim() : customAccent; + + setTheme(nextTheme); + setLightUiThemeId(nextLightId); + setDarkUiThemeId(nextDarkId); + setAccentMode(nextAccentMode); + setCustomAccent(nextAccent); + + const tokens = getUiThemeById(nextTheme, nextTheme === 'dark' ? nextDarkId : nextLightId).tokens; + applyThemeTokens(nextTheme, tokens, nextAccentMode, nextAccent); + }, [theme, lightUiThemeId, darkUiThemeId, accentMode, customAccent]); + + const syncCustomCssFromStorage = useCallback(() => { + const storedCss = localStorageAdapter.readString(STORAGE_KEY_CUSTOM_CSS) || ''; + setCustomCSS((prev) => (prev === storedCss ? prev : storedCss)); + }, []); + useLayoutEffect(() => { const tokens = getUiThemeById(theme, theme === 'dark' ? darkUiThemeId : lightUiThemeId).tokens; applyThemeTokens(theme, tokens, accentMode, customAccent); @@ -192,43 +219,24 @@ export const useSettingsState = () => { if (!bridge?.onSettingsChanged) return; const unsubscribe = bridge.onSettingsChanged((payload) => { const { key, value } = payload; - if (key === STORAGE_KEY_THEME && (value === 'light' || value === 'dark')) { - setTheme(value); - const tokens = getUiThemeById(value, value === 'dark' ? darkUiThemeId : lightUiThemeId).tokens; - applyThemeTokens(value, tokens, accentMode, customAccent); - } - if (key === STORAGE_KEY_UI_THEME_LIGHT && typeof value === 'string' && isValidUiThemeId('light', value)) { - setLightUiThemeId(value); - if (theme === 'light') { - const tokens = getUiThemeById('light', value).tokens; - applyThemeTokens('light', tokens, accentMode, customAccent); - } - } - if (key === STORAGE_KEY_UI_THEME_DARK && typeof value === 'string' && isValidUiThemeId('dark', value)) { - setDarkUiThemeId(value); - if (theme === 'dark') { - const tokens = getUiThemeById('dark', value).tokens; - applyThemeTokens('dark', tokens, accentMode, customAccent); - } - } - if (key === STORAGE_KEY_ACCENT_MODE && (value === 'theme' || value === 'custom')) { - setAccentMode(value); - const tokens = getUiThemeById(theme, theme === 'dark' ? darkUiThemeId : lightUiThemeId).tokens; - applyThemeTokens(theme, tokens, value, customAccent); - } - if (key === STORAGE_KEY_COLOR && typeof value === 'string' && isValidHslToken(value)) { - const next = value.trim(); - setCustomAccent(next); - if (accentMode === 'custom') { - const tokens = getUiThemeById(theme, theme === 'dark' ? darkUiThemeId : lightUiThemeId).tokens; - applyThemeTokens(theme, tokens, accentMode, next); - } + if ( + key === STORAGE_KEY_THEME || + key === STORAGE_KEY_UI_THEME_LIGHT || + key === STORAGE_KEY_UI_THEME_DARK || + key === STORAGE_KEY_ACCENT_MODE || + key === STORAGE_KEY_COLOR + ) { + syncAppearanceFromStorage(); + return; } if (key === STORAGE_KEY_UI_LANGUAGE && typeof value === 'string') { const next = resolveSupportedLocale(value); setUiLanguage((prev) => (prev === next ? prev : next)); document.documentElement.lang = next; } + if (key === STORAGE_KEY_CUSTOM_CSS && typeof value === 'string') { + syncCustomCssFromStorage(); + } if (key === STORAGE_KEY_TERM_THEME && typeof value === 'string') { setTerminalThemeId(value); } @@ -263,7 +271,7 @@ export const useSettingsState = () => { // ignore } }; - }, [theme, lightUiThemeId, darkUiThemeId, accentMode, customAccent]); + }, [syncAppearanceFromStorage, syncCustomCssFromStorage]); useEffect(() => { const bridge = netcattyBridge.get(); @@ -406,6 +414,7 @@ export const useSettingsState = () => { // Apply and persist custom CSS useEffect(() => { localStorageAdapter.writeString(STORAGE_KEY_CUSTOM_CSS, customCSS); + notifySettingsChanged(STORAGE_KEY_CUSTOM_CSS, customCSS); // Apply custom CSS to document let styleEl = document.getElementById('netcatty-custom-css') as HTMLStyleElement | null; @@ -415,7 +424,7 @@ export const useSettingsState = () => { document.head.appendChild(styleEl); } styleEl.textContent = customCSS; - }, [customCSS]); + }, [customCSS, notifySettingsChanged]); // Get merged key bindings (defaults + custom overrides) const keyBindings = useMemo((): KeyBinding[] => { diff --git a/components/SettingsPage.tsx b/components/SettingsPage.tsx index aeceede67..e4ba15b7e 100644 --- a/components/SettingsPage.tsx +++ b/components/SettingsPage.tsx @@ -3,7 +3,7 @@ * This component is rendered in a separate Electron window */ import { AppWindow, Cloud, Keyboard, Palette, TerminalSquare, X } from "lucide-react"; -import React, { useCallback, useEffect } from "react"; +import React, { useCallback, useEffect, useState } from "react"; import { useSettingsState } from "../application/state/useSettingsState"; import { useVaultState } from "../application/state/useVaultState"; import { useWindowControls } from "../application/state/useWindowControls"; @@ -11,7 +11,6 @@ import { I18nProvider, useI18n } from "../application/i18n/I18nProvider"; import SettingsApplicationTab from "./SettingsApplicationTab"; import SettingsAppearanceTab from "./settings/tabs/SettingsAppearanceTab"; import SettingsShortcutsTab from "./settings/tabs/SettingsShortcutsTab"; -import SettingsSyncTab from "./settings/tabs/SettingsSyncTab"; import SettingsTerminalTab from "./settings/tabs/SettingsTerminalTab"; import { Tabs, TabsList, TabsTrigger } from "./ui/tabs"; @@ -19,15 +18,40 @@ const isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigat type SettingsState = ReturnType; +const SettingsSyncTab = React.lazy(() => import("./settings/tabs/SettingsSyncTab")); + +const SettingsSyncTabWithVault: React.FC = () => { + const { hosts, keys, identities, snippets, importDataFromString } = useVaultState(); + + return ( + + ); +}; + const SettingsPageContent: React.FC<{ settings: SettingsState }> = ({ settings }) => { const { t } = useI18n(); const { notifyRendererReady, closeSettingsWindow } = useWindowControls(); + const [activeTab, setActiveTab] = useState("application"); + const [mountedTabs, setMountedTabs] = useState(() => new Set(["application"])); useEffect(() => { notifyRendererReady(); }, [notifyRendererReady]); - const { hosts, keys, identities, snippets, importDataFromString } = useVaultState(); + useEffect(() => { + setMountedTabs((prev) => { + if (prev.has(activeTab)) return prev; + const next = new Set(prev); + next.add(activeTab); + return next; + }); + }, [activeTab]); const handleClose = useCallback(() => { closeSettingsWindow(); @@ -53,7 +77,12 @@ const SettingsPageContent: React.FC<{ settings: SettingsState }> = ({ settings } - +
= ({ settings }
- + {mountedTabs.has("application") && } - + {mountedTabs.has("appearance") && ( + + )} - + {mountedTabs.has("terminal") && ( + + )} - + {mountedTabs.has("shortcuts") && ( + + )} - + {mountedTabs.has("sync") && ( + + + + )}