diff --git a/src/components/notes/note-import-panel.tsx b/src/components/notes/note-import-panel.tsx new file mode 100644 index 0000000..96e1d38 --- /dev/null +++ b/src/components/notes/note-import-panel.tsx @@ -0,0 +1,199 @@ +import { Show, createSignal } from "solid-js"; +import { Button } from "@/components/ui/button"; +import { IconFileText, IconUploadCloud } from "@/components/ui/icons"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { processImportedText } from "@/lib/note-importer"; +import { formatBytes } from "@/lib/upload-limits"; +import { useT } from "@/stores/preferences-context"; + +export function NoteImportPanel(props: { + onImport: (title: string, content: string) => void; + onCancel?: () => void; +}) { + const t = useT(); + let fileInput: HTMLInputElement | undefined; + + const [file, setFile] = createSignal(null); + const [processing, setProcessing] = createSignal(false); + const [error, setError] = createSignal(""); + const [importedTitle, setImportedTitle] = createSignal(""); + const [importedMarkdown, setImportedMarkdown] = createSignal(""); + const [hasGarbled, setHasGarbled] = createSignal(false); + + const handleFileSelect = async (selectedFile: File | undefined) => { + if (!selectedFile) return; + setError(""); + + const name = selectedFile.name.toLowerCase(); + const isText = name.endsWith(".txt") || name.endsWith(".md") || selectedFile.type.startsWith("text/"); + const isPdf = name.endsWith(".pdf") || selectedFile.type === "application/pdf"; + + if (!isText && !isPdf) { + setError(t("notes.previewUnsupported")); + return; + } + + setFile(selectedFile); + setProcessing(true); + + try { + let rawText = ""; + if (isPdf) { + rawText = await extractPdfText(selectedFile); + } else { + rawText = await selectedFile.text(); + } + + const result = processImportedText(rawText, selectedFile.name); + setImportedTitle(result.title); + setImportedMarkdown(result.markdown); + setHasGarbled(result.hasGarbledWarning); + } catch (err) { + setError(t("notes.previewUnsupported")); + } finally { + setProcessing(false); + } + }; + + const handleApply = () => { + const title = importedTitle().trim() || "Imported Note"; + const content = importedMarkdown(); + props.onImport(title, content); + }; + + return ( +
+ +
fileInput?.click()} + > + { + fileInput = el; + }} + type="file" + accept=".pdf,.txt,.md,text/plain,text/markdown,application/pdf" + class="hidden" + onChange={(e) => void handleFileSelect(e.currentTarget.files?.[0])} + /> + + + +
+

+ {t("notes.import")} (PDF, TXT, Markdown) +

+

+ {t("notes.importHelp")} +

+
+
+
+ + +
+
+
+ + {file()?.name} + ({formatBytes(file()?.size ?? 0)}) +
+ +
+ + +

+ ⚠️ Some content may be unreadable due to OCR/extraction issues +

+
+ +
+ + setImportedTitle(e.currentTarget.value)} + /> +
+ +
+ +