import { useCallback, useEffect, useState } from 'react' import { get, set } from 'idb-keyval' const KEY = 'careless-bookmarks' export interface Bookmark { id: string threadId: string | null messageId: string text: string role: 'user' | 'assistant' | 'system' timestamp: number note?: string } export function useBookmarks() { const [bookmarks, setBookmarks] = useState([]) useEffect(() => { get(KEY).then(b => setBookmarks(b || [])) }, []) const add = useCallback(async (b: Omit) => { const item: Bookmark = { ...b, id: 'bm-' + Math.random().toString(36).slice(2, 10), timestamp: Date.now() } const next = [...bookmarks, item] await set(KEY, next) setBookmarks(next) return item }, [bookmarks]) const remove = useCallback(async (id: string) => { const next = bookmarks.filter(b => b.id !== id) await set(KEY, next) setBookmarks(next) }, [bookmarks]) return { bookmarks, add, remove } }