TipTap + tiptap-markdown render the document as it will appear; Markdown
stays the only storage/transport format so autosave/version conflicts,
server-side link+embed parsing, revisions, exports and the MCP tools are
all untouched.
- components/page-editor.tsx: edit-in-place blocks, Markdown input rules
(# / ** / [] ), slash block menu, formatting toolbar, GFM tables and
task lists, bare-URL paste offers a titled-link upgrade, superbrain:page
links navigate on click
- {{collection:...}} marker paragraphs render as live PageEmbedCards via a
paragraph node view (components/page-embed-card.tsx, extracted from the
pages client so both can use it without a cycle)
- the Eye/Pencil source<->preview toggle and the textarea are gone;
conflict banner, history restore and recovery drafts drive the editor
through resetContent()
- regressions drive the contenteditable via [data-page-editor] +
select-all + insertText (fill() cannot target contenteditable)
Pages stay Pages (no rename).
114 lines
6.0 KiB
TypeScript
114 lines
6.0 KiB
TypeScript
'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<string, unknown>; 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 (
|
|
<section className="not-prose my-4 overflow-hidden rounded-lg border bg-card">
|
|
<div className="flex items-center gap-2 border-b px-3 py-2">
|
|
<Layers className="h-4 w-4 shrink-0 text-muted-foreground" />
|
|
<Link href="/collections" className="truncate text-sm font-medium hover:underline">{embed.collection.name}</Link>
|
|
{viewLabel && <span className="truncate text-xs text-muted-foreground">· {viewLabel}</span>}
|
|
<span className="ml-auto text-xs text-muted-foreground">{items ? `${items.length} item${items.length === 1 ? '' : 's'}` : ''}</span>
|
|
</div>
|
|
{failed ? (
|
|
<p className="px-3 py-4 text-sm text-muted-foreground">This collection could not be loaded.</p>
|
|
) : items === null ? (
|
|
<p className="px-3 py-4 text-sm text-muted-foreground">Loading collection…</p>
|
|
) : items.length === 0 ? (
|
|
<p className="px-3 py-4 text-sm text-muted-foreground">No items match this view.</p>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-sm">
|
|
<thead className="bg-muted/40 text-xs text-muted-foreground">
|
|
<tr>
|
|
<th className="px-3 py-2 font-medium">Bookmark</th>
|
|
{properties.map((p) => <th key={p.id} className="px-3 py-2 font-medium">{p.name}</th>)}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{items.map((item) => (
|
|
<tr key={item.id} className="border-t">
|
|
<td className="max-w-[18rem] px-3 py-2">
|
|
<Link href={`/bookmarks?open=${item.bookmark.id}`} className="block truncate hover:underline">
|
|
{item.bookmark.title || item.bookmark.ogTitle || item.bookmark.url}
|
|
</Link>
|
|
<span className="block truncate text-xs text-muted-foreground">{host(item.bookmark.url)}</span>
|
|
</td>
|
|
{properties.map((p) => <td key={p.id} className="max-w-[12rem] truncate px-3 py-2 text-muted-foreground">{optionLabel(p, item.propertyValues?.[p.id])}</td>)}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</section>
|
|
)
|
|
}
|