'use client'; import { useTransition } from 'react'; import { useRouter } from 'next/navigation'; import { useTranslations } from 'next-intl'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@kit/ui/alert-dialog'; import { Button } from '@kit/ui/button'; import { toast } from '@kit/ui/sonner'; interface PublishToggleButtonProps { pageId: string; accountId: string; isPublished: boolean; } export function PublishToggleButton({ pageId, accountId, isPublished, }: PublishToggleButtonProps) { const router = useRouter(); const t = useTranslations('siteBuilder'); const [isPending, startTransition] = useTransition(); const handleToggle = () => { startTransition(async () => { try { const response = await fetch( `/api/site-builder/pages/${pageId}/publish`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ accountId, isPublished: !isPublished }), }, ); if (!response.ok) { toast.error(t('pages.toggleError')); return; } router.refresh(); } catch (error) { console.error('Failed to toggle publish state:', error); toast.error(t('pages.toggleError')); } }); }; return ( {isPublished ? t('pages.hide') : t('pages.publish')} } /> {isPublished ? t('pages.hideTitle') : t('pages.publishTitle')} {isPublished ? t('pages.hideDesc') : t('pages.publishDesc')} {t('pages.cancelAction')} {isPublished ? t('pages.hide') : t('pages.publish')} ); }