hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 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 | role="button" |
| 90 | tabindex="0" |
| 91 | aria-busy={processing()} |
| 92 | class="flex min-h-56 cursor-pointer flex-col items-center justify-center gap-3 rounded-xl border border-dashed border-primary/40 bg-muted/30 p-6 text-center outline-hidden transition-colors hover:border-primary/70 hover:bg-muted/50 focus-visible:ring-2 focus-visible:ring-ring" |
| 93 | onClick={() => fileInput?.click()} |
| 94 | onKeyDown={(event) => { |
| 95 | if (event.target !== event.currentTarget) return; |
| 96 | if (event.key === "Enter" || event.key === " ") { |
| 97 | event.preventDefault(); |
| 98 | fileInput?.click(); |
| 99 | } |
| 100 | }} |
| 101 | > |
| 102 | <input |
| 103 | ref={(el) => { |
| 104 | fileInput = el; |
| 105 | }} |
| 106 | type="file" |
| 107 | accept=".pdf,.txt,.md,text/plain,text/markdown,application/pdf" |
| 108 | class="hidden" |
| 109 | onChange={(e) => void handleFileSelect(e.currentTarget.files?.[0])} |
| 110 | /> |
| 111 | <span class="flex h-12 w-12 items-center justify-center rounded-lg border border-primary/40 bg-primary/10 text-primary-text shadow-xs"> |
| 112 | <IconUploadCloud class="h-6 w-6" /> |
| 113 | </span> |
| 114 | <div class="space-y-1 max-w-sm mx-auto"> |
| 115 | <p class="text-sm font-semibold text-foreground"> |
| 116 | {t("notes.import")} (PDF, TXT, Markdown) |
| 117 | </p> |
| 118 | <p class="text-xs leading-normal text-muted-foreground" aria-live="polite"> |
| 119 | {processing() ? t("notes.importReading") : t("notes.importHelp")} |
| 120 | </p> |
| 121 | </div> |
| 122 | </div> |
| 123 | </Show> |
| 124 | |
| 125 | <Show when={file()}> |
| 126 | <div class="space-y-4"> |
| 127 | <div class="flex items-center justify-between gap-2 rounded-xl border border-border-line bg-card px-4 py-3 text-xs"> |
| 128 | <div class="flex items-center gap-2.5 min-w-0"> |
| 129 | <IconFileText class="h-4 w-4 shrink-0 text-foreground" /> |
| 130 | <span class="truncate font-medium text-foreground">{file()?.name}</span> |
| 131 | <span class="text-muted-foreground shrink-0">({formatBytes(file()?.size ?? 0)})</span> |
| 132 | </div> |
| 133 | <Button |
| 134 | type="button" |
| 135 | variant="ghost" |
| 136 | size="sm" |
| 137 | class="h-8 shrink-0 rounded-full px-3 text-xs touch:h-10" |
| 138 | onClick={() => { |
| 139 | setFile(null); |
| 140 | setError(""); |
| 141 | setImportedTitle(""); |
| 142 | setImportedMarkdown(""); |
| 143 | setHasGarbled(false); |
| 144 | }} |
| 145 | > |
| 146 | {t("notes.importChangeFile")} |
| 147 | </Button> |
| 148 | </div> |
| 149 | |
| 150 | <Show when={hasGarbled()}> |
| 151 | <p class="rounded-xl border border-warning/30 bg-warning/10 px-3.5 py-2.5 text-xs font-medium text-warning-text"> |
| 152 | {t("notes.importGarbled")} |
| 153 | </p> |
| 154 | </Show> |
| 155 | |
| 156 | <div class="space-y-1.5 rounded-xl border border-border-line bg-card p-4 shadow-xs"> |
| 157 | <Label for="import-title">{t("form.title")}</Label> |
| 158 | <Input |
| 159 | id="import-title" |
| 160 | class="h-10 rounded-lg bg-background/80" |
| 161 | value={importedTitle()} |
| 162 | maxlength={200} |
| 163 | onInput={(e) => setImportedTitle(e.currentTarget.value)} |
| 164 | /> |
| 165 | </div> |
| 166 | |
| 167 | <div class="space-y-1.5 rounded-xl border border-border-line bg-card p-4 shadow-xs"> |
| 168 | <Label for="import-content">{t("form.content")} (Markdown)</Label> |
| 169 | <Textarea |
| 170 | id="import-content" |
| 171 | class="min-h-56 rounded-lg font-mono text-xs bg-background/80 leading-relaxed" |
| 172 | value={importedMarkdown()} |
| 173 | rows={10} |
| 174 | onInput={(e) => setImportedMarkdown(e.currentTarget.value)} |
| 175 | /> |
| 176 | </div> |
| 177 | |
| 178 | <div class="flex flex-wrap items-center justify-end gap-2 border-t border-border/80 pt-4"> |
| 179 | <Show when={props.onCancel}> |
| 180 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={props.onCancel}> |
| 181 | {t("common.cancel")} |
| 182 | </Button> |
| 183 | </Show> |
| 184 | <Button |
| 185 | type="button" |
| 186 | class="h-10 rounded-lg px-5 font-semibold" |
| 187 | disabled={processing() || submitting() || !importedMarkdown().trim()} |
| 188 | onClick={() => void handleApply()} |
| 189 | > |
| 190 | {t("common.create")} |
| 191 | </Button> |
| 192 | </div> |
| 193 | </div> |
| 194 | </Show> |
| 195 | |
| 196 | <Show when={error()}> |
| 197 | <p class="rounded-lg border border-destructive/20 bg-destructive/10 px-3 py-2 text-sm text-destructive-text"> |
| 198 | {error()} |
| 199 | </p> |
| 200 | </Show> |
| 201 | </div> |
| 202 | ); |
| 203 | } |