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 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 | <article |
| 33 | role="button" |
| 34 | tabindex="0" |
| 35 | class="group flex h-full min-h-44 cursor-pointer flex-col rounded-xl border border-border bg-card p-4 shadow-xs transition-[border-color,box-shadow] hover:border-primary/40 hover:shadow-md focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 36 | onClick={() => props.onOpen(props.note)} |
| 37 | onKeyDown={(event) => { |
| 38 | if (event.key === "Enter" || event.key === " ") { |
| 39 | event.preventDefault(); |
| 40 | props.onOpen(props.note); |
| 41 | } |
| 42 | }} |
| 43 | > |
| 44 | <div class="flex items-start justify-between gap-3"> |
| 45 | <h3 class="line-clamp-2 min-w-0 text-[15px] font-semibold leading-snug text-text-strong">{props.note.title}</h3> |
| 46 | <Show when={canManage()}> |
| 47 | <div class="-mr-1.5 -mt-1 shrink-0" onClick={(event) => event.stopPropagation()}> |
| 48 | <TableRowActions |
| 49 | label={t("common.actions")} |
| 50 | actions={[ |
| 51 | { |
| 52 | label: t("common.edit"), |
| 53 | icon: <IconEdit class="h-4 w-4" />, |
| 54 | onSelect: () => (props.onEdit ? props.onEdit(props.note) : setEditing(true)), |
| 55 | }, |
| 56 | { |
| 57 | label: t("common.delete"), |
| 58 | icon: <IconTrash class="h-4 w-4" />, |
| 59 | destructive: true, |
| 60 | onSelect: () => setDeleteOpen(true), |
| 61 | }, |
| 62 | ]} |
| 63 | /> |
| 64 | </div> |
| 65 | </Show> |
| 66 | </div> |
| 67 | {/* Plain-text excerpt: the card never renders stored HTML. */} |
| 68 | <p class={cn("mt-2 line-clamp-5 flex-1 text-[13px] leading-6", excerpt() ? "text-muted-foreground" : "italic text-muted-foreground/70")}> |
| 69 | {excerpt() || t("notes.noContent")} |
| 70 | </p> |
| 71 | <span 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-focus-visible:opacity-100"> |
| 72 | {t("notes.open")} |
| 73 | <IconChevronRight class="h-3.5 w-3.5" /> |
| 74 | </span> |
| 75 | {error() && <p class="mt-2 text-sm text-destructive-text">{error()}</p>} |
| 76 | </article> |
| 77 | |
| 78 | <SidePanel guardUnsaved open={editing()} onOpenChange={setEditing} title={t("common.edit")} description={props.note.title}> |
| 79 | <NoteForm |
| 80 | initial={props.note} |
| 81 | submitLabel={t("common.update")} |
| 82 | onCancel={() => setEditing(false)} |
| 83 | onSubmit={async (values) => { |
| 84 | await props.onUpdate(props.note.id, values); |
| 85 | setEditing(false); |
| 86 | }} |
| 87 | /> |
| 88 | </SidePanel> |
| 89 | |
| 90 | <ConfirmDialog |
| 91 | open={deleteOpen()} |
| 92 | onOpenChange={setDeleteOpen} |
| 93 | title={t("confirm.deleteTitle")} |
| 94 | variant="destructive" |
| 95 | summary={t("confirm.deleteNote", { title: props.note.title })} |
| 96 | onConfirm={async () => { |
| 97 | setError(""); |
| 98 | try { |
| 99 | await props.onDelete(props.note.id); |
| 100 | } catch (err) { |
| 101 | setError(formatApiError(err)); |
| 102 | throw err; |
| 103 | } |
| 104 | }} |
| 105 | /> |
| 106 | </> |
| 107 | ); |
| 108 | } |