'use client' // Read-only live collection view embedded in a page (phase 3). Extracted // from the pages client so the WYSIWYG page editor can render embed markers // as cards inside the document (paragraph node view) without an import // cycle. It fetches the same authorized item endpoint the collection // workspace uses, so a live embed never exposes a bookmark the viewer // cannot already see and the server's filters/sorts still apply. It is a // display, never a publication. import { useEffect, useState } from 'react' import Link from 'next/link' import { Layers } from 'lucide-react' export type LinkedBookmark = { id: string; url: string; title: string | null; favicon: string | null; ogImage: string | null; ogTitle: string | null; ogDescription: string | null; description: string | null } export type EmbeddedProperty = { id: string; name: string type: 'TEXT' | 'NUMBER' | 'SELECT' | 'MULTI_SELECT' | 'DATE' | 'CHECKBOX' | 'URL' position: number; config: { options?: { id: string; label: string; color?: string }[]; mode?: 'date' | 'datetime' } } export type EmbeddedView = { id: string; name: string; type: string; position: number } export type PageEmbed = { collectionId: string; viewId: string | null; position: number collection: { id: string; name: string; icon: string | null; description: string | null; properties: EmbeddedProperty[]; views: EmbeddedView[] } view: EmbeddedView | null } export type EmbeddedItem = { id: string; propertyValues: Record; bookmark: LinkedBookmark } const host = (u: string) => { try { return new URL(u).hostname.replace(/^www\./, '') } catch { return u } } export function PageEmbedCard({ embed }: { embed: PageEmbed }) { const viewId = embed.viewId ?? embed.collection.views[0]?.id ?? null const embedKey = `${embed.collectionId}|${viewId ?? ''}` // The fetched result carries its embed key so switching collections never // shows another embed's items for a frame; the effect only writes state // asynchronously (no cascading synchronous setState). const [loaded, setLoaded] = useState<{ key: string; items: EmbeddedItem[] | null; failed: boolean }>({ key: '', items: null, failed: false }) useEffect(() => { let cancelled = false const params = new URLSearchParams({ limit: '50' }) if (viewId) params.set('viewId', viewId) fetch(`/api/collections/${embed.collectionId}/items?${params}`) .then((res) => (res.ok ? res.json() : Promise.reject(new Error('load failed')))) .then((data: EmbeddedItem[]) => { if (!cancelled) setLoaded({ key: embedKey, items: data, failed: false }) }) .catch(() => { if (!cancelled) setLoaded({ key: embedKey, items: null, failed: true }) }) return () => { cancelled = true } }, [embed.collectionId, embedKey, viewId]) const current = loaded.key === embedKey ? loaded : { key: embedKey, items: null, failed: false } const items = current.items const failed = current.failed const properties = embed.collection.properties.slice(0, 4) const optionLabel = (prop: EmbeddedProperty, value: unknown) => { if (prop.type === 'CHECKBOX') return value === true ? 'Yes' : 'No' if (prop.type === 'SELECT') return prop.config.options?.find((o) => o.id === value)?.label ?? '—' if (prop.type === 'MULTI_SELECT') { if (!Array.isArray(value)) return '—' return value.map((id) => prop.config.options?.find((o) => o.id === id)?.label).filter(Boolean).join(', ') || '—' } if (prop.type === 'DATE' && typeof value === 'string' && value) { if (prop.config.mode === 'date') return value.slice(0, 10) const d = new Date(value) return Number.isNaN(d.getTime()) ? value : d.toLocaleDateString('en-US', { timeZone: 'UTC' }) } if (value === null || value === undefined || value === '') return '—' return String(value) } const viewLabel = embed.view?.name ?? embed.collection.views[0]?.name return (
{embed.collection.name} {viewLabel && · {viewLabel}} {items ? `${items.length} item${items.length === 1 ? '' : 's'}` : ''}
{failed ? (

This collection could not be loaded.

) : items === null ? (

Loading collection…

) : items.length === 0 ? (

No items match this view.

) : (
{properties.map((p) => )} {items.map((item) => ( {properties.map((p) => )} ))}
Bookmark{p.name}
{item.bookmark.title || item.bookmark.ogTitle || item.bookmark.url} {host(item.bookmark.url)} {optionLabel(p, item.propertyValues?.[p.id])}
)}
) }