dsh-brain/src-client/SettingsSummaryCard.tsx

65 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Slim settings card: status summary + a jump into the Brain total panel.
import * as React from 'react'
import { API, request, messageOf } from './api'
import { styles } from './styles'
import { Pill, Banner, Btn } from './components'
const h = React.createElement
export function SettingsSummaryCard(props: { ctx?: any }): any {
const [status, setStatus] = React.useState<any>(null)
const [error, setError] = React.useState<string | null>(null)
React.useEffect(() => {
let cancelled = false
const load = (): void => {
request(API.status).then(
(result) => {
if (cancelled) return
if (result.status === 200 && result.body !== null && result.body.ok === true) {
setStatus(result.body)
setError(null)
} else {
setError('读取状态失败(HTTP ' + String(result.status) + ')')
}
},
(failure) => { if (!cancelled) setError(messageOf(failure)) },
)
}
load()
const timer = setInterval(load, 15000)
return () => {
cancelled = true
clearInterval(timer)
}
}, [])
function openPanel(): void {
const ctx = props.ctx
const layout = ctx === undefined || ctx === null ? undefined : ctx.layout
if (layout !== undefined && layout !== null && typeof layout.selectPanel === 'function') {
layout.selectPanel('dsh-brain')
return
}
if (typeof window !== 'undefined' && typeof window.open === 'function') window.open('/dsh-brain')
}
const settings = status?.settings ?? {}
return h('div', { style: styles.root, 'data-dsh-plugin': 'dsh-brain' },
h('div', { style: styles.sectionHead },
h('h4', { style: styles.sectionTitle }, '云端记忆'),
h('div', { style: styles.actions },
h(Btn, { variant: 'primary', label: '打开总面板', onClick: openPanel }),
),
),
h('p', { style: styles.hint }, '设置与连接内容已收进左侧栏 Brain 总面板的「连接与上报」页签。这里保留状态摘要和快捷入口。'),
error === null ? null : h(Banner, { tone: 'bad' }, error),
status === null ? h('p', { style: styles.hint }, '正在读取状态…') : h('div', { style: styles.pills },
h(Pill, { label: '服务', value: settings.baseUrlConfigured === false ? '未配置' : '已配置', tone: settings.baseUrlConfigured === false ? 'warn' : 'ok' }),
h(Pill, { label: '上报', value: settings.activityEnabled === true ? '开' : '关', tone: settings.activityEnabled === true ? 'ok' : 'muted' }),
h(Pill, { label: '记忆', value: settings.memoryEnabled === true ? '开' : '关', tone: settings.memoryEnabled === true ? 'ok' : 'muted' }),
h(Pill, { label: '本地记忆', value: settings.localMemoryEnabled === false ? '关' : '开', tone: settings.localMemoryEnabled === false ? 'muted' : 'ok' }),
),
)
}