import { useCallback, useState, useRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import type { YjsState } from "~/lib/use-yjs"; import { buildDayGpxFiles, buildPlanGpx, buildRouteGpx, hasDayBreaks, } from "~/lib/gpx-export"; function download(gpx: string, filename: string) { const blob = new Blob([gpx], { type: "application/gpx+xml" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; a.click(); URL.revokeObjectURL(url); } export function ExportButton({ yjs }: { yjs: YjsState }) { const { t } = useTranslation("planner"); const [open, setOpen] = useState(false); const ref = useRef(null); // Close dropdown on outside mousedown useEffect(() => { if (!open) return; const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); }, [open]); const handleExportRoute = useCallback(() => { download(buildRouteGpx(yjs), "route.gpx"); setOpen(false); }, [yjs]); const handleExportPlan = useCallback(() => { download(buildPlanGpx(yjs), "route-plan.gpx"); setOpen(false); }, [yjs]); const handleExportDays = useCallback(() => { for (const file of buildDayGpxFiles(yjs)) { download(file.gpx, file.filename); } setOpen(false); }, [yjs]); const hasMultipleDays = hasDayBreaks(yjs); const item = "block w-full px-3 py-1.5 text-left transition-colors hover:bg-bg-subtle"; const split = "h-7 border border-border bg-bg-raised text-sm font-medium text-text-hi transition-colors hover:bg-bg-subtle focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"; return (
{open && (
{hasMultipleDays && ( )}
)}
); }