"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { addManualTimeEntry, deleteTimeEntry } from "@/app/admin/timer-actions"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; type TimeEntry = { id: string; started_at: Date; ended_at: Date | null; duration_seconds: number | null; }; function formatDuration(seconds: number | null): string { if (!seconds) return "0 min"; const h = Math.floor(seconds / 3600); const m = Math.floor((seconds % 3600) / 60); if (h > 0) return `${h}h ${m}min`; return `${m} min`; } function formatDate(date: Date): string { return new Date(date).toLocaleDateString("it-IT", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", }); } type Props = { projectId: string; recentEntries: TimeEntry[]; }; export function ManualTimeEntry({ projectId, recentEntries }: Props) { const router = useRouter(); const [, startTransition] = useTransition(); const [minutes, setMinutes] = useState(""); const [date, setDate] = useState(() => new Date().toISOString().split("T")[0]); const [loading, setLoading] = useState(false); const [deletingId, setDeletingId] = useState(null); async function handleQuickAdd(mins: number) { setLoading(true); try { const startedAt = new Date(`${date}T09:00:00`).toISOString(); await addManualTimeEntry(projectId, mins, startedAt); startTransition(() => router.refresh()); } finally { setLoading(false); } } async function handleCustomAdd(e: React.FormEvent) { e.preventDefault(); const mins = parseFloat(minutes); if (!mins || mins <= 0) return; setLoading(true); try { const startedAt = new Date(`${date}T09:00:00`).toISOString(); await addManualTimeEntry(projectId, mins, startedAt); setMinutes(""); startTransition(() => router.refresh()); } finally { setLoading(false); } } async function handleDelete(entryId: string) { setDeletingId(entryId); try { await deleteTimeEntry(entryId, projectId); startTransition(() => router.refresh()); } finally { setDeletingId(null); } } return (

Aggiungi tempo manuale

{/* Data */}
setDate(e.target.value)} className="max-w-[180px]" />
{/* Quick buttons */}

Aggiunte rapide

{/* Custom minutes */}
setMinutes(e.target.value)} className="max-w-[120px]" />
{/* Recent entries */} {recentEntries.length > 0 && (

Ultime registrazioni

    {recentEntries.map((entry) => (
  • {formatDuration(entry.duration_seconds)}

    {formatDate(entry.started_at)}

  • ))}
)}
); }