"use client"; import React, { useRef, useState } from "react"; import Button from "@/app/ui/Button"; /** * File upload tab for importing `.ics` calendar files. * * Supports both file picker selection and drag-and-drop. * Sends the file content to `/api/calendar/parse` for server-side ICS parsing. */ type FileTabProps = { onLoadCalendar: (file: File) => Promise; loading: boolean; error: string | null; className?: string; }; const FileTab: React.FC = ({ onLoadCalendar, loading, error, className = "" }) => { const fileInputRef = useRef(null); const [dragging, setDragging] = useState(false); const handleFileSelect = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { handleFileUpload(file); } }; const handleFileUpload = async (file: File) => { try { await onLoadCalendar(file); if (fileInputRef.current) { fileInputRef.current.value = ""; } } catch (err) { console.error("Error uploading file:", err); } }; const handleDragEnter = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragging(true); }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragging(false); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragging(true); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragging(false); const files = e.dataTransfer.files; if (files.length > 0) { handleFileUpload(files[0]); } }; return (
fileInputRef.current?.click()} > {loading ? <>Loading calendar... : <>Click to upload or drag and drop an .ics file here}
{error &&
{error}
}
); }; export default FileTab;