perf(web): 优化大数据量页面切换与聚合性能
This commit is contained in:
@@ -3,7 +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 { loadServerData, saveServerData } from '@/lib/server-data';
|
||||
import { loadServerData, saveServerData, SERVER_DATA_CACHE_MS } from '@/lib/server-data';
|
||||
import {
|
||||
makeBugCreatedActivity,
|
||||
makeBugStatusActivity,
|
||||
@@ -15,6 +15,8 @@ function saveStored(items: Bug[]) {
|
||||
saveServerData('bugs', items).catch(() => {});
|
||||
}
|
||||
|
||||
let lastBugsFetchAt = 0;
|
||||
|
||||
async function loadStored(): Promise<Bug[] | null> {
|
||||
try {
|
||||
return await loadServerData<Bug[]>('bugs');
|
||||
@@ -28,7 +30,8 @@ function makeLog(action: BugLog['action'], operator: string, from?: string, to?:
|
||||
|
||||
interface BugState {
|
||||
bugs: Bug[];
|
||||
fetchBugs: () => Promise<void>;
|
||||
loaded: boolean;
|
||||
fetchBugs: (options?: { force?: boolean }) => Promise<void>;
|
||||
createBug: (data: Omit<Bug, 'id' | 'bugNo' | 'createdAt' | 'updatedAt' | 'status' | 'logs'>, operator: string) => Bug;
|
||||
updateBug: (id: string, data: Partial<Bug>) => void;
|
||||
deleteBug: (id: string) => void;
|
||||
@@ -41,10 +44,14 @@ interface BugState {
|
||||
|
||||
export const useBugStore = create<BugState>((set, get) => ({
|
||||
bugs: [],
|
||||
loaded: false,
|
||||
|
||||
fetchBugs: async () => {
|
||||
fetchBugs: async (options) => {
|
||||
if (!options?.force && get().loaded && Date.now() - lastBugsFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
const cached = await loadStored();
|
||||
if (cached) set({ bugs: cached });
|
||||
if (!options?.force && get().loaded && Date.now() - lastBugsFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
lastBugsFetchAt = Date.now();
|
||||
set({ bugs: cached ?? [], loaded: true });
|
||||
},
|
||||
|
||||
createBug: (data, operator) => {
|
||||
@@ -61,7 +68,7 @@ export const useBugStore = create<BugState>((set, get) => ({
|
||||
updatedAt: now,
|
||||
};
|
||||
const updated = [...list, bug];
|
||||
set({ bugs: updated });
|
||||
set({ bugs: updated, loaded: true });
|
||||
saveStored(updated);
|
||||
useWorkActivityStore.getState().addActivity(makeBugCreatedActivity(bug, operator));
|
||||
return bug;
|
||||
@@ -71,13 +78,13 @@ export const useBugStore = create<BugState>((set, get) => ({
|
||||
const updated = get().bugs.map((b) =>
|
||||
b.id === id ? { ...b, ...data, updatedAt: new Date().toISOString() } : b,
|
||||
);
|
||||
set({ bugs: updated });
|
||||
set({ bugs: updated, loaded: true });
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
deleteBug: (id) => {
|
||||
const updated = get().bugs.filter((b) => b.id !== id);
|
||||
set({ bugs: updated });
|
||||
set({ bugs: updated, loaded: true });
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user