perf(web): 优化大数据量页面切换与聚合性能

This commit is contained in:
Script Generator
2026-07-03 09:43:36 +08:00
parent 554ab520d1
commit a509bb4922
32 changed files with 1087 additions and 360 deletions

View File

@@ -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);
},