|
| 1 | +import { useState, useEffect, useCallback } from "react"; |
| 2 | + |
| 3 | +interface Tag { |
| 4 | + id: string; |
| 5 | + name: string; |
| 6 | + color: string; |
| 7 | +} |
| 8 | + |
| 9 | +const DEFAULT_COLORS = [ |
| 10 | + "#6366f1", "#ec4899", "#f59e0b", "#22c55e", "#06b6d4", |
| 11 | + "#8b5cf6", "#ef4444", "#14b8a6", "#f97316", "#64748b", |
| 12 | +]; |
| 13 | + |
| 14 | +export function useTags() { |
| 15 | + const [tags, setTags] = useState<Tag[]>(() => { |
| 16 | + const saved = localStorage.getItem("todo-tags"); |
| 17 | + return saved ? JSON.parse(saved) : [ |
| 18 | + { id: "1", name: "urgent", color: "#ef4444" }, |
| 19 | + { id: "2", name: "important", color: "#f59e0b" }, |
| 20 | + { id: "3", name: "later", color: "#64748b" }, |
| 21 | + ]; |
| 22 | + }); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + localStorage.setItem("todo-tags", JSON.stringify(tags)); |
| 26 | + }, [tags]); |
| 27 | + |
| 28 | + const addTag = useCallback((name: string) => { |
| 29 | + const color = DEFAULT_COLORS[tags.length % DEFAULT_COLORS.length]; |
| 30 | + setTags(prev => [...prev, { id: crypto.randomUUID(), name, color }]); |
| 31 | + }, [tags.length]); |
| 32 | + |
| 33 | + const removeTag = useCallback((id: string) => { |
| 34 | + setTags(prev => prev.filter(t => t.id !== id)); |
| 35 | + }, []); |
| 36 | + |
| 37 | + const updateTag = useCallback((id: string, updates: Partial<Tag>) => { |
| 38 | + setTags(prev => prev.map(t => t.id === id ? { ...t, ...updates } : t)); |
| 39 | + }, []); |
| 40 | + |
| 41 | + return { tags, addTag, removeTag, updateTag }; |
| 42 | +} |
0 commit comments