feat(平台): 补齐服务端持久化和AI拆解契约
This commit is contained in:
@@ -1,24 +1,22 @@
|
||||
'use client';
|
||||
import { create } from 'zustand';
|
||||
import type { TaskWorklog } from '@/lib/task-worklog';
|
||||
import { loadServerData, saveServerData } from '@/lib/server-data';
|
||||
|
||||
const STORAGE_KEY = 'ftb_task_worklogs_v1';
|
||||
|
||||
function saveLocal(items: TaskWorklog[]) {
|
||||
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {}
|
||||
function saveStored(items: TaskWorklog[]) {
|
||||
saveServerData('task-worklogs', items).catch(() => {});
|
||||
}
|
||||
|
||||
function loadLocal(): TaskWorklog[] | null {
|
||||
async function loadStored(): Promise<TaskWorklog[] | null> {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (raw) return JSON.parse(raw);
|
||||
return await loadServerData<TaskWorklog[]>('task-worklogs');
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
interface TaskWorklogState {
|
||||
worklogs: TaskWorklog[];
|
||||
fetchWorklogs: () => void;
|
||||
fetchWorklogs: () => Promise<void>;
|
||||
addWorklog: (data: Omit<TaskWorklog, 'id' | 'createdAt'>) => void;
|
||||
deleteWorklog: (id: string) => void;
|
||||
getActualHours: (taskId: string) => number;
|
||||
@@ -27,8 +25,8 @@ interface TaskWorklogState {
|
||||
export const useTaskWorklogStore = create<TaskWorklogState>((set, get) => ({
|
||||
worklogs: [],
|
||||
|
||||
fetchWorklogs: () => {
|
||||
const cached = loadLocal();
|
||||
fetchWorklogs: async () => {
|
||||
const cached = await loadStored();
|
||||
if (cached) set({ worklogs: cached });
|
||||
},
|
||||
|
||||
@@ -40,13 +38,13 @@ export const useTaskWorklogStore = create<TaskWorklogState>((set, get) => ({
|
||||
};
|
||||
const updated = [...get().worklogs, item];
|
||||
set({ worklogs: updated });
|
||||
saveLocal(updated);
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
deleteWorklog: (id) => {
|
||||
const updated = get().worklogs.filter((w) => w.id !== id);
|
||||
set({ worklogs: updated });
|
||||
saveLocal(updated);
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
getActualHours: (taskId) => {
|
||||
|
||||
Reference in New Issue
Block a user