Skip to content

Commit 97a87de

Browse files
committed
refactor: state restore
Signed-off-by: Adam Setch <[email protected]>
1 parent d51cbbf commit 97a87de

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/renderer/context/App.tsx

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
106106
const { setColorMode, setDayScheme, setNightScheme } = useTheme();
107107
const [auth, setAuth] = useState<AuthState>(defaultAuth);
108108
const [settings, setSettings] = useState<SettingsState>(defaultSettings);
109+
const [needsAccountRefresh, setNeedsAccountRefresh] = useState(false);
110+
109111
const {
110112
removeAccountNotifications,
111113
fetchNotifications,
@@ -134,6 +136,34 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
134136
restoreSettings();
135137
}, []);
136138

139+
// Refresh account details on startup or restore
140+
useEffect(() => {
141+
if (!needsAccountRefresh || auth.accounts.length === 0) {
142+
return;
143+
}
144+
145+
(async () => {
146+
for (const account of auth.accounts) {
147+
await refreshAccount(account);
148+
}
149+
150+
setNeedsAccountRefresh(false);
151+
})();
152+
}, [needsAccountRefresh, auth.accounts]);
153+
154+
useIntervalTimer(() => {
155+
for (const account of auth.accounts) {
156+
refreshAccount(account);
157+
}
158+
}, Constants.REFRESH_ACCOUNTS_INTERVAL_MS);
159+
160+
// Apply zoom level when settings change
161+
useEffect(() => {
162+
globalThis.gitify.zoom.setLevel(
163+
zoomPercentageToLevel(settings.zoomPercentage),
164+
);
165+
}, [settings.zoomPercentage]);
166+
137167
useEffect(() => {
138168
const colorMode = mapThemeModeToColorMode(settings.theme);
139169
const colorScheme = mapThemeModeToColorScheme(
@@ -179,12 +209,6 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
179209
settings.fetchType === FetchType.INACTIVITY ? settings.fetchInterval : null,
180210
);
181211

182-
useIntervalTimer(() => {
183-
for (const account of auth.accounts) {
184-
refreshAccount(account);
185-
}
186-
}, Constants.REFRESH_ACCOUNTS_INTERVAL_MS);
187-
188212
// biome-ignore lint/correctness/useExhaustiveDependencies: We want to update the tray on setting or notification changes
189213
useEffect(() => {
190214
setUseUnreadActiveIcon(settings.useUnreadActiveIcon);
@@ -208,7 +232,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
208232
}, [settings.openAtStartup]);
209233

210234
useEffect(() => {
211-
window.gitify.onResetApp(() => {
235+
globalThis.gitify.onResetApp(() => {
212236
clearState();
213237
setAuth(defaultAuth);
214238
setSettings(defaultSettings);
@@ -309,33 +333,13 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
309333

310334
// Restore settings before accounts to ensure filters are available before fetching notifications
311335
if (existing.settings) {
312-
setUseUnreadActiveIcon(existing.settings.useUnreadActiveIcon);
313-
setUseAlternateIdleIcon(existing.settings.useAlternateIdleIcon);
314-
setKeyboardShortcut(existing.settings.keyboardShortcut);
315336
setSettings({ ...defaultSettings, ...existing.settings });
316-
window.gitify.zoom.setLevel(
317-
zoomPercentageToLevel(existing.settings.zoomPercentage),
318-
);
319337
}
320338

321339
if (existing.auth) {
322340
setAuth({ ...defaultAuth, ...existing.auth });
323-
324-
// Refresh account data on app start
325-
for (const account of existing.auth.accounts) {
326-
/**
327-
* Check if the account is using an encrypted token.
328-
* If not encrypt it and save it.
329-
*/
330-
try {
331-
await decryptValue(account.token);
332-
} catch (_err) {
333-
const encryptedToken = await encryptValue(account.token);
334-
account.token = encryptedToken as Token;
335-
}
336-
337-
await refreshAccount(account);
338-
}
341+
// Trigger the effect to refresh accounts and handle token encryption
342+
setNeedsAccountRefresh(true);
339343
}
340344
}, []);
341345

0 commit comments

Comments
 (0)