import React, { useMemo } from 'react'; import { useAuth0 } from '@auth0/auth0-react'; import { useDocumentsList, useDeleteDocument } from '../hooks/useDocuments'; import { Card } from '../../../shared-minimal/components/Card'; import { Button } from '../../../shared-minimal/components/Button'; import { useNavigate } from 'react-router-dom'; import { AddDocumentDialog } from '../components/AddDocumentDialog'; import { ExpirationBadge } from '../components/ExpirationBadge'; import { DocumentCardMetadata } from '../components/DocumentCardMetadata'; import { useVehicles } from '../../vehicles/hooks/useVehicles'; import { getVehicleLabel } from '../utils/vehicleLabel'; export const DocumentsPage: React.FC = () => { const { isAuthenticated, isLoading: authLoading, loginWithRedirect } = useAuth0(); const { data, isLoading, error } = useDocumentsList(); const { data: vehicles } = useVehicles(); const navigate = useNavigate(); const removeDoc = useDeleteDocument(); const [isAddOpen, setIsAddOpen] = React.useState(false); const vehiclesMap = useMemo(() => new Map(vehicles?.map(v => [v.id, v]) || []), [vehicles]); // Show loading while auth is initializing if (authLoading) { return (

Documents

Checking authentication...
); } // Show login prompt when not authenticated if (!isAuthenticated) { return (

Documents

Authentication Required

Please log in to view your documents

); } // Check for authentication error (401) const isAuthError = error && (error as any).response?.status === 401; if (isAuthError) { return (

Documents

Session Expired

Your session has expired. Please log in again to continue.

); } return (

Documents

setIsAddOpen(false)} /> {isLoading && (
Loading documents...
)} {error && !isAuthError && (

Error Loading Documents

Failed to load documents. Please try again.

)} {!isLoading && !error && data && data.length === 0 && (

No Documents Yet

You haven't added any documents yet. Documents will appear here once you create them.

)} {!isLoading && !error && data && data.length > 0 && (
{data.map((doc) => { const vehicle = vehiclesMap.get(doc.vehicleId); const vehicleLabel = getVehicleLabel(vehicle); return (
{doc.title}
Type: {doc.documentType}
Vehicle:
{doc.sharedVehicleIds.length > 0 && (
Shared with {doc.sharedVehicleIds.length} other vehicle{doc.sharedVehicleIds.length > 1 ? 's' : ''}
)}
); })}
)}
); }; export default DocumentsPage;