dsh-brain/src-client/sidebar/ProjectMemoryView.tsx

53 lines
2.4 KiB
TypeScript

import * as React from 'react'
import { postJson } from '../api'
import { Btn } from '../components'
import { useApi } from '../panel/useApi'
import { DepartmentTab } from '../panel/DepartmentTab'
import { MemoryTab } from '../panel/MemoryTab'
import panel from '../panel/BrainPanel.module.css'
import styles from './ProjectMemory.module.css'
const h = React.createElement
const TABS = [
{ id: 'memory', label: '记忆与文档' },
{ id: 'departments', label: '部门' },
] as const
export function ProjectMemoryView(props: { sessionId?: string; visible?: boolean }): any {
const [tab, setTab] = React.useState<(typeof TABS)[number]['id']>('memory')
const { sessionId } = props
const directory = useApi(async () => {
const result = await postJson('/sidebar/api/session.cwd', { sessionId })
const value = result.body?.value
const cwd = typeof value?.cwd === 'string' ? value.cwd.trim() : ''
if (result.status !== 200 || result.body?.ok !== true || value?.sessionId !== sessionId || cwd === '') {
throw new Error('无法确认当前会话的项目目录。')
}
return cwd
}, { visible: props.visible !== false && sessionId !== undefined })
const workspace = directory.error === null ? directory.data ?? undefined : undefined
return h('div', { className: styles.betterSidebarSeat, 'data-dsh-plugin': 'dsh-brain', 'data-dsh-part': 'better-sidebar-memory' },
h('div', { className: styles.inner },
workspace === undefined ? h('div', null,
h('p', { className: styles.hint }, directory.error ?? '等待当前会话的项目目录。'),
directory.error === null ? null : h(Btn, { label: '重试', variant: 'small', onClick: directory.reload }),
) : h(React.Fragment, null,
h('p', { className: styles.workspace, title: workspace }, workspace),
h('div', { className: panel.tabBar, role: 'tablist', 'aria-label': '项目记忆页签' },
TABS.map((item) => h('button', {
key: item.id,
type: 'button',
role: 'tab',
'aria-selected': tab === item.id,
className: tab === item.id ? panel.tab + ' ' + panel.tabActive : panel.tab,
onClick: () => setTab(item.id),
}, item.label)),
),
tab === 'memory'
? h(MemoryTab, { scope: 'project', workspace, key: workspace, active: props.visible })
: h(DepartmentTab, { scope: 'project', workspace, key: workspace, active: props.visible }),
),
),
)
}