mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 01:40:45 +00:00
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.
This commit is contained in:
2 files changed
+122
-80
No files matched your search
@@ -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[] => {
|
||||
|
||||
+80
-47
@@ -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<typeof useSettingsState>;
|
||||
|
||||
const SettingsSyncTab = React.lazy(() => import("./settings/tabs/SettingsSyncTab"));
|
||||
|
||||
const SettingsSyncTabWithVault: React.FC = () => {
|
||||
const { hosts, keys, identities, snippets, importDataFromString } = useVaultState();
|
||||
|
||||
return (
|
||||
<SettingsSyncTab
|
||||
hosts={hosts}
|
||||
keys={keys}
|
||||
identities={identities}
|
||||
snippets={snippets}
|
||||
importDataFromString={importDataFromString}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
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 }
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Tabs defaultValue="application" orientation="vertical" className="flex-1 flex overflow-hidden">
|
||||
<Tabs
|
||||
value={activeTab}
|
||||
onValueChange={setActiveTab}
|
||||
orientation="vertical"
|
||||
className="flex-1 flex overflow-hidden"
|
||||
>
|
||||
<div className="w-56 border-r border-border flex flex-col shrink-0 px-3 py-3">
|
||||
<TabsList className="flex flex-col h-auto bg-transparent gap-1 p-0 justify-start">
|
||||
<TabsTrigger
|
||||
@@ -90,53 +119,57 @@ const SettingsPageContent: React.FC<{ settings: SettingsState }> = ({ settings }
|
||||
</div>
|
||||
|
||||
<div className="flex-1 h-full flex flex-col min-h-0 bg-muted/10">
|
||||
<SettingsApplicationTab />
|
||||
{mountedTabs.has("application") && <SettingsApplicationTab />}
|
||||
|
||||
<SettingsAppearanceTab
|
||||
theme={settings.theme}
|
||||
setTheme={settings.setTheme}
|
||||
lightUiThemeId={settings.lightUiThemeId}
|
||||
setLightUiThemeId={settings.setLightUiThemeId}
|
||||
darkUiThemeId={settings.darkUiThemeId}
|
||||
setDarkUiThemeId={settings.setDarkUiThemeId}
|
||||
accentMode={settings.accentMode}
|
||||
setAccentMode={settings.setAccentMode}
|
||||
customAccent={settings.customAccent}
|
||||
setCustomAccent={settings.setCustomAccent}
|
||||
uiLanguage={settings.uiLanguage}
|
||||
setUiLanguage={settings.setUiLanguage}
|
||||
customCSS={settings.customCSS}
|
||||
setCustomCSS={settings.setCustomCSS}
|
||||
/>
|
||||
{mountedTabs.has("appearance") && (
|
||||
<SettingsAppearanceTab
|
||||
theme={settings.theme}
|
||||
setTheme={settings.setTheme}
|
||||
lightUiThemeId={settings.lightUiThemeId}
|
||||
setLightUiThemeId={settings.setLightUiThemeId}
|
||||
darkUiThemeId={settings.darkUiThemeId}
|
||||
setDarkUiThemeId={settings.setDarkUiThemeId}
|
||||
accentMode={settings.accentMode}
|
||||
setAccentMode={settings.setAccentMode}
|
||||
customAccent={settings.customAccent}
|
||||
setCustomAccent={settings.setCustomAccent}
|
||||
uiLanguage={settings.uiLanguage}
|
||||
setUiLanguage={settings.setUiLanguage}
|
||||
customCSS={settings.customCSS}
|
||||
setCustomCSS={settings.setCustomCSS}
|
||||
/>
|
||||
)}
|
||||
|
||||
<SettingsTerminalTab
|
||||
terminalThemeId={settings.terminalThemeId}
|
||||
setTerminalThemeId={settings.setTerminalThemeId}
|
||||
terminalFontFamilyId={settings.terminalFontFamilyId}
|
||||
setTerminalFontFamilyId={settings.setTerminalFontFamilyId}
|
||||
terminalFontSize={settings.terminalFontSize}
|
||||
setTerminalFontSize={settings.setTerminalFontSize}
|
||||
terminalSettings={settings.terminalSettings}
|
||||
updateTerminalSetting={settings.updateTerminalSetting}
|
||||
/>
|
||||
{mountedTabs.has("terminal") && (
|
||||
<SettingsTerminalTab
|
||||
terminalThemeId={settings.terminalThemeId}
|
||||
setTerminalThemeId={settings.setTerminalThemeId}
|
||||
terminalFontFamilyId={settings.terminalFontFamilyId}
|
||||
setTerminalFontFamilyId={settings.setTerminalFontFamilyId}
|
||||
terminalFontSize={settings.terminalFontSize}
|
||||
setTerminalFontSize={settings.setTerminalFontSize}
|
||||
terminalSettings={settings.terminalSettings}
|
||||
updateTerminalSetting={settings.updateTerminalSetting}
|
||||
/>
|
||||
)}
|
||||
|
||||
<SettingsShortcutsTab
|
||||
hotkeyScheme={settings.hotkeyScheme}
|
||||
setHotkeyScheme={settings.setHotkeyScheme}
|
||||
keyBindings={settings.keyBindings}
|
||||
updateKeyBinding={settings.updateKeyBinding}
|
||||
resetKeyBinding={settings.resetKeyBinding}
|
||||
resetAllKeyBindings={settings.resetAllKeyBindings}
|
||||
setIsHotkeyRecording={settings.setIsHotkeyRecording}
|
||||
/>
|
||||
{mountedTabs.has("shortcuts") && (
|
||||
<SettingsShortcutsTab
|
||||
hotkeyScheme={settings.hotkeyScheme}
|
||||
setHotkeyScheme={settings.setHotkeyScheme}
|
||||
keyBindings={settings.keyBindings}
|
||||
updateKeyBinding={settings.updateKeyBinding}
|
||||
resetKeyBinding={settings.resetKeyBinding}
|
||||
resetAllKeyBindings={settings.resetAllKeyBindings}
|
||||
setIsHotkeyRecording={settings.setIsHotkeyRecording}
|
||||
/>
|
||||
)}
|
||||
|
||||
<SettingsSyncTab
|
||||
hosts={hosts}
|
||||
keys={keys}
|
||||
identities={identities}
|
||||
snippets={snippets}
|
||||
importDataFromString={importDataFromString}
|
||||
/>
|
||||
{mountedTabs.has("sync") && (
|
||||
<React.Suspense fallback={null}>
|
||||
<SettingsSyncTabWithVault />
|
||||
</React.Suspense>
|
||||
)}
|
||||
</div>
|
||||
</Tabs>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user