fix(data): 增加全局保存失败保护

This commit is contained in:
Script Generator
2026-07-03 19:36:18 +08:00
parent 516a5f1b63
commit b4a3c990b4
17 changed files with 727 additions and 132 deletions

View File

@@ -3,6 +3,7 @@ import { create } from 'zustand';
import type { Bug, BugStatus, BugLog } from '@/lib/bug';
import { generateBugNo } from '@/lib/bug';
import { applyBugTransition } from '@/lib/bug-workflow';
import { scheduleSaveWithOptimisticRollback } from '@/lib/optimistic-persistence';
import { loadServerData, saveServerData, SERVER_DATA_CACHE_MS } from '@/lib/server-data';
import {
makeBugCreatedActivity,
@@ -11,10 +12,6 @@ import {
} from '@/lib/work-activity-factory';
import { useWorkActivityStore } from './useWorkActivityStore';
function saveStored(items: Bug[]) {
saveServerData('bugs', items).catch(() => {});
}
let lastBugsFetchAt = 0;
async function loadStored(): Promise<Bug[] | null> {
@@ -69,23 +66,40 @@ export const useBugStore = create<BugState>((set, get) => ({
};
const updated = [...list, bug];
set({ bugs: updated, loaded: true });
saveStored(updated);
scheduleSaveWithOptimisticRollback({
save: () => saveServerData('bugs', updated),
expected: updated,
getCurrent: () => get().bugs,
rollback: () => set({ bugs: list, loaded: true }),
});
useWorkActivityStore.getState().addActivity(makeBugCreatedActivity(bug, operator));
return bug;
},
updateBug: (id, data) => {
const updated = get().bugs.map((b) =>
const previous = get().bugs;
const updated = previous.map((b) =>
b.id === id ? { ...b, ...data, updatedAt: new Date().toISOString() } : b,
);
set({ bugs: updated, loaded: true });
saveStored(updated);
scheduleSaveWithOptimisticRollback({
save: () => saveServerData('bugs', updated),
expected: updated,
getCurrent: () => get().bugs,
rollback: () => set({ bugs: previous, loaded: true }),
});
},
deleteBug: (id) => {
const updated = get().bugs.filter((b) => b.id !== id);
const previous = get().bugs;
const updated = previous.filter((b) => b.id !== id);
set({ bugs: updated, loaded: true });
saveStored(updated);
scheduleSaveWithOptimisticRollback({
save: () => saveServerData('bugs', updated),
expected: updated,
getCurrent: () => get().bugs,
rollback: () => set({ bugs: previous, loaded: true }),
});
},
changeStatus: (id, to, operator, extra) => {