'use client'; import { create } from 'zustand'; import type { TaskCategory, CategoryGroup } from '@/lib/task-category'; import { PRESET_CATEGORIES } from '@/lib/task-category'; const STORAGE_KEY = 'ftb_task_categories_v1'; function saveLocal(items: TaskCategory[]) { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {} } function loadLocal(): TaskCategory[] | null { try { const raw = localStorage.getItem(STORAGE_KEY); if (raw) return JSON.parse(raw); } catch {} return null; } interface TaskCategoryState { categories: TaskCategory[]; fetchCategories: () => void; addCategory: (name: string, group: CategoryGroup, color?: string) => void; updateCategory: (id: string, data: Partial) => void; deleteCategory: (id: string) => boolean; } export const useTaskCategoryStore = create((set, get) => ({ categories: PRESET_CATEGORIES, fetchCategories: () => { const cached = loadLocal(); if (cached) set({ categories: cached }); }, addCategory: (name, group, color) => { const list = get().categories; const item: TaskCategory = { id: `cat-${Date.now()}`, name, group, color, sortOrder: list.length + 1, isSystem: false, }; const updated = [...list, item]; set({ categories: updated }); saveLocal(updated); }, updateCategory: (id, data) => { const updated = get().categories.map((c) => (c.id === id ? { ...c, ...data } : c)); set({ categories: updated }); saveLocal(updated); }, deleteCategory: (id) => { const target = get().categories.find((c) => c.id === id); if (!target || target.isSystem) return false; const updated = get().categories.filter((c) => c.id !== id); set({ categories: updated }); saveLocal(updated); return true; }, }));