feat(Bug): 状态流转接入单条工作流

This commit is contained in:
Script Generator
2026-06-25 14:47:28 +08:00
parent c617625a99
commit 125057ea55
3 changed files with 142 additions and 25 deletions

View File

@@ -1,18 +1,17 @@
'use client';
import { create } from 'zustand';
import type { Bug, BugStatus, BugLog } from '@/lib/bug';
import { canBugTransition, generateBugNo } from '@/lib/bug';
import { generateBugNo } from '@/lib/bug';
import { applyBugTransition } from '@/lib/bug-workflow';
import { loadServerData, saveServerData } from '@/lib/server-data';
const STORAGE_KEY = 'ftb_bugs_v1';
function saveLocal(items: Bug[]) {
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {}
function saveStored(items: Bug[]) {
saveServerData('bugs', items).catch(() => {});
}
function loadLocal(): Bug[] | null {
async function loadStored(): Promise<Bug[] | null> {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) return JSON.parse(raw);
return await loadServerData<Bug[]>('bugs');
} catch {}
return null;
}
@@ -23,7 +22,7 @@ function makeLog(action: BugLog['action'], operator: string, from?: string, to?:
interface BugState {
bugs: Bug[];
fetchBugs: () => void;
fetchBugs: () => 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;
@@ -37,8 +36,8 @@ interface BugState {
export const useBugStore = create<BugState>((set, get) => ({
bugs: [],
fetchBugs: () => {
const cached = loadLocal();
fetchBugs: async () => {
const cached = await loadStored();
if (cached) set({ bugs: cached });
},
@@ -57,7 +56,7 @@ export const useBugStore = create<BugState>((set, get) => ({
};
const updated = [...list, bug];
set({ bugs: updated });
saveLocal(updated);
saveStored(updated);
return bug;
},
@@ -66,29 +65,24 @@ export const useBugStore = create<BugState>((set, get) => ({
b.id === id ? { ...b, ...data, updatedAt: new Date().toISOString() } : b,
);
set({ bugs: updated });
saveLocal(updated);
saveStored(updated);
},
deleteBug: (id) => {
const updated = get().bugs.filter((b) => b.id !== id);
set({ bugs: updated });
saveLocal(updated);
saveStored(updated);
},
changeStatus: (id, to, operator, extra) => {
const bug = get().bugs.find((b) => b.id === id);
if (!bug) return { ok: false, message: 'Bug不存在' };
if (!canBugTransition(bug.status, to)) {
return { ok: false, message: `不允许从「${bug.status}」流转到「${to}` };
}
const now = new Date().toISOString();
const patch: Partial<Bug> = { status: to };
if (to === 'fixed') patch.resolvedAt = now;
if (to === 'closed') patch.closedAt = now;
if (extra?.resolution) patch.resolution = extra.resolution;
const log = makeLog(to === 'fixed' ? 'resolve' : 'status_change', operator, bug.status, to, extra?.resolution);
patch.logs = [...(bug.logs || []), log];
get().updateBug(id, patch);
const result = applyBugTransition(bug, to, operator, {
now: new Date(),
resolution: extra?.resolution,
});
if (!result.ok || !result.patch) return { ok: false, message: result.message };
get().updateBug(id, result.patch);
return { ok: true };
},