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 type { Note } from "@/api/client"; |
| 4 | import { NoteForm } from "@/components/notes/note-form"; |
| 5 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 6 | import { IconChevronRight, IconEdit, IconTrash } from "@/components/ui/icons"; |
| 7 | import { SidePanel } from "@/components/ui/side-panel"; |
| 8 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 9 | import { cn } from "@/lib/cn"; |
| 10 | import { richTextExcerpt, toRichTextHtml } from "@/lib/rich-text"; |
| 11 | import { useT } from "@/stores/preferences-context"; |
| 12 | |
| 13 | export function NoteCard(props: { |
| 14 | note: Note; |
| 15 | /** Hides the edit/delete menu when false. Defaults to true. */ |
| 16 | canManage?: boolean; |
| 17 | onOpen: (note: Note) => void; |
| 18 | /** Edits somewhere else (the full-page editor) instead of the side panel. */ |
| 19 | onEdit?: (note: Note) => void; |
| 20 | onUpdate: (id: string, values: { title: string; content: string }) => Promise<void>; |
| 21 | onDelete: (id: string) => Promise<void>; |
| 22 | }) { |
| 23 | const t = useT(); |
| 24 | const [editing, setEditing] = createSignal(false); |
| 25 | const [error, setError] = createSignal(""); |
| 26 | const [deleteOpen, setDeleteOpen] = createSignal(false); |
| 27 | const canManage = () => props.canManage !== false; |
| 28 | const excerpt = () => richTextExcerpt(toRichTextHtml(props.note.content), 320); |
| 29 | |
| 30 | return ( |
| 31 | <> |
| 32 | {/* The title is the card's one open control, stretched over the whole |
| 33 | card; the actions menu sits above it. A role="button" card wrapping |
| 34 | the menu nested one button in another, and Enter on the menu |
| 35 | trigger bubbled up and opened the note. */} |
| 36 | <article class="group relative flex h-full min-h-44 cursor-pointer flex-col rounded-xl border border-border-line bg-card p-4 shadow-xs transition-[border-color,box-shadow] hover:border-primary/40 hover:shadow-md has-[.note-card-open:focus-visible]:ring-2 has-[.note-card-open:focus-visible]:ring-ring"> |
| 37 | <div class="flex items-start justify-between gap-3"> |
| 38 | <h3 class="line-clamp-2 min-w-0 text-[15px] font-semibold leading-snug text-text-strong"> |
| 39 | <button |
| 40 | type="button" |
| 41 | class="note-card-open text-left outline-hidden after:absolute after:inset-0 after:rounded-xl after:content-['']" |
| 42 | onClick={() => props.onOpen(props.note)} |
| 43 | > |
| 44 | {props.note.title} |
| 45 | </button> |
| 46 | </h3> |
| 47 | <Show when={canManage()}> |
| 48 | <div class="relative z-10 -mr-1.5 -mt-1 shrink-0"> |
| 49 | <TableRowActions |
| 50 | label={t("common.actions")} |
| 51 | actions={[ |
| 52 | { |
| 53 | label: t("common.edit"), |
| 54 | icon: <IconEdit class="h-4 w-4" />, |
| 55 | onSelect: () => (props.onEdit ? props.onEdit(props.note) : setEditing(true)), |
| 56 | }, |
| 57 | { |
| 58 | label: t("common.delete"), |
| 59 | icon: <IconTrash class="h-4 w-4" />, |
| 60 | destructive: true, |
| 61 | onSelect: () => setDeleteOpen(true), |
| 62 | }, |
| 63 | ]} |
| 64 | /> |
| 65 | </div> |
| 66 | </Show> |
| 67 | </div> |
| 68 | {/* Plain-text excerpt: the card never renders stored HTML. */} |
| 69 | <p class={cn("mt-2 line-clamp-5 flex-1 text-[13px] leading-6", excerpt() ? "text-muted-foreground" : "italic text-muted-foreground/70")}> |
| 70 | {excerpt() || t("notes.noContent")} |
| 71 | </p> |
| 72 | <span aria-hidden="true" class="mt-3 inline-flex items-center gap-1 text-xs font-medium text-primary-text opacity-0 transition-opacity group-hover:opacity-100 group-has-[.note-card-open:focus-visible]:opacity-100"> |
| 73 | {t("notes.open")} |
| 74 | <IconChevronRight class="h-3.5 w-3.5" /> |
| 75 | </span> |
| 76 | {error() && <p class="relative z-10 mt-2 text-sm text-destructive-text">{error()}</p>} |
| 77 | </article> |
| 78 | |
| 79 | <SidePanel guardUnsaved open={editing()} onOpenChange={setEditing} title={t("common.edit")} description={props.note.title}> |
| 80 | <NoteForm |
| 81 | initial={props.note} |
| 82 | submitLabel={t("common.update")} |
| 83 | onCancel={() => setEditing(false)} |
| 84 | onSubmit={async (values) => { |
| 85 | await props.onUpdate(props.note.id, values); |
| 86 | setEditing(false); |
| 87 | }} |
| 88 | /> |
| 89 | </SidePanel> |
| 90 | |
| 91 | <ConfirmDialog |
| 92 | open={deleteOpen()} |
| 93 | onOpenChange={setDeleteOpen} |
| 94 | title={t("confirm.deleteTitle")} |
| 95 | variant="destructive" |
| 96 | summary={t("confirm.deleteNote", { title: props.note.title })} |
| 97 | onConfirm={async () => { |
| 98 | setError(""); |
| 99 | try { |
| 100 | await props.onDelete(props.note.id); |
| 101 | } catch (err) { |
| 102 | setError(formatApiError(err)); |
| 103 | throw err; |
| 104 | } |
| 105 | }} |
| 106 | /> |
| 107 | </> |
| 108 | ); |
| 109 | } |