'use client'; import Link from 'next/link'; import { useMemo, useState } from 'react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { MapPin, Plus, RefreshCw, Trash2 } from 'lucide-react'; import { toast } from 'sonner'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { EmptyStateWithBorder } from '@/components/layout/empty'; import { ErrorInline } from '@/components/layout/error'; import { LoadingStateWithBorder } from '@/components/layout/loading'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { type OriginItem, OriginService } from '@/lib/services/openflare'; import { formatDateTime } from '@/lib/utils'; import { useTranslations } from 'next-intl'; import { OriginEditorDialog } from './components/origin-editor-dialog'; const originsQueryKey = ['openflare', 'origins'] as const; export default function OriginsPage() { const t = useTranslations('origins'); const tc = useTranslations('common'); const queryClient = useQueryClient(); const [editorOpen, setEditorOpen] = useState(false); const [editingOrigin, setEditingOrigin] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); const originsQuery = useQuery({ queryKey: originsQueryKey, queryFn: () => OriginService.list(), }); const origins = useMemo(() => originsQuery.data ?? [], [originsQuery.data]); const deleteMutation = useMutation({ mutationFn: (id: number) => OriginService.deleteById(id), onSuccess: async () => { toast.success(t('deleted')); setDeleteTarget(null); await queryClient.invalidateQueries({ queryKey: originsQueryKey }); }, onError: (error) => { toast.error(error instanceof Error ? error.message : t('deleteFailed')); }, }); return (

{t('title')}

{t('subtitle')}

{originsQuery.isError ? ( void originsQuery.refetch()} /> ) : null}
{originsQuery.isLoading ? ( ) : origins.length === 0 ? ( ) : (
{origins.map((origin) => (

{origin.name}

0 ? 'text-emerald-600 border-emerald-500/20' : 'text-amber-600 border-amber-500/20' }`} > {t('routeCount', { count: origin.route_count })}

{origin.address}

{origin.remark || t('noRemark')}

{t('lastUpdated', { date: formatDateTime(origin.updated_at), })}

))}
)}
{ if (!open) setDeleteTarget(null); }} > {t('deleteTitle')} {t('deleteDesc', { name: deleteTarget?.name ?? '' })} {tc('cancel')} { if (deleteTarget) deleteMutation.mutate(deleteTarget.id); }} > {t('confirmDelete')}
); }