fix: keep localStorage cache in sync with server settings

When switching between Electron and web modes or when the server temporarily
stops, web mode was falling back to stale localStorage data instead of fresh
server data.

This fix:
1. Updates localStorage cache whenever fresh server settings are fetched
2. Updates localStorage cache whenever settings are synced to server
3. Prioritizes fresh settings cache over old Zustand persisted storage

This ensures that:
- Web mode always sees the latest projects even after mode switches
- Switching from Electron to web mode immediately shows new projects
- Server restarts don't cause web mode to use stale cached data

Fixes issue where projects opened in Electron didn't appear in web mode
after stopping and restarting the server.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
DhanushSantosh
2026-01-18 02:46:31 +05:30
parent b66efae5b7
commit 9137f0e75f
2 changed files with 32 additions and 0 deletions

View File

@@ -114,6 +114,20 @@ export function resetMigrationState(): void {
*/
export function parseLocalStorageSettings(): Partial<GlobalSettings> | null {
try {
// First, check for fresh server settings cache (updated whenever server settings are fetched)
// This prevents stale data when switching between modes
const settingsCache = getItem('automaker-settings-cache');
if (settingsCache) {
try {
const cached = JSON.parse(settingsCache) as GlobalSettings;
logger.debug('Using fresh settings cache from localStorage');
return cached;
} catch (e) {
logger.warn('Failed to parse settings cache, falling back to old storage');
}
}
// Fall back to old Zustand persisted storage
const automakerStorage = getItem('automaker-storage');
if (!automakerStorage) {
return null;
@@ -412,6 +426,15 @@ export function useSettingsMigration(): MigrationState {
if (global.success && global.settings) {
serverSettings = global.settings as unknown as GlobalSettings;
logger.info(`Server has ${serverSettings.projects?.length ?? 0} projects`);
// Update localStorage with fresh server data to keep cache in sync
// This prevents stale localStorage data from being used when switching between modes
try {
localStorage.setItem('automaker-settings-cache', JSON.stringify(serverSettings));
logger.debug('Updated localStorage with fresh server settings');
} catch (storageError) {
logger.warn('Failed to update localStorage cache:', storageError);
}
}
} catch (error) {
logger.error('Failed to fetch server settings:', error);

View File

@@ -215,6 +215,15 @@ export function useSettingsSync(): SettingsSyncState {
if (result.success) {
lastSyncedRef.current = updateHash;
logger.debug('Settings synced to server');
// Update localStorage cache with synced settings to keep it fresh
// This prevents stale data when switching between Electron and web modes
try {
setItem('automaker-settings-cache', JSON.stringify(updates));
logger.debug('Updated localStorage cache after sync');
} catch (storageError) {
logger.warn('Failed to update localStorage cache after sync:', storageError);
}
} else {
logger.error('Failed to sync settings:', result.error);
}