'use client' /** * A favorite placed on a note page: the saved original, read-only, with its * way back to where it was saved. * * Collapsed it is one quiet card — what kind of thing it is, where it came * from, its title and gist. Opening it shows the original in place, capped * to a reading height with "Show all" for long ones, so a forty-turn * conversation does not push the rest of the page off the screen. */ import '@/components/notebook/favorites.css' import { useLayoutEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { ArrowUpRight, ChevronDown } from 'lucide-react' import FavoriteContent from '@/components/notebook/NotebookFavoriteContent' import { FavoriteKindIcon, favoriteLabel } from '@/components/notebook/favorite-kinds' import { favoriteKind } from '@/lib/favorite-content' import { formatRelativeTime } from '@/lib/relative-time' import Tooltip from '@/shared/ui/Tooltip' const BODY_CAP = 520 export default function NoteFavoriteBlock({ title, markdown, sourceUrl, type, summary, metadata, expanded, onExpanded, onActivate, }: { title: string markdown: string sourceUrl: string type: string summary: string metadata: string expanded: boolean onExpanded: (open: boolean) => void onActivate?: () => void }) { const { t, i18n } = useTranslation() let meta: Record = {} try { const value = JSON.parse(metadata) if (value && typeof value === 'object' && !Array.isArray(value)) meta = value } catch { /* Legacy clips have no structured metadata. */ } const kind = favoriteKind(type, meta) const collection = typeof meta.favorite_collection === 'string' ? meta.favorite_collection : '' const savedAt = typeof meta.favorite_saved_at === 'number' ? meta.favorite_saved_at : 0 const body = useRef(null) const [showAll, setShowAll] = useState(false) const [overflows, setOverflows] = useState(false) const toggle = () => { setShowAll(false) onExpanded(!expanded) } useLayoutEffect(() => { if (!expanded) return const el = body.current if (!el) return const measure = () => setOverflows(el.scrollHeight > BODY_CAP + 24) measure() // Markdown, KaTeX and media settle after the first paint. const observer = new ResizeObserver(measure) observer.observe(el.firstElementChild || el) return () => observer.disconnect() }, [expanded]) return (
{ onActivate?.() event.stopPropagation() }} onClick={event => event.stopPropagation()} >
{sourceUrl && ( )}
{expanded && (
)} {expanded && overflows && ( )}
) }