import { Modal, vehicleStore } from "@shared"; import { Box, Button, CircularProgress, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Typography, } from "@mui/material"; import { observer } from "mobx-react-lite"; import { useEffect } from "react"; interface VehicleSessionsModalProps { open: boolean; vehicleId: number | null; tailNumber?: string | null; onClose: () => void; } const formatDateTime = (value?: string | null) => { if (!value) return "-"; const date = new Date(value); if (Number.isNaN(date.getTime())) return value; return new Intl.DateTimeFormat("ru-RU", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false, }).format(date); }; const formatDuration = (durationSeconds?: number | null) => { if (durationSeconds == null || !Number.isFinite(durationSeconds) || durationSeconds < 0) { return "-"; } const hours = Math.floor(durationSeconds / 3600); const minutes = Math.floor((durationSeconds % 3600) / 60); const seconds = durationSeconds % 60; return [hours, minutes, seconds] .map((part) => String(part).padStart(2, "0")) .join(":"); }; export const VehicleSessionsModal = observer( ({ open, vehicleId, tailNumber, onClose }: VehicleSessionsModalProps) => { const { vehicleSessions, vehicleSessionsLoading, vehicleSessionsError, getVehicleSessions, } = vehicleStore; useEffect(() => { if (!open || vehicleId == null) return; getVehicleSessions(vehicleId).catch(() => undefined); }, [open, vehicleId, getVehicleSessions]); const title = tailNumber && tailNumber !== "" ? `Сессии ТО: ${tailNumber}` : "Сессии ТО"; return (
{title} {vehicleSessionsLoading && ( )} {!vehicleSessionsLoading && vehicleSessionsError && ( {vehicleSessionsError} )} {!vehicleSessionsLoading && !vehicleSessionsError && vehicleSessions && vehicleSessions.length === 0 && ( По этому транспорту нет сессий ТО. )} {!vehicleSessionsLoading && !vehicleSessionsError && vehicleSessions && vehicleSessions.length > 0 && ( ID Начало Окончание Длительность {vehicleSessions.map((session) => ( {session.id} {formatDateTime(session.started_at)} {session.ended_at ? formatDateTime(session.ended_at) : "В процессе"} {session.ended_at ? formatDuration(session.duration_seconds) : "—"} ))}
)}
); }, );