'use client'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { ArrowPathIcon, CheckIcon } from '@heroicons/react/24/outline'; import { createOrUpdateSnapshot } from '@/app/(authenticated)/shared/lib/actions'; import { useRouter } from 'next/navigation'; import { useTranslations } from 'next-intl'; interface UpdateSnapshotButtonProps { shareId: string; snapshotUpdatedAt?: Date | string | null; variant?: 'header' | 'table'; } export default function UpdateSnapshotButton({ shareId, snapshotUpdatedAt, variant = 'header', }: UpdateSnapshotButtonProps) { const t = useTranslations('UpdateSnapshot'); const router = useRouter(); const [loading, setLoading] = useState(false); const [done, setDone] = useState(false); const handleUpdate = async () => { setLoading(true); try { await createOrUpdateSnapshot(shareId); setDone(true); router.refresh(); setTimeout(() => setDone(false), 2500); } catch (e) { console.error('Failed to update snapshot:', e); } finally { setLoading(false); } }; if (variant === 'table') { return ( ); } return (
{snapshotUpdatedAt && ( {t('lastUpdated', { date: new Intl.DateTimeFormat(undefined, { dateStyle: 'short', timeStyle: 'short' }).format( new Date(snapshotUpdatedAt), ), })} )}
); }