feat(renderer): add updater store mirroring main process state

This commit is contained in:
2026-06-08 06:27:13 +05:30
parent ef88b6343b
commit d87f175212
2 changed files with 94 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
import { create } from 'zustand';
import { ipc } from '@/lib/ipc';
type State = 'idle' | 'checking' | 'available' | 'downloading' | 'ready' | 'error';
interface UpdaterState {
state: State;
version: string | null;
percent: number;
lastCheckAt: number;
applyStatus: (s: { state: State; version?: string; percent?: number; code?: string }) => void;
check: () => Promise<void>;
install: () => void;
}
const DEBOUNCE_MS = 60_000;
export const useUpdaterStore = create<UpdaterState>((set, get) => ({
state: 'idle',
version: null,
percent: 0,
lastCheckAt: 0,
applyStatus: (s) =>
set({
state: s.state,
version: s.version ?? get().version,
percent: s.percent ?? get().percent,
}),
check: async () => {
if (Date.now() - get().lastCheckAt < DEBOUNCE_MS) return;
set({ lastCheckAt: Date.now() });
await ipc.updater.check();
},
install: () => ipc.updater.install(),
}));
// Subscribe to live updates from main
if (typeof window !== 'undefined' && (window as any).electronAPI?.updater?.onStatus) {
(window as any).electronAPI.updater.onStatus((payload: any) => {
useUpdaterStore.getState().applyStatus(payload);
});
}