mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 01:10:10 +08:00
Add rich text and learning blocks, media uploads, page trees, and revisioned notebook storage. Present AI suggestions at their target blocks with durable accept and dismiss actions. Unify saved outputs as Favorites with typed expandable blocks, stable source links, atomic batch insertion, nesting, dragging, and undo/redo. Validation: 99 frontend tests and 99 backend tests pass in an isolated snapshot, along with TypeScript, scoped ESLint, dependency architecture, and locale parity checks.
125 lines
3.5 KiB
TypeScript
125 lines
3.5 KiB
TypeScript
'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<NoteSummary[] | null>(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 (
|
|
<div className="px-5 py-3 text-sm">
|
|
{pages === null ? (
|
|
<button
|
|
className="rounded px-2 py-1.5 hover:bg-[var(--secondary)]"
|
|
onClick={() => {
|
|
void listNotes()
|
|
.then(r => setPages(r.documents))
|
|
.catch(e => setError(String(e)))
|
|
}}
|
|
>
|
|
{t('Add to a note')}
|
|
</button>
|
|
) : (
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<select
|
|
aria-label={t('Target page')}
|
|
className="max-w-64 rounded bg-[var(--secondary)] p-2"
|
|
value={target}
|
|
onChange={e => {
|
|
setTarget(e.target.value)
|
|
setSaved(false)
|
|
}}
|
|
>
|
|
<option value="">{t('New page')}</option>
|
|
{pages.map(p => (
|
|
<option key={p.id} value={p.id}>
|
|
{p.title}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<button
|
|
className="rounded px-2 py-1.5 hover:bg-[var(--secondary)]"
|
|
disabled={busy || saved || !markdown.trim()}
|
|
onClick={() => void save()}
|
|
>
|
|
{t(saved ? 'Added to note' : busy ? 'Saving…' : 'Add to a note')}
|
|
</button>
|
|
{saved && target && (
|
|
<Link
|
|
href={`/co-writer/${target}`}
|
|
className="rounded px-2 py-1.5 text-[var(--primary)] hover:underline"
|
|
>
|
|
{t('Open note')}
|
|
</Link>
|
|
)}
|
|
</div>
|
|
)}
|
|
{error && <p role="alert">{error}</p>}
|
|
</div>
|
|
)
|
|
}
|