import { useCallback } from 'react' import { avatarSrc } from '../../utils/avatarSrc' import { safeExternalHref } from '../../utils/safeUrl' import Markdown from 'react-markdown' import remarkGfm from 'remark-gfm' import remarkBreaks from 'remark-breaks' import { sanitizedMarkdownPlugins, sanitizedMarkdownComponents } from '../shared/markdownSanitize' import { ExternalLink, Maximize2, MoreHorizontal, Pencil, Pin, PinOff, Trash2 } from 'lucide-react' import { Tooltip } from '../shared/Tooltip' import { ContextMenu, useContextMenu } from '../shared/ContextMenu' import { Eyebrow, fs } from '../Planner/bookings/bookingParts' import { AuthedImg } from './CollabNotesAuthedImg' import { UserAvatar } from './CollabNotesUserAvatar' import { NoteHeadBand } from './CollabNoteHead' import type { CollabNote, NoteFile } from './CollabNotes.types' import type { User } from '../../types' // ── Note Card ─────────────────────────────────────────────────────────────── interface NoteCardProps { note: CollabNote currentUser: User canEdit: boolean onUpdate: (noteId: number, data: Partial) => Promise onDelete: (noteId: number) => void onEdit: (note: CollabNote) => void onView: (note: CollabNote) => void onPreviewFile: (file: NoteFile) => void getCategoryColor: (category: string) => string tripId: number t: (key: string) => string } /** The link chip shows the host: that is what people recognise, "www." is noise. */ function linkHost(url: string): string { try { return new URL(url).hostname.replace(/^www\./, '') } catch { return url } } const LINK_BTN = 'grid h-[26px] w-[26px] flex-none place-items-center rounded-full bg-surface-card shadow-sm' /** * A note as the booking cards are drawn, kept compact: one head band tinted by * its category, carrying the category dot, the title, its link, the actions * behind "…" and who wrote it; under it the text and the files. */ export function NoteCard({ note, canEdit, onUpdate, onDelete, onEdit, onView, onPreviewFile, getCategoryColor, t }: NoteCardProps) { const author = note.author || note.user || { username: note.username, avatar: note.avatar_url || avatarSrc(note.avatar) } const color = getCategoryColor ? getCategoryColor(note.category) : (note.color || '#6366f1') const attachments = note.attachments || [] const shown = attachments.slice(0, 3) const hidden = attachments.length - shown.length // Allow-listed like everywhere else a note's link opens: the field takes any string. const websiteHref = safeExternalHref(note.website) const hasBody = !!note.content || attachments.length > 0 const menu = useContextMenu() const handleTogglePin = useCallback(() => { void onUpdate(note.id, { pinned: !note.pinned }) }, [note.id, note.pinned, onUpdate]) const handleDelete = useCallback(() => { onDelete(note.id) }, [note.id, onDelete]) const menuItems = [ ...(note.content ? [{ label: t('common.expand'), icon: Maximize2, onClick: () => onView?.(note) }] : []), ...(canEdit ? [ { label: note.pinned ? t('collab.notes.unpin') : t('collab.notes.pin'), icon: note.pinned ? PinOff : Pin, onClick: handleTogglePin }, { label: t('collab.notes.edit'), icon: Pencil, onClick: () => onEdit?.(note) }, { divider: true }, { label: t('collab.notes.delete'), icon: Trash2, onClick: handleDelete, danger: true }, ] : []), ] return (
{/* The link rides in the title row as one round button; the host is in its tooltip. */} {note.website && ( {websiteHref ? ( ) : ( )} )} {menuItems.length > 0 && ( )} } /> {hasBody && (
{note.content && (
{note.content}
)} {attachments.length > 0 && (
{t('files.title')}
{shown.map(a => { const isImage = a.mime_type?.startsWith('image/') const isPdf = a.mime_type === 'application/pdf' const ext = (a.original_name || '').split('.').pop()?.toUpperCase() || '?' return isImage ? ( onPreviewFile?.(a)} /> ) : ( ) })} {hidden > 0 && ( +{hidden} )}
)}
)}
) }