mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 07:30:13 +08:00
199 lines
6.5 KiB
TypeScript
199 lines
6.5 KiB
TypeScript
'use client'
|
|
|
|
import { useState, type MouseEvent } from 'react'
|
|
import Link from 'next/link'
|
|
import dynamic from 'next/dynamic'
|
|
import { useTranslation } from 'react-i18next'
|
|
import {
|
|
MoreHorizontal,
|
|
PanelLeft,
|
|
Pin,
|
|
Sparkles,
|
|
} from 'lucide-react'
|
|
import Tooltip from '@/shared/ui/Tooltip'
|
|
import { useMenuPopover } from '@/shared/ui/useMenuPopover'
|
|
import { FeatureCrumb, InstanceCrumb } from '@/components/layout/LocationCrumbs'
|
|
import { useShelfFeature } from '@/components/sidebar/PageBar'
|
|
import type { NotePage, NoteSummary } from '@/lib/notes-api'
|
|
const NotePageMenu = dynamic(() => import('./NotePageMenu'), { ssr: false })
|
|
|
|
export type NoteSaveStatus = 'Saved' | 'Saving…' | 'Unsaved changes' | 'Save failed'
|
|
|
|
function Crumb({
|
|
page,
|
|
onNavigate,
|
|
}: {
|
|
page: Pick<NoteSummary, 'id' | 'title' | 'icon'>
|
|
onNavigate: (event: MouseEvent<HTMLAnchorElement>, id: string) => void
|
|
}) {
|
|
const { t } = useTranslation()
|
|
return (
|
|
<Link
|
|
href={`/co-writer/${page.id}`}
|
|
className="dt-note-crumb"
|
|
onClick={event => onNavigate(event, page.id)}
|
|
>
|
|
{page.icon && <span aria-hidden>{page.icon}</span>}
|
|
<span className="truncate">{page.title || t('Untitled')}</span>
|
|
</Link>
|
|
)
|
|
}
|
|
|
|
export default function NoteTopBar({
|
|
page,
|
|
pages,
|
|
invalidParents,
|
|
status,
|
|
busy,
|
|
embedded,
|
|
treeOpen,
|
|
companionOpen,
|
|
onToggleTree,
|
|
onToggleCompanion,
|
|
onNavigate,
|
|
onOpenPage,
|
|
onCreatePage,
|
|
onMetadata,
|
|
onInsertFavorite,
|
|
onExport,
|
|
}: {
|
|
page: NotePage | null
|
|
pages: NoteSummary[]
|
|
invalidParents: Set<string>
|
|
status: NoteSaveStatus
|
|
busy: boolean
|
|
embedded: boolean
|
|
treeOpen: boolean
|
|
companionOpen: boolean
|
|
onToggleTree: () => void
|
|
onToggleCompanion: () => void
|
|
onNavigate: (event: MouseEvent<HTMLAnchorElement>, id: string) => void
|
|
/** Opens another page from the page switcher, saving this one first. */
|
|
onOpenPage: (id: string) => void
|
|
onCreatePage: () => void
|
|
onMetadata: (patch: Partial<NotePage>) => void
|
|
onInsertFavorite: () => void
|
|
onExport: (format: 'md' | 'docx' | 'anki') => void
|
|
}) {
|
|
const { t } = useTranslation()
|
|
const feature = useShelfFeature('/co-writer')
|
|
const [open, setOpen] = useState(false)
|
|
const menu = useMenuPopover<HTMLDivElement>(open, setOpen)
|
|
const byId = new Map(pages.map(p => [p.id, p]))
|
|
const ancestors: NoteSummary[] = []
|
|
for (
|
|
let parent = page?.parent_id ? byId.get(page.parent_id) : undefined, guard = 0;
|
|
parent && guard < 12;
|
|
parent = parent.parent_id ? byId.get(parent.parent_id) : undefined, guard++
|
|
)
|
|
ancestors.unshift(parent)
|
|
// Deep pages keep their first and last ancestor; the middle folds away.
|
|
const shown = ancestors.length > 2 ? [ancestors[0], null, ancestors.at(-1)!] : ancestors
|
|
return (
|
|
<header className="dt-note-topbar">
|
|
<Tooltip label={t(treeOpen ? 'Hide pages' : 'Show pages')} side="bottom">
|
|
<button
|
|
className="dt-note-icon-button"
|
|
aria-label={t('Page tree')}
|
|
aria-expanded={treeOpen}
|
|
aria-controls="note-page-tree"
|
|
onClick={onToggleTree}
|
|
>
|
|
<PanelLeft size={16} strokeWidth={1.7} />
|
|
</button>
|
|
</Tooltip>
|
|
{/* 「笔记本 / … / this page」: the feature opens onto the home shelf,
|
|
the page onto every other page of the notebook. */}
|
|
<nav className="dt-note-breadcrumb" aria-label={t('Breadcrumb')}>
|
|
{!embedded && feature && (
|
|
<span className="dt-note-feature">
|
|
<FeatureCrumb section={feature.section} page={feature.page} />
|
|
</span>
|
|
)}
|
|
{shown.map((ancestor, i) => (
|
|
<span key={ancestor?.id ?? `gap-${i}`} className="contents">
|
|
{(i > 0 || (!embedded && feature)) && (
|
|
<span aria-hidden className="dt-crumb-sep">
|
|
/
|
|
</span>
|
|
)}
|
|
{ancestor ? (
|
|
<Crumb page={ancestor} onNavigate={onNavigate} />
|
|
) : (
|
|
<span className="dt-note-crumb is-gap">…</span>
|
|
)}
|
|
</span>
|
|
))}
|
|
{page && (
|
|
<span className="dt-note-current" aria-current="page">
|
|
<InstanceCrumb
|
|
label={
|
|
<>
|
|
{page.icon && <span aria-hidden>{page.icon} </span>}
|
|
{page.title || t('Untitled')}
|
|
</>
|
|
}
|
|
separator={shown.length > 0 || (!embedded && Boolean(feature))}
|
|
menuLabel={t('Notebook pages')}
|
|
currentId={page.id}
|
|
load={async () =>
|
|
[...pages]
|
|
.sort((a, b) => (b.updated_at ?? 0) - (a.updated_at ?? 0))
|
|
.map(entry => ({
|
|
id: entry.id,
|
|
label: `${entry.icon ? `${entry.icon} ` : ''}${entry.title || t('Untitled')}`,
|
|
updatedAt: entry.updated_at,
|
|
}))
|
|
}
|
|
onSelect={item => onOpenPage(item.id)}
|
|
create={{ label: t('New page'), onSelect: onCreatePage }}
|
|
all={embedded ? undefined : { label: t('All pages'), href: '/co-writer' }}
|
|
/>
|
|
{page.pinned && <Pin size={11} className="shrink-0 opacity-50" aria-label={t('Pinned page')} />}
|
|
</span>
|
|
)}
|
|
</nav>
|
|
<span
|
|
className={`dt-note-status ${status === 'Save failed' ? 'is-error' : ''} ${status === 'Saved' ? 'is-idle' : ''}`}
|
|
role="status"
|
|
>
|
|
{t(status)}
|
|
</span>
|
|
<button
|
|
className={`dt-note-ask ${companionOpen ? 'is-active' : ''}`}
|
|
aria-pressed={companionOpen}
|
|
onClick={onToggleCompanion}
|
|
>
|
|
<Sparkles size={14} strokeWidth={1.8} />
|
|
{t('Ask Dito')}
|
|
</button>
|
|
<div ref={menu} className="relative">
|
|
<Tooltip label={t('More')} side="bottom" suppressed={open}>
|
|
<button
|
|
className="dt-note-icon-button"
|
|
aria-label={t('More page actions')}
|
|
aria-haspopup="menu"
|
|
aria-expanded={open}
|
|
disabled={!page}
|
|
onClick={() => setOpen(!open)}
|
|
>
|
|
<MoreHorizontal size={16} strokeWidth={1.7} />
|
|
</button>
|
|
</Tooltip>
|
|
{open && page && (
|
|
<NotePageMenu
|
|
page={page}
|
|
pages={pages}
|
|
invalidParents={invalidParents}
|
|
busy={busy}
|
|
onClose={() => setOpen(false)}
|
|
onMetadata={onMetadata}
|
|
onInsertFavorite={onInsertFavorite}
|
|
onExport={onExport}
|
|
/>
|
|
)}
|
|
</div>
|
|
</header>
|
|
)
|
|
}
|