import { Ban, FileDown, Loader2 } from 'lucide-react'; import { useRef, useState, type DragEvent, type ReactNode } from 'react'; import { useTranslation } from '../../i18n'; import { fs } from '../shared/DialogShell'; import TourGpxPreviewDialog from './TourGpxPreviewDialog'; import type { TourGpxImport } from './useTourGpxImport'; /** Only a drag of files from the desktop; a waypoint or a tour row being dragged is not one. */ const carriesFiles = (event: DragEvent) => Array.from(event.dataTransfer.types).includes('Files'); /** * The tours workspace as a drop target for GPX files: a file dragged over it * dims the workspace and says what will happen, and a drop hands the files to * the same import the button runs. While an import runs, the overlay stays and * says so. A drag of anything else (a waypoint in the list, the map) passes * straight through. */ export default function TourGpxDropZone({ gpxImport, canEdit, className = 'relative', children, }: { gpxImport: TourGpxImport; canEdit: boolean; /** Positions the zone; it must be positioned for the overlay to cover it. */ className?: string; children: ReactNode; }) { const { t } = useTranslation(); const [over, setOver] = useState(false); // dragenter and dragleave fire for every child the pointer crosses, so the // overlay goes only when the count of entered elements is back at zero. const depth = useRef(0); const reset = () => { depth.current = 0; setOver(false); }; return (
{ if (!carriesFiles(event)) return; event.preventDefault(); depth.current += 1; setOver(true); }} onDragOver={(event) => { if (!carriesFiles(event)) return; event.preventDefault(); event.dataTransfer.dropEffect = canEdit && !gpxImport.importing ? 'copy' : 'none'; }} onDragLeave={(event) => { if (!carriesFiles(event)) return; depth.current = Math.max(0, depth.current - 1); if (depth.current === 0) setOver(false); }} onDrop={(event) => { if (!carriesFiles(event)) return; event.preventDefault(); reset(); if (canEdit) void gpxImport.importFiles(Array.from(event.dataTransfer.files)); }} > {children} {(over || gpxImport.importing) && (
{gpxImport.importing ? ( ) : canEdit ? ( ) : ( )} {gpxImport.importing ? t('tours.import.importing') : canEdit ? t('tours.import.drop') : t('tours.import.dropNoPermission')} {!gpxImport.importing && canEdit && ( {t('tours.import.dropHint')} )}
)}
); }