feat(平台): 补齐服务端持久化和AI拆解契约
This commit is contained in:
@@ -1,25 +1,23 @@
|
||||
'use client';
|
||||
import { create } from 'zustand';
|
||||
import type { TaskCategory, CategoryGroup } from '@/lib/task-category';
|
||||
import { PRESET_CATEGORIES } from '@/lib/task-category';
|
||||
import { PRESET_CATEGORIES, normalizeTaskCategories } from '@/lib/task-category';
|
||||
import { loadServerData, saveServerData } from '@/lib/server-data';
|
||||
|
||||
const STORAGE_KEY = 'ftb_task_categories_v1';
|
||||
|
||||
function saveLocal(items: TaskCategory[]) {
|
||||
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {}
|
||||
function saveStored(items: TaskCategory[]) {
|
||||
saveServerData('task-categories', items).catch(() => {});
|
||||
}
|
||||
|
||||
function loadLocal(): TaskCategory[] | null {
|
||||
async function loadStored(): Promise<TaskCategory[] | null> {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (raw) return JSON.parse(raw);
|
||||
return await loadServerData<TaskCategory[]>('task-categories');
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
interface TaskCategoryState {
|
||||
categories: TaskCategory[];
|
||||
fetchCategories: () => void;
|
||||
fetchCategories: () => Promise<void>;
|
||||
addCategory: (name: string, group: CategoryGroup, color?: string) => void;
|
||||
updateCategory: (id: string, data: Partial<TaskCategory>) => void;
|
||||
deleteCategory: (id: string) => boolean;
|
||||
@@ -28,15 +26,16 @@ interface TaskCategoryState {
|
||||
export const useTaskCategoryStore = create<TaskCategoryState>((set, get) => ({
|
||||
categories: PRESET_CATEGORIES,
|
||||
|
||||
fetchCategories: () => {
|
||||
const cached = loadLocal();
|
||||
if (cached) set({ categories: cached });
|
||||
fetchCategories: async () => {
|
||||
const cached = await loadStored();
|
||||
if (cached) set({ categories: normalizeTaskCategories(cached) });
|
||||
},
|
||||
|
||||
addCategory: (name, group, color) => {
|
||||
const list = get().categories;
|
||||
const item: TaskCategory = {
|
||||
id: `cat-${Date.now()}`,
|
||||
code: name.trim().toLowerCase().replace(/[^a-z0-9\u4e00-\u9fa5]+/g, '_').replace(/^_+|_+$/g, '') || `cat_${Date.now()}`,
|
||||
name,
|
||||
group,
|
||||
color,
|
||||
@@ -45,13 +44,13 @@ export const useTaskCategoryStore = create<TaskCategoryState>((set, get) => ({
|
||||
};
|
||||
const updated = [...list, item];
|
||||
set({ categories: updated });
|
||||
saveLocal(updated);
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
updateCategory: (id, data) => {
|
||||
const updated = get().categories.map((c) => (c.id === id ? { ...c, ...data } : c));
|
||||
set({ categories: updated });
|
||||
saveLocal(updated);
|
||||
saveStored(updated);
|
||||
},
|
||||
|
||||
deleteCategory: (id) => {
|
||||
@@ -59,7 +58,7 @@ export const useTaskCategoryStore = create<TaskCategoryState>((set, get) => ({
|
||||
if (!target || target.isSystem) return false;
|
||||
const updated = get().categories.filter((c) => c.id !== id);
|
||||
set({ categories: updated });
|
||||
saveLocal(updated);
|
||||
saveStored(updated);
|
||||
return true;
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user