import React, { useRef } 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'; 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 inputRef = useRef(null); const [currentId, setCurrentId] = React.useState(null); const upload = useUploadWithProgress(currentId || ''); const navigate = useNavigate(); const [isAddOpen, setIsAddOpen] = React.useState(false); 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 vehicleLabel = doc.vehicleId ? `${doc.vehicleId.slice(0, 8)}...` : '—'; return (
{doc.title}
{doc.documentType} • {vehicleLabel}
{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;