hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB4e0a329Burak Boduroğlu2 months ago537 commits
| 1 | import { For, Show, Suspense, createResource, createSignal, lazy } from "solid-js"; |
| 2 | import { formatApiError } from "@/api/client"; |
| 3 | import { getSettings } from "@/api/settings"; |
| 4 | import type { Note } from "@/api/client"; |
| 5 | import { Button } from "@/components/ui/button"; |
| 6 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 7 | import { FormDialog } from "@/components/ui/form-dialog"; |
| 8 | import { IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; |
| 9 | import { Input } from "@/components/ui/input"; |
| 10 | import { Label } from "@/components/ui/label"; |
| 11 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 12 | import { RichTextEditor } from "@/components/ui/rich-text-editor"; |
| 13 | import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 14 | import { formatBytes, maxUploadBytes } from "@/lib/upload-limits"; |
| 15 | import { useT } from "@/stores/preferences-context"; |
| 16 | |
| 17 | const DrawCanvas = lazy(() => import("@/components/ui/draw-canvas").then((m) => ({ default: m.DrawCanvas }))); |
| 18 | |
| 19 | export type NoteFormValues = { |
| 20 | title: string; |
| 21 | content: string; |
| 22 | files: File[]; |
| 23 | }; |
| 24 | |
| 25 | const MAX_NOTE_FILES = 10; |
| 26 | |
| 27 | export function NoteForm(props: { |
| 28 | initial?: Partial<Note>; |
| 29 | submitLabel?: string; |
| 30 | /** Stage files on create (uploaded by parent after note exists). */ |
| 31 | enableFiles?: boolean; |
| 32 | onSubmit: (values: NoteFormValues) => Promise<void>; |
| 33 | onCancel?: () => void; |
| 34 | }) { |
| 35 | const t = useT(); |
| 36 | let fileInput: HTMLInputElement | undefined; |
| 37 | const isUpdate = () => !!props.initial?.id; |
| 38 | const [title, setTitle] = createSignal(props.initial?.title ?? ""); |
| 39 | const [content, setContent] = createSignal(props.initial?.content ?? ""); |
| 40 | const [contentView, setContentView] = createSignal<"write" | "preview">("write"); |
| 41 | const [files, setFiles] = createSignal<File[]>([]); |
| 42 | const [error, setError] = createSignal(""); |
| 43 | const [pending, setPending] = createSignal(false); |
| 44 | const [confirmOpen, setConfirmOpen] = createSignal(false); |
| 45 | const [drawOpen, setDrawOpen] = createSignal(false); |
| 46 | const [pendingValues, setPendingValues] = createSignal<NoteFormValues | null>(null); |
| 47 | const [settings] = createResource(async () => { |
| 48 | try { |
| 49 | return await getSettings(); |
| 50 | } catch { |
| 51 | return null; |
| 52 | } |
| 53 | }); |
| 54 | const maxFileBytes = () => maxUploadBytes(settings()); |
| 55 | const showFiles = () => !!props.enableFiles && !isUpdate(); |
| 56 | |
| 57 | const validate = (): string | null => { |
| 58 | const value = title().trim(); |
| 59 | if (!value) return t("form.titleRequired"); |
| 60 | if (value.length > 200) return t("form.titleMax"); |
| 61 | if (content().length > 10_000) return t("form.contentMax"); |
| 62 | return null; |
| 63 | }; |
| 64 | |
| 65 | const addFile = (file: File | undefined) => { |
| 66 | if (!file) return; |
| 67 | setError(""); |
| 68 | if (file.size > maxFileBytes()) { |
| 69 | setError(t("notes.fileTooLarge", { size: formatBytes(maxFileBytes()) })); |
| 70 | return; |
| 71 | } |
| 72 | if (files().length >= MAX_NOTE_FILES) { |
| 73 | setError(t("notes.fileLimit")); |
| 74 | return; |
| 75 | } |
| 76 | setFiles((prev) => [...prev, file]); |
| 77 | if (fileInput) fileInput.value = ""; |
| 78 | }; |
| 79 | |
| 80 | const save = async (values: NoteFormValues) => { |
| 81 | setError(""); |
| 82 | setPending(true); |
| 83 | try { |
| 84 | await props.onSubmit(values); |
| 85 | if (!isUpdate()) { |
| 86 | setTitle(""); |
| 87 | setContent(""); |
| 88 | setFiles([]); |
| 89 | } |
| 90 | } catch (err) { |
| 91 | setError(formatApiError(err)); |
| 92 | } finally { |
| 93 | setPending(false); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | const handleSubmit = async (e: SubmitEvent) => { |
| 98 | e.preventDefault(); |
| 99 | const v = validate(); |
| 100 | if (v) { |
| 101 | setError(v); |
| 102 | return; |
| 103 | } |
| 104 | const values: NoteFormValues = { title: title().trim(), content: content(), files: files() }; |
| 105 | if (isUpdate()) { |
| 106 | setPendingValues(values); |
| 107 | setConfirmOpen(true); |
| 108 | return; |
| 109 | } |
| 110 | await save(values); |
| 111 | }; |
| 112 | |
| 113 | return ( |
| 114 | <> |
| 115 | <form class="space-y-5" onSubmit={handleSubmit}> |
| 116 | <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-xs"> |
| 117 | <Label for="note-title">{t("form.title")}</Label> |
| 118 | <Input |
| 119 | id="note-title" |
| 120 | class="h-10 rounded-lg bg-background/80" |
| 121 | value={title()} |
| 122 | maxlength={200} |
| 123 | required |
| 124 | onInput={(e) => setTitle(e.currentTarget.value)} |
| 125 | /> |
| 126 | </div> |
| 127 | <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-xs"> |
| 128 | <div class="flex flex-wrap items-center justify-between gap-2"> |
| 129 | <Label for="note-content">{t("form.content")}</Label> |
| 130 | <Tabs value={contentView()} onChange={(value) => setContentView(value as "write" | "preview")}> |
| 131 | <TabsList class="w-auto"> |
| 132 | <TabsTrigger value="write" class="h-8 px-2.5 text-xs">{t("notes.write")}</TabsTrigger> |
| 133 | <TabsTrigger value="preview" class="h-8 px-2.5 text-xs">{t("notes.preview")}</TabsTrigger> |
| 134 | </TabsList> |
| 135 | <TabsContent value="write" class="hidden" /> |
| 136 | <TabsContent value="preview" class="hidden" /> |
| 137 | </Tabs> |
| 138 | </div> |
| 139 | <Show |
| 140 | when={contentView() === "write"} |
| 141 | fallback={ |
| 142 | <div class="min-h-40 rounded-lg border border-input bg-background/80 p-3 text-sm leading-relaxed text-foreground [&_ol]:ml-5 [&_ol]:list-decimal [&_ul]:ml-5 [&_ul]:list-disc"> |
| 143 | <Show when={content().trim()} fallback={<p class="text-muted-foreground">{t("notes.noContent")}</p>}> |
| 144 | <div innerHTML={content()} /> |
| 145 | </Show> |
| 146 | </div> |
| 147 | } |
| 148 | > |
| 149 | <RichTextEditor |
| 150 | value={content()} |
| 151 | onChange={setContent} |
| 152 | placeholder={t("form.content")} |
| 153 | minHeight="min-h-40" |
| 154 | /> |
| 155 | </Show> |
| 156 | </div> |
| 157 | |
| 158 | <Show when={showFiles()}> |
| 159 | <section class="space-y-3 rounded-xl border border-border/80 bg-card p-4 shadow-xs"> |
| 160 | <div class="flex flex-wrap items-center justify-between gap-3"> |
| 161 | <div> |
| 162 | <h3 class="text-sm font-semibold">{t("notes.files")}</h3> |
| 163 | <p class="mt-1 text-xs text-muted-foreground"> |
| 164 | {t("notes.filesHelp", { size: formatBytes(maxFileBytes()) })} |
| 165 | </p> |
| 166 | </div> |
| 167 | <input |
| 168 | ref={(el) => { |
| 169 | fileInput = el; |
| 170 | }} |
| 171 | type="file" |
| 172 | class="hidden" |
| 173 | disabled={pending() || files().length >= MAX_NOTE_FILES} |
| 174 | onChange={(event) => addFile(event.currentTarget.files?.[0])} |
| 175 | /> |
| 176 | <div class="flex flex-wrap items-center gap-2"> |
| 177 | <Button |
| 178 | type="button" |
| 179 | variant="outline" |
| 180 | size="sm" |
| 181 | class="rounded-md" |
| 182 | disabled={pending() || files().length >= MAX_NOTE_FILES} |
| 183 | onClick={() => setDrawOpen(true)} |
| 184 | > |
| 185 | <IconEdit class="h-4 w-4" /> |
| 186 | {t("notes.draw")} |
| 187 | </Button> |
| 188 | <Button |
| 189 | type="button" |
| 190 | size="sm" |
| 191 | class="rounded-md" |
| 192 | disabled={pending() || files().length >= MAX_NOTE_FILES} |
| 193 | onClick={() => fileInput?.click()} |
| 194 | > |
| 195 | <IconPlus class="h-4 w-4" /> |
| 196 | {t("notes.addFile")} |
| 197 | </Button> |
| 198 | </div> |
| 199 | </div> |
| 200 | <Show |
| 201 | when={files().length > 0} |
| 202 | fallback={ |
| 203 | <p class="rounded-md border border-dashed border-border/80 bg-muted/20 px-3 py-4 text-center text-sm text-muted-foreground"> |
| 204 | {t("notes.noFiles")} |
| 205 | </p> |
| 206 | } |
| 207 | > |
| 208 | <ul class="divide-y divide-border/70 rounded-md border border-border/70"> |
| 209 | <For each={files()}> |
| 210 | {(file, index) => ( |
| 211 | <li class="flex items-center justify-between gap-3 px-3 py-2 text-sm"> |
| 212 | <span class="min-w-0 flex-1 truncate font-medium">{file.name}</span> |
| 213 | <span class="shrink-0 text-xs text-muted-foreground">{formatBytes(file.size)}</span> |
| 214 | <Button |
| 215 | type="button" |
| 216 | variant="ghost" |
| 217 | size="icon" |
| 218 | class="h-8 w-8 rounded-md text-destructive hover:text-destructive" |
| 219 | onClick={() => setFiles((prev) => prev.filter((_, i) => i !== index()))} |
| 220 | > |
| 221 | <IconTrash class="h-4 w-4" /> |
| 222 | </Button> |
| 223 | </li> |
| 224 | )} |
| 225 | </For> |
| 226 | </ul> |
| 227 | </Show> |
| 228 | </section> |
| 229 | </Show> |
| 230 | |
| 231 | {error() && ( |
| 232 | <p class="rounded-lg border border-destructive/20 bg-destructive/10 px-3 py-2 text-sm text-destructive"> |
| 233 | {error()} |
| 234 | </p> |
| 235 | )} |
| 236 | <div class="flex flex-wrap items-center justify-end gap-2 border-t border-border/80 pt-4"> |
| 237 | {props.onCancel && ( |
| 238 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={props.onCancel}> |
| 239 | {t("common.cancel")} |
| 240 | </Button> |
| 241 | )} |
| 242 | <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> |
| 243 | {props.submitLabel ?? t("common.save")} |
| 244 | </Button> |
| 245 | </div> |
| 246 | </form> |
| 247 | |
| 248 | <FormDialog |
| 249 | open={drawOpen()} |
| 250 | onOpenChange={setDrawOpen} |
| 251 | title={t("notes.draw")} |
| 252 | description={t("notes.filesHelp", { size: formatBytes(maxFileBytes()) })} |
| 253 | class="max-w-4xl" |
| 254 | > |
| 255 | <Suspense fallback={<PageSpinner />}> |
| 256 | <DrawCanvas |
| 257 | onSave={(file) => { |
| 258 | addFile(file); |
| 259 | setDrawOpen(false); |
| 260 | }} |
| 261 | /> |
| 262 | </Suspense> |
| 263 | </FormDialog> |
| 264 | |
| 265 | <ConfirmDialog |
| 266 | open={confirmOpen()} |
| 267 | onOpenChange={setConfirmOpen} |
| 268 | title={t("confirm.updateTitle")} |
| 269 | summary={t("confirm.updateNote", { title: pendingValues()?.title ?? "" })} |
| 270 | onConfirm={async () => { |
| 271 | const values = pendingValues(); |
| 272 | if (!values) return; |
| 273 | await save(values); |
| 274 | }} |
| 275 | /> |
| 276 | </> |
| 277 | ); |
| 278 | } |