'use client'; import { useEffect, useState } from 'react'; import { Bookmark, BookmarkCheck } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { useBookmarks } from '@/lib/navigation'; import { toast } from 'sonner'; interface Props { readonly type: 'section' | 'judgment' | 'law'; readonly id: number; readonly label: string; readonly url: string; } export function BookmarkButton({ type, id, label, url }: Props) { const { isBookmarked, addBookmark, removeBookmark } = useBookmarks(); const [bookmarked, setBookmarked] = useState(() => isBookmarked(type, id)); useEffect(() => { const handler = () => setBookmarked(isBookmarked(type, id)); window.addEventListener('bookmarks-changed', handler); return () => window.removeEventListener('bookmarks-changed', handler); }, [type, id, isBookmarked]); const toggle = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); if (bookmarked) { removeBookmark(type, id); setBookmarked(false); toast.success('นำออกจากบันทึกแล้ว'); } else { addBookmark({ type, id, label, url }); setBookmarked(true); toast.success('บันทึกแล้ว'); } }; return ( ); }