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