302 lines
13 KiB
TypeScript
302 lines
13 KiB
TypeScript
// dsh-brain client section component.
|
||
import * as React from 'react'
|
||
import { API, request, postJson, messageOf, bodyError } from '../api'
|
||
import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format'
|
||
import { styles } from '../styles'
|
||
import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components'
|
||
|
||
const h = React.createElement
|
||
|
||
export function ImportSection(props) {
|
||
var onImported = props && typeof props.onImported === 'function' ? props.onImported : null
|
||
var tuple = React.useState({ expanded: false, status: 'idle', sources: [], open: {}, busy: '', notice: null, imported: {}, search: '', selection: [], progress: null })
|
||
var s = tuple[0]
|
||
var set = tuple[1]
|
||
function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) }
|
||
function load() {
|
||
patch({ status: 'loading', notice: null })
|
||
request(API.skills + '/sources', { method: 'GET' }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
patch({ status: 'ready', sources: Array.isArray(res.body.sources) ? res.body.sources : [] })
|
||
} else patch({ status: 'error', notice: { tone: 'bad', text: bodyError(res, '读取来源失败') } })
|
||
}, function (error) { patch({ status: 'error', notice: { tone: 'bad', text: messageOf(error) } }) })
|
||
}
|
||
function openPanel() {
|
||
patch({ expanded: true })
|
||
if (s.status === 'idle' || s.status === 'error') load()
|
||
}
|
||
function closePanel() {
|
||
patch({ expanded: false })
|
||
}
|
||
function dshFolders() {
|
||
var found = {}
|
||
s.sources.forEach(function (source) {
|
||
if (!source.writable) return
|
||
var groups = source.groups || []
|
||
groups.forEach(function (group) {
|
||
var skills = group.skills || []
|
||
skills.forEach(function (skill) { found[skill.folder] = true })
|
||
})
|
||
})
|
||
return found
|
||
}
|
||
function toggleSource(sourceId) {
|
||
var next = Object.assign({}, s.open)
|
||
var open = s.open[sourceId] !== false
|
||
next[sourceId] = !open
|
||
patch({ open: next })
|
||
}
|
||
var mine = dshFolders()
|
||
var others = s.sources.filter(function (source) { return !source.writable })
|
||
var needle = String(s.search).trim().toLowerCase()
|
||
function skillsOf(source) {
|
||
var list = []
|
||
var groups = source.groups || []
|
||
groups.forEach(function (group) {
|
||
var items = group.skills || []
|
||
items.forEach(function (skill) { list.push(skill) })
|
||
})
|
||
return list
|
||
}
|
||
function importedYet(skill) {
|
||
return mine[skill.folder] === true || s.imported[skill.folder] === true
|
||
}
|
||
var candidates = []
|
||
others.forEach(function (source) {
|
||
if (!source.exists) return
|
||
skillsOf(source).forEach(function (skill) {
|
||
candidates.push({ source: source, skill: skill })
|
||
})
|
||
})
|
||
var matches = needle === '' ? candidates : candidates.filter(function (entry) {
|
||
var hay = String(entry.skill.name || '') + ' ' + String(entry.skill.folder || '') + ' ' + String(entry.skill.description || '')
|
||
return hay.toLowerCase().indexOf(needle) >= 0
|
||
})
|
||
var importable = matches.filter(function (entry) { return !importedYet(entry.skill) })
|
||
var alreadyCount = candidates.length - candidates.filter(function (entry) { return !importedYet(entry.skill) }).length
|
||
function keyOf(entry) {
|
||
return String(entry.source.id) + ':' + String(entry.skill.folder)
|
||
}
|
||
var chosen = s.selection
|
||
function isChosen(entry) {
|
||
return chosen.indexOf(keyOf(entry)) >= 0
|
||
}
|
||
function chooseMany(list, on) {
|
||
var next = chosen.slice()
|
||
list.forEach(function (entry) {
|
||
var key = keyOf(entry)
|
||
var at = next.indexOf(key)
|
||
if (on === true && at < 0) next.push(key)
|
||
if (on !== true && at >= 0) next.splice(at, 1)
|
||
})
|
||
patch({ selection: next })
|
||
}
|
||
var chosenEntries = candidates.filter(function (entry) {
|
||
return chosen.indexOf(keyOf(entry)) >= 0 && !importedYet(entry.skill)
|
||
})
|
||
function importSelected() {
|
||
var targets = chosenEntries
|
||
if (targets.length === 0) return Promise.resolve()
|
||
patch({ busy: 'batch', notice: null, progress: { done: 0, total: targets.length } })
|
||
var done = 0
|
||
var failures = []
|
||
var importedMap = Object.assign({}, s.imported)
|
||
var chain = Promise.resolve()
|
||
targets.forEach(function (entry) {
|
||
chain = chain.then(function () {
|
||
return postJson(API.skills + '/import', { sourceId: entry.source.id, folder: entry.skill.folder }).then(
|
||
function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
done += 1
|
||
importedMap[entry.skill.folder] = true
|
||
} else failures.push(String(entry.skill.name) + '(' + bodyError(res, '失败') + ')')
|
||
patch({ progress: { done: done + failures.length, total: targets.length } })
|
||
},
|
||
function (error) {
|
||
failures.push(String(entry.skill.name) + '(' + messageOf(error) + ')')
|
||
patch({ progress: { done: done + failures.length, total: targets.length } })
|
||
},
|
||
)
|
||
})
|
||
})
|
||
return chain.then(function () {
|
||
patch({
|
||
busy: '',
|
||
progress: null,
|
||
selection: [],
|
||
imported: importedMap,
|
||
notice: {
|
||
tone: failures.length === 0 ? 'ok' : 'warn',
|
||
text: '已导入 ' + String(done) + ' / ' + String(targets.length) + ' 个技能到 DSH' + (failures.length === 0 ? '。' : ';失败:' + failures.join('、')),
|
||
},
|
||
})
|
||
if (onImported !== null) onImported()
|
||
load()
|
||
})
|
||
}
|
||
if (!s.expanded) {
|
||
return h(
|
||
'div',
|
||
{ style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px', flexWrap: 'wrap' } },
|
||
h(Btn, {
|
||
label: '从其他软件导入',
|
||
ariaLabel: '从其他软件导入',
|
||
onClick: openPanel,
|
||
}),
|
||
s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text),
|
||
)
|
||
}
|
||
function describe(skill) {
|
||
var text = String(skill.description === undefined || skill.description === null ? '' : skill.description).trim()
|
||
if (/^[>|][+-]?$/.test(text)) return ''
|
||
return text
|
||
}
|
||
var blocks = others.map(function (source) {
|
||
var all = skillsOf(source)
|
||
var visible = matches.filter(function (entry) { return entry.source.id === source.id })
|
||
var open = s.open[source.id] !== false
|
||
var selectable = visible.filter(function (entry) { return !importedYet(entry.skill) })
|
||
return h('div', { key: source.id, style: styles.row },
|
||
h('div', { style: styles.rowHead },
|
||
h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: '8px', minWidth: 0 } },
|
||
h('span', { style: styles.sourceGlyph }, String(source.label).slice(0, 1)),
|
||
h('span', { style: styles.sourceTitle }, String(source.label)),
|
||
h(
|
||
'span',
|
||
{ style: styles.hint },
|
||
source.exists
|
||
? String(all.length) + ' 个技能' + (needle === '' ? '' : ' · 匹配 ' + String(visible.length)) + ' · 可导入 ' + String(selectable.length)
|
||
: '未安装',
|
||
),
|
||
),
|
||
source.exists
|
||
? h('div', { style: styles.actions },
|
||
h(Btn, {
|
||
label: '全选本来源(' + String(selectable.length) + ')',
|
||
ariaLabel: '全选本来源 ' + String(source.label),
|
||
variant: 'small',
|
||
disabled: selectable.length === 0 || s.busy !== '',
|
||
onClick: function () { chooseMany(visible, true) },
|
||
}),
|
||
h(Btn, { label: open ? '收起' : '展开', variant: 'small', onClick: function () { toggleSource(source.id) } }),
|
||
)
|
||
: null,
|
||
),
|
||
h('span', { style: styles.mono }, String(source.root)),
|
||
!source.exists || !open
|
||
? null
|
||
: visible.length === 0
|
||
? h('p', { style: styles.hint }, needle === '' ? '这个来源里还没有技能。' : '没有匹配「' + s.search + '」的技能。')
|
||
: h(
|
||
'div',
|
||
{ style: { display: 'flex', flexDirection: 'column', gap: '2px', maxHeight: '320px', overflow: 'auto' } },
|
||
visible.map(function (entry) {
|
||
var skill = entry.skill
|
||
var has = importedYet(skill)
|
||
return h(
|
||
'label',
|
||
{ key: String(skill.folder), style: styles.checkboxRow },
|
||
h('input', {
|
||
type: 'checkbox',
|
||
checked: has || isChosen(entry),
|
||
disabled: has || s.busy !== '',
|
||
'aria-label': '选择技能 ' + String(skill.name),
|
||
onChange: function (event) { chooseMany([entry], event.target.checked === true) },
|
||
}),
|
||
h(
|
||
'span',
|
||
{ style: { minWidth: 0, flex: '1 1 auto' } },
|
||
h('span', { style: styles.rowTitle }, String(skill.name)),
|
||
describe(skill) === '' ? null : h('span', { style: Object.assign({}, styles.hint, styles.clamp1) }, describe(skill)),
|
||
),
|
||
has ? h(Pill, { label: 'DSH', value: '已导入', tone: 'muted' }) : null,
|
||
)
|
||
}),
|
||
),
|
||
)
|
||
})
|
||
return h(
|
||
Section,
|
||
{
|
||
title: '从其他软件导入',
|
||
hint: '把其他软件技能目录里的技能复制一份到 DSH,原目录不动;同名技能不能重复导入。',
|
||
aside: h(
|
||
'div',
|
||
{ style: styles.actions },
|
||
h(Btn, { label: '刷新', variant: 'small', onClick: load }),
|
||
h(Btn, { label: '收起', variant: 'small', onClick: closePanel }),
|
||
),
|
||
},
|
||
s.status === 'loading' ? h('p', { style: styles.hint }, '正在读取来源…')
|
||
: others.length === 0 ? h('p', { style: styles.hint }, '没有检测到其他软件的技能目录。')
|
||
: h(
|
||
'div',
|
||
{ style: styles.root },
|
||
h(
|
||
'div',
|
||
{ style: styles.pills },
|
||
h(Pill, { label: '来源', value: String(others.length), tone: 'muted' }),
|
||
h(Pill, { label: '可导入', value: String(candidates.length - alreadyCount), tone: 'muted' }),
|
||
s.search === '' ? null : h(Pill, { label: '匹配', value: String(matches.length), tone: matches.length === 0 ? 'muted' : 'ok' }),
|
||
h(Pill, { label: '已选', value: String(chosenEntries.length), tone: chosenEntries.length === 0 ? 'muted' : 'ok' }),
|
||
alreadyCount === 0 ? null : h(Pill, { label: '已在 DSH', value: String(alreadyCount), tone: 'muted' }),
|
||
),
|
||
h(
|
||
'div',
|
||
{ style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } },
|
||
h('input', {
|
||
type: 'search',
|
||
style: Object.assign({}, styles.input, { flex: '1 1 220px', minWidth: 0 }),
|
||
value: s.search,
|
||
placeholder: '搜索技能:名称 / 目录 / 描述',
|
||
'aria-label': '搜索要导入的技能',
|
||
onChange: function (event) { patch({ search: event.target.value }) },
|
||
}),
|
||
s.search === '' ? null : h(Btn, {
|
||
label: '清空搜索',
|
||
variant: 'small',
|
||
onClick: function () { patch({ search: '' }) },
|
||
}),
|
||
),
|
||
h(
|
||
'div',
|
||
{ style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) },
|
||
h(Btn, {
|
||
label: '全选匹配可导入(' + String(importable.length) + ')',
|
||
ariaLabel: '全选匹配可导入',
|
||
variant: 'small',
|
||
disabled: importable.length === 0 || s.busy !== '',
|
||
onClick: function () { chooseMany(importable, true) },
|
||
}),
|
||
h(Btn, {
|
||
label: '全选全部(' + String(candidates.length - alreadyCount) + ')',
|
||
ariaLabel: '全选全部',
|
||
variant: 'small',
|
||
disabled: candidates.length - alreadyCount === 0 || s.busy !== '',
|
||
onClick: function () {
|
||
chooseMany(candidates.filter(function (entry) { return !importedYet(entry.skill) }), true)
|
||
},
|
||
}),
|
||
h(Btn, {
|
||
label: '清空已选',
|
||
ariaLabel: '清空已选',
|
||
variant: 'small',
|
||
disabled: chosenEntries.length === 0,
|
||
onClick: function () { patch({ selection: [] }) },
|
||
}),
|
||
h(Btn, {
|
||
variant: 'primary',
|
||
label: s.busy === 'batch' ? '导入中…' : '导入所选(' + String(chosenEntries.length) + ')',
|
||
ariaLabel: '导入所选',
|
||
disabled: s.busy !== '' || chosenEntries.length === 0,
|
||
onClick: importSelected,
|
||
}),
|
||
),
|
||
s.progress === null ? null : h('p', { style: styles.hint }, '正在导入:' + String(s.progress.done) + ' / ' + String(s.progress.total)),
|
||
h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, blocks),
|
||
),
|
||
s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text),
|
||
)
|
||
}
|
||
|