"use client"; import { useAppRouter } from "@/hooks/use-app-router"; import { useState, useTransition } from "react"; import { RefreshCw } from "lucide-react"; import { Button } from "@/components/ui/button"; type SyncPayload = { ok?: boolean; error?: string; data?: { total: number; inserted: number; updated: number; imagesSaved: number; }; }; export function FacultySyncButton() { const router = useAppRouter(); const [pending, startTransition] = useTransition(); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(null); const [error, setError] = useState(null); async function onSync() { setBusy(true); setMessage(null); setError(null); try { const res = await fetch("/api/admin/faculty/sync", { method: "POST" }); const body = (await res.json()) as SyncPayload; if (!res.ok || !body.ok || !body.data) { setError(body.error ?? "همگام سازی ناموفق بود"); return; } const { total, inserted, updated, imagesSaved } = body.data; setMessage( `${total} استاد خوانده شد — ${inserted} جدید، ${updated} به روز` + (imagesSaved > 0 ? `، ${imagesSaved} تصویر` : ""), ); startTransition(() => { router.refresh(); }); } catch { setError("ارتباط با سرور برقرار نشد"); } finally { setBusy(false); } } const loading = busy || pending; return (
{message ? (

{message}

) : null} {error ? (

{error}

) : null}
); }