'use client' import Link from 'next/link' import type { FavoriteSnapshot } from '@/lib/favorite-content' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { listNotes, createNote, getNote, saveNote, noteRequest, type NoteSummary, } from '@/lib/notes-api' import { notifyCoWriterChanged } from '@/lib/co-writer-events' export default function AddToNote({ title, markdown, sourceUrl = '', sourceId = '', onSaved, favorite, }: { title: string markdown: string sourceUrl?: string sourceId?: string onSaved?: () => void favorite?: FavoriteSnapshot }) { const { t } = useTranslation() const [pages, setPages] = useState(null) const [target, setTarget] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState('') const [saved, setSaved] = useState(false) const save = async () => { setBusy(true) setError('') try { let id = target if (!id) { const doc = await createNote(title) id = doc.id setTarget(id) setPages(current => [{ ...doc, preview: doc.content.slice(0, 160) }, ...(current || [])]) } const page = await getNote(id) if (page.blocks === null && page.content.trim()) { const { parseLegacyNoteContent } = await import('./NoteEditor') await saveNote(id, { blocks: parseLegacyNoteContent(page.content), expected_revision: page.revision, }) } await noteRequest(`/${id}/clips`, { title, markdown, source_url: sourceUrl, source_id: sourceId, favorite_type: favorite?.type || '', summary: favorite?.summary || '', favorite_metadata: favorite?.metadata || {}, }) setSaved(true) notifyCoWriterChanged() onSaved?.() } catch (err) { setError(String(err)) } finally { setBusy(false) } } return (
{pages === null ? ( ) : (
{saved && target && ( {t('Open note')} )}
)} {error &&

{error}

}
) }