fix: better rebuilding of database

This commit is contained in:
2026-02-11 06:05:18 +01:00
parent 77e117ae06
commit b7b1a4881f
7 changed files with 102 additions and 27 deletions

View File

@@ -144,6 +144,13 @@ const App: React.FC = () => {
);
// Task events
unsubscribers.push(
window.electronAPI?.on('task:started', (task: unknown) => {
const t = task as TaskProgress;
updateTask(t.taskId, t);
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('task:progress', (task: unknown) => {
const t = task as TaskProgress;
@@ -235,22 +242,47 @@ const App: React.FC = () => {
}) || (() => {})
);
// Rebuild events - clear store on start, reload on complete
unsubscribers.push(
window.electronAPI?.on('menu:rebuildDatabase', async () => {
await window.electronAPI?.posts.rebuildFromFiles();
await window.electronAPI?.media.rebuildFromFiles();
// Reload data
const posts = await window.electronAPI?.posts.getAll();
if (posts) {
setPosts(posts as PostData[]);
window.electronAPI?.on('posts:rebuildStarted', () => {
setPosts([], false, 0);
setSelectedPost(null);
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('posts:databaseRebuilt', async () => {
const postsResult = await window.electronAPI?.posts.getAll({ limit: 500, offset: 0 });
if (postsResult) {
const { items, hasMore, total } = postsResult as { items: PostData[]; hasMore: boolean; total: number };
setPosts(items, hasMore, total);
}
const media = await window.electronAPI?.media.getAll();
if (media) {
setMedia(media as MediaData[]);
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('media:rebuildStarted', () => {
setMedia([]);
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('media:databaseRebuilt', async () => {
const mediaResult = await window.electronAPI?.media.getAll();
if (mediaResult) {
setMedia(mediaResult as MediaData[]);
}
}) || (() => {})
);
unsubscribers.push(
window.electronAPI?.on('menu:rebuildDatabase', () => {
// Fire and forget - the handlers return immediately now
window.electronAPI?.posts.rebuildFromFiles();
window.electronAPI?.media.rebuildFromFiles();
}) || (() => {})
);
return () => {
unsubscribers.forEach(unsub => unsub());
};

View File

@@ -255,9 +255,14 @@ export const useAppStore = create<AppState>()(
// Task Actions
setTasks: (tasks) => set({ tasks }),
updateTask: (taskId, task) => set((state) => ({
tasks: state.tasks.map((t) => (t.taskId === taskId ? { ...t, ...task } : t)),
})),
updateTask: (taskId, task) => set((state) => {
const exists = state.tasks.some((t) => t.taskId === taskId);
if (exists) {
return { tasks: state.tasks.map((t) => (t.taskId === taskId ? { ...t, ...task } : t)) };
}
// Add new task if it doesn't exist yet
return { tasks: [...state.tasks, { taskId, status: 'running', progress: 0, message: '', startTime: new Date().toISOString(), ...task } as TaskProgress] };
}),
// Sync Actions
setSyncStatus: (syncStatus) => set({ syncStatus }),