import React, { useRef, useMemo } from 'react'; import { useAuth0 } from '@auth0/auth0-react'; import { isAxiosError } from 'axios'; import { useNavigate } from 'react-router-dom'; import { GlassCard } from '../../../shared-minimal/components/mobile/GlassCard'; import { useDocumentsList } from '../hooks/useDocuments'; import { useUploadWithProgress } from '../hooks/useUploadWithProgress'; import { Button } from '../../../shared-minimal/components/Button'; import { AddDocumentDialog } from '../components/AddDocumentDialog'; import { ExpirationBadge } from '../components/ExpirationBadge'; import { DocumentCardMetadata } from '../components/DocumentCardMetadata'; import { useVehicles } from '../../vehicles/hooks/useVehicles'; import { getVehicleLabel } from '@/core/utils/vehicleDisplay'; import PictureAsPdfRoundedIcon from '@mui/icons-material/PictureAsPdfRounded'; import ImageRoundedIcon from '@mui/icons-material/ImageRounded'; import InsertDriveFileRoundedIcon from '@mui/icons-material/InsertDriveFileRounded'; import dayjs from 'dayjs'; import relativeTime from 'dayjs/plugin/relativeTime'; dayjs.extend(relativeTime); export const DocumentsMobileScreen: React.FC = () => { console.log('[DocumentsMobileScreen] Component initializing'); // Auth is managed at App level; keep hook to support session-expired UI. const auth = useAuth0(); const { isAuthenticated, isLoading: authLoading, loginWithRedirect } = auth; // Data hooks (unconditional per React rules) const { data, isLoading, error } = useDocumentsList(); const { data: vehicles } = useVehicles(); const inputRef = useRef(null); const [currentId, setCurrentId] = React.useState(null); const upload = useUploadWithProgress(currentId || ''); const navigate = useNavigate(); const [isAddOpen, setIsAddOpen] = React.useState(false); const vehiclesMap = useMemo(() => new Map(vehicles?.map(v => [v.id, v]) || []), [vehicles]); const getFileTypeIcon = (contentType: string | null | undefined) => { if (!contentType) return ; if (contentType === 'application/pdf') return ; if (contentType.startsWith('image/')) return ; return ; }; const triggerUpload = (docId: string) => { try { setCurrentId(docId); if (!inputRef.current) return; inputRef.current.value = ''; inputRef.current.click(); } catch (error) { console.error('[Documents Mobile] Upload trigger error:', error); } }; const onFileChange = () => { try { const file = inputRef.current?.files?.[0]; if (file && currentId) { const allowed = new Set(['application/pdf', 'image/jpeg', 'image/png']); if (!file.type || !allowed.has(file.type)) { alert('Unsupported file type. Allowed: PDF, JPG/JPEG, PNG.'); return; } upload.mutate(file); } } catch (error) { console.error('[Documents Mobile] File change error:', error); } }; // Show loading while auth is initializing if (authLoading) { return (

Documents

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

Login Required

Please log in to view your documents

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

Session Expired

Your session has expired. Please log in again.

); } return (
setIsAddOpen(false)} />

Documents

{isLoading &&
Loading...
} {hasError && !isAuthError && (

Failed to load documents

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

No documents yet

Documents will appear here once you create them

)} {!isLoading && !hasError && data && data.length > 0 && (
{data.map((doc) => { const vehicle = vehiclesMap.get(doc.vehicleId); const vehicleLabel = getVehicleLabel(vehicle); const isShared = doc.sharedVehicleIds.length > 0; return (
{doc.title}
{getFileTypeIcon(doc.contentType)} {doc.documentType} {doc.createdAt && ` \u00B7 ${dayjs(doc.createdAt).fromNow()}`} {isShared && ' \u00B7 Shared'}
{upload.isPending && currentId === doc.id && ( {upload.progress}% )} {upload.isError && currentId === doc.id && ( {((upload.error as any)?.response?.status === 415) ? 'Unsupported file type. Use PDF, JPG/JPEG, PNG.' : 'Upload failed'} )}
);})}
)}
); }; export default DocumentsMobileScreen;