hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Show, createSignal } from "solid-js"; |
| 2 | import { formatApiError } from "@/api/client"; |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { IconFileText, IconUploadCloud } from "@/components/ui/icons"; |
| 5 | import { Input } from "@/components/ui/input"; |
| 6 | import { Label } from "@/components/ui/label"; |
| 7 | import { Textarea } from "@/components/ui/textarea"; |
| 8 | import { extractImportedPdfText, processImportedText } from "@/lib/note-importer"; |
| 9 | import { triggerConfetti } from "@/lib/confetti"; |
| 10 | import { formatBytes } from "@/lib/upload-limits"; |
| 11 | import { useT } from "@/stores/preferences-context"; |
| 12 | |
| 13 | export function NoteImportPanel(props: { |
| 14 | onImport: (title: string, content: string) => Promise<void> | void; |
| 15 | onCancel?: () => void; |
| 16 | }) { |
| 17 | const t = useT(); |
| 18 | let fileInput: HTMLInputElement | undefined; |
| 19 | |
| 20 | const [file, setFile] = createSignal<File | null>(null); |
| 21 | const [processing, setProcessing] = createSignal(false); |
| 22 | const [submitting, setSubmitting] = createSignal(false); |
| 23 | const [error, setError] = createSignal(""); |
| 24 | const [importedTitle, setImportedTitle] = createSignal(""); |
| 25 | const [importedMarkdown, setImportedMarkdown] = createSignal(""); |
| 26 | const [hasGarbled, setHasGarbled] = createSignal(false); |
| 27 | |
| 28 | const handleFileSelect = async (selectedFile: File | undefined) => { |
| 29 | if (!selectedFile) return; |
| 30 | setError(""); |
| 31 | |
| 32 | const name = selectedFile.name.toLowerCase(); |
| 33 | const isText = name.endsWith(".txt") || name.endsWith(".md") || selectedFile.type.startsWith("text/"); |
| 34 | const isPdf = name.endsWith(".pdf") || selectedFile.type === "application/pdf"; |
| 35 | |
| 36 | if (!isText && !isPdf) { |
| 37 | setError(t("notes.previewUnsupported")); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | setFile(selectedFile); |
| 42 | setProcessing(true); |
| 43 | |
| 44 | try { |
| 45 | let rawText = ""; |
| 46 | if (isPdf) { |
| 47 | rawText = await extractImportedPdfText(selectedFile); |
| 48 | } else { |
| 49 | rawText = await selectedFile.text(); |
| 50 | } |
| 51 | |
| 52 | if (!rawText.trim()) throw new Error("empty import"); |
| 53 | |
| 54 | const result = processImportedText(rawText, selectedFile.name); |
| 55 | setImportedTitle(result.title); |
| 56 | setImportedMarkdown(result.markdown); |
| 57 | setHasGarbled(result.hasGarbledWarning); |
| 58 | } catch { |
| 59 | setFile(null); |
| 60 | setImportedTitle(""); |
| 61 | setImportedMarkdown(""); |
| 62 | setHasGarbled(false); |
| 63 | setError(t("notes.importReadError")); |
| 64 | } finally { |
| 65 | setProcessing(false); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | const handleApply = async () => { |
| 70 | const title = importedTitle().trim() || "Imported Note"; |
| 71 | const content = importedMarkdown(); |
| 72 | setError(""); |
| 73 | setSubmitting(true); |
| 74 | |
| 75 | try { |
| 76 | await props.onImport(title, content); |
| 77 | triggerConfetti(); |
| 78 | } catch (err) { |
| 79 | setError(formatApiError(err)); |
| 80 | } finally { |
| 81 | setSubmitting(false); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | return ( |
| 86 | <div class="space-y-5"> |
| 87 | <Show when={!file()}> |
| 88 | <div |
| 89 | class="flex min-h-56 cursor-pointer flex-col items-center justify-center gap-3 rounded-lg border border-dashed border-amber-500/50 ring-1 ring-amber-500/25 bg-muted/30 p-6 text-center transition-all hover:border-amber-400 hover:ring-amber-400/40 hover:bg-muted/50" |
| 90 | onClick={() => fileInput?.click()} |
| 91 | > |
| 92 | <input |
| 93 | ref={(el) => { |
| 94 | fileInput = el; |
| 95 | }} |
| 96 | type="file" |
| 97 | accept=".pdf,.txt,.md,text/plain,text/markdown,application/pdf" |
| 98 | class="hidden" |
| 99 | onChange={(e) => void handleFileSelect(e.currentTarget.files?.[0])} |
| 100 | /> |
| 101 | <span class="flex h-12 w-12 items-center justify-center rounded-lg border border-amber-500/50 ring-1 ring-amber-500/30 bg-muted/40 text-foreground shadow-xs"> |
| 102 | <IconUploadCloud class="h-6 w-6" /> |
| 103 | </span> |
| 104 | <div class="space-y-1 max-w-sm mx-auto"> |
| 105 | <p class="text-sm font-semibold text-foreground"> |
| 106 | {t("notes.import")} (PDF, TXT, Markdown) |
| 107 | </p> |
| 108 | <p class="text-xs leading-normal text-muted-foreground"> |
| 109 | {t("notes.importHelp")} |
| 110 | </p> |
| 111 | </div> |
| 112 | </div> |
| 113 | </Show> |
| 114 | |
| 115 | <Show when={file()}> |
| 116 | <div class="space-y-4"> |
| 117 | <div class="flex items-center justify-between rounded-xl border border-amber-500/40 ring-1 ring-amber-500/25 bg-card px-4 py-3 text-xs"> |
| 118 | <div class="flex items-center gap-2.5 min-w-0"> |
| 119 | <IconFileText class="h-4 w-4 shrink-0 text-foreground" /> |
| 120 | <span class="truncate font-medium text-foreground">{file()?.name}</span> |
| 121 | <span class="text-muted-foreground shrink-0">({formatBytes(file()?.size ?? 0)})</span> |
| 122 | </div> |
| 123 | <Button |
| 124 | type="button" |
| 125 | variant="ghost" |
| 126 | size="sm" |
| 127 | class="h-7 text-xs rounded-lg" |
| 128 | onClick={() => { |
| 129 | setFile(null); |
| 130 | setError(""); |
| 131 | setImportedTitle(""); |
| 132 | setImportedMarkdown(""); |
| 133 | setHasGarbled(false); |
| 134 | }} |
| 135 | > |
| 136 | {t("common.edit")} |
| 137 | </Button> |
| 138 | </div> |
| 139 | |
| 140 | <Show when={hasGarbled()}> |
| 141 | <p class="rounded-xl border border-amber-500/30 bg-amber-500/10 px-3.5 py-2.5 text-xs font-medium text-amber-700 dark:text-amber-300"> |
| 142 | ⚠️ Some content may be unreadable due to OCR/extraction issues |
| 143 | </p> |
| 144 | </Show> |
| 145 | |
| 146 | <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-xs"> |
| 147 | <Label for="import-title">{t("form.title")}</Label> |
| 148 | <Input |
| 149 | id="import-title" |
| 150 | class="h-10 rounded-lg bg-background/80" |
| 151 | value={importedTitle()} |
| 152 | maxlength={200} |
| 153 | onInput={(e) => setImportedTitle(e.currentTarget.value)} |
| 154 | /> |
| 155 | </div> |
| 156 | |
| 157 | <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-xs"> |
| 158 | <Label for="import-content">{t("form.content")} (Markdown)</Label> |
| 159 | <Textarea |
| 160 | id="import-content" |
| 161 | class="min-h-56 rounded-lg font-mono text-xs bg-background/80 leading-relaxed" |
| 162 | value={importedMarkdown()} |
| 163 | rows={10} |
| 164 | onInput={(e) => setImportedMarkdown(e.currentTarget.value)} |
| 165 | /> |
| 166 | </div> |
| 167 | |
| 168 | <div class="flex flex-wrap items-center justify-end gap-2 border-t border-border/80 pt-4"> |
| 169 | <Show when={props.onCancel}> |
| 170 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={props.onCancel}> |
| 171 | {t("common.cancel")} |
| 172 | </Button> |
| 173 | </Show> |
| 174 | <Button |
| 175 | type="button" |
| 176 | class="h-10 rounded-lg px-5 font-semibold" |
| 177 | disabled={processing() || submitting() || !importedMarkdown().trim()} |
| 178 | onClick={() => void handleApply()} |
| 179 | > |
| 180 | {t("common.create")} |
| 181 | </Button> |
| 182 | </div> |
| 183 | </div> |
| 184 | </Show> |
| 185 | |
| 186 | <Show when={error()}> |
| 187 | <p class="rounded-lg border border-destructive/20 bg-destructive/10 px-3 py-2 text-sm text-destructive-text"> |
| 188 | {error()} |
| 189 | </p> |
| 190 | </Show> |
| 191 | </div> |
| 192 | ); |
| 193 | } |