"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useT } from "@/lib/i18n"; export function BrandTrackerActions({ brandPageId, trackingId }: { brandPageId: string; trackingId?: string }) { const router = useRouter(); const t = useT(); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); async function toggle() { setBusy(true); setError(""); try { const response = await fetch(trackingId ? `/api/tracked-brands/${trackingId}` : "/api/tracked-brands", { method: trackingId ? "DELETE" : "POST", headers: trackingId ? undefined : { "content-type": "application/json" }, body: trackingId ? undefined : JSON.stringify({ brandPageId }) }); const data = await response.json().catch(() => ({})); if (!response.ok) throw new Error(data.error || "BRAND_TRACKER_FAILED"); router.refresh(); } catch (caught) { setError(caught instanceof Error ? caught.message : t("common.error")); } finally { setBusy(false); } } return (
{error &&
{error}
}
); }