'use client'; import { Bookmark, BookmarkCheck } from 'lucide-react'; import { useState } from 'react'; import { ownerApi } from '@/lib/client-api'; import { cn } from '@/lib/cn'; import { ensureOwnerToken, readWatched, useWatched, writeWatched } from '@/lib/owner'; /** Watch / unwatch a company (owner token generated on first use). Optimistic; mirrors the slug locally. */ export function WatchButton({ slug, name, className, size = 'md' }: { slug: string; name?: string; className?: string; size?: 'sm' | 'md' }) { const watched = useWatched(); const on = watched.includes(slug); const [busy, setBusy] = useState(false); const [err, setErr] = useState(null); const toggle = async () => { setBusy(true); setErr(null); const token = ensureOwnerToken(); const api = ownerApi(token); const cur = readWatched(); try { if (on) { writeWatched(cur.filter((s) => s !== slug)); await api.unwatch(slug); } else { writeWatched([...cur, slug]); await api.watch(slug); } } catch (e) { writeWatched(cur); setErr((e as Error).message || 'Could not update watchlist'); } finally { setBusy(false); } }; return ( {err && {err}} ); }