"use client"; import { useParams } from "next/navigation"; import Link from "next/link"; import { useMemo } from "react"; import useSWR from "swr"; import { Car, Wrench, FileText, Clock, Printer, ArrowLeft } from "lucide-react"; import { HistoryData } from "@/types"; import { formatDate } from "@/lib/format"; import Loading from "@/components/Loading"; const fetcher = (url: string) => fetch(url).then((r) => r.json()); export default function CarHistoryPrintPage() { const { id } = useParams(); const { data: history } = useSWR(`/api/cars/${id}/history`, fetcher); const grouped = useMemo(() => { if (!history) return []; const map = new Map(); for (const task of history.tasks) { const key = task.title.trim(); if (!map.has(key)) map.set(key, []); map.get(key)!.push(task); } return Array.from(map.entries()) .map(([title, tasks]) => ({ title, tasks, count: tasks.length })) .sort((a, b) => a.title.localeCompare(b.title)); }, [history]); if (!history) return ; return (
Back to history

{history.car.regNumber} — Service history

Service tasks

{history.tasks.length === 0 ? (

No service history yet.

) : (
{grouped.map((group) => (

{group.title}{" "} ({group.count})

    {group.tasks.map((task) => (
  • {formatDate(task.finishedAt || task.createdAt)} Order #{task.orderId} {task.orderMileage != null && — {task.orderMileage.toLocaleString()} mi} {task.notes && — {task.notes}}
  • ))}
))}
)}
Printed from Andris Car Service on {new Date().toLocaleDateString("en-IE")}.
); } function Info({ label, value }: { label: string; value?: string | null }) { return (
{label}
{value || "—"}
); }