Files
var-monorepo/apps/hub/app/(app)/_components/RecentFlights.tsx
2025-05-30 19:28:07 -07:00

81 lines
2.5 KiB
TypeScript

"use client";
import { Mission, MissionAlertLog, MissionLog, Station } from "@repo/db";
import { ColumnDef } from "@tanstack/react-table";
import { PaginatedTable } from "_components/PaginatedTable";
import { ArrowRight, NotebookText } from "lucide-react";
import { useSession } from "next-auth/react";
import Link from "next/link";
export const RecentFlights = () => {
const session = useSession();
return (
<div className="card-body">
<h2 className="card-title justify-between">
<span className="card-title">
<NotebookText className="w-4 h-4" /> Logbook
</span>
<Link className="badge badge-sm badge-info badge-outline" href="/logbook">
Zum vollständigen Logbook <ArrowRight className="w-4 h-4" />
</Link>
</h2>
<PaginatedTable
prismaModel={"missionOnStationUsers"}
filter={{
userId: session.data?.user?.id ?? "",
Mission: {
state: "finished",
},
}}
include={{
Station: true,
User: true,
Mission: true,
}}
columns={
[
{
header: "Station",
accessorKey: "Station.name",
cell: ({ row }) => row.original.Station.bosCallsign,
},
{
header: "Datum",
accessorKey: "createdAt",
cell: ({ row }) => new Date(row.original.Mission.createdAt).toLocaleDateString(),
},
{
header: "Einsatzlänge",
cell: ({ row }) => {
const missionStartLogs = row.original.Mission.missionLog.filter(
(log) => (log as unknown as MissionLog).type === "alert-log",
) as unknown as MissionAlertLog[] | undefined;
// Find the first log with a station ID that matches the current row's station ID or use the first log if none match
const missionStartLog =
missionStartLogs?.find(
(log) => log.data.station?.id === row.original.Station.id,
) || missionStartLogs?.[0];
const missionEndLog = row.original.Mission.missionLog.find(
(log) => (log as unknown as MissionLog).type === "completed-log",
) as unknown as MissionAlertLog | undefined;
if (!missionStartLog) return "Unbekannt";
if (!missionEndLog) return "Unbekannt";
const start = new Date(missionStartLog.timeStamp);
const end = new Date(missionEndLog?.timeStamp);
const duration = Math.round((end.getTime() - start.getTime()) / 1000 / 60); // in minutes
return `${duration} Minuten`;
},
},
] as ColumnDef<{
Station: Station;
Mission: Mission;
}>[]
}
/>
</div>
);
};