165 lines
10 KiB
TypeScript
165 lines
10 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 MarketSection(props) {
|
||
var capabilities = props.capabilities || {}
|
||
var tuple = React.useState({
|
||
providers: null, provider: '', query: '', entries: [], nextCursor: null, cursor: 0,
|
||
status: 'idle', error: null, detail: null, plan: null, selected: {}, busy: '', notice: null,
|
||
})
|
||
var state = tuple[0]
|
||
var setState = tuple[1]
|
||
function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) }
|
||
var available = capabilities.features !== undefined && capabilities.features.skillsMarketplace === true
|
||
|
||
React.useEffect(function () {
|
||
if (available) loadProviders()
|
||
}, [])
|
||
|
||
function loadProviders() {
|
||
request(API.market + '/providers', { method: 'GET' }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
var list = Array.isArray(res.body.providers) ? res.body.providers : []
|
||
patch({ providers: list, provider: state.provider !== '' ? state.provider : (list.length > 0 ? list[0].id : '') })
|
||
} else {
|
||
patch({ error: bodyError(res, '读取市场来源失败') })
|
||
}
|
||
}, function (error) { patch({ error: messageOf(error) }) })
|
||
}
|
||
function search(append) {
|
||
if (state.provider === '') return
|
||
var cursor = append === true ? state.cursor : 0
|
||
patch({ busy: 'search', error: null, status: 'loading' })
|
||
var url = API.market + '/search?provider=' + encodeURIComponent(state.provider) + '&q=' + encodeURIComponent(state.query) + '&cursor=' + String(cursor)
|
||
request(url, { method: 'GET' }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
var entries = append === true ? state.entries.concat(res.body.entries || []) : (res.body.entries || [])
|
||
patch({ entries: entries, nextCursor: res.body.nextCursor === undefined ? null : res.body.nextCursor, cursor: res.body.nextCursor === undefined ? cursor : res.body.nextCursor, status: 'ready', busy: '', detail: null, plan: null })
|
||
} else {
|
||
patch({ status: 'error', busy: '', error: bodyError(res, '市场搜索失败') })
|
||
}
|
||
}, function (error) { patch({ status: 'error', busy: '', error: messageOf(error) }) })
|
||
}
|
||
function openDetail(entry) {
|
||
patch({ busy: 'detail', detail: null, plan: null, notice: null })
|
||
postJson(API.market + '/detail', { provider: state.provider, id: entry.id }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ detail: res.body, busy: '' })
|
||
else patch({ busy: '', error: bodyError(res, '读取条目详情失败') })
|
||
}, function (error) { patch({ busy: '', error: messageOf(error) }) })
|
||
}
|
||
function makePlan() {
|
||
if (state.detail === null) return
|
||
patch({ busy: 'plan', plan: null, notice: null })
|
||
postJson(API.market + '/plan', { provider: state.provider, id: state.detail.id }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
var selected = {}
|
||
;(res.body.candidates || []).forEach(function (candidate) {
|
||
if (candidate.conflict !== true && candidate.problem === undefined) selected[candidate.folder] = true
|
||
})
|
||
patch({ plan: res.body, selected: selected, busy: '' })
|
||
} else patch({ busy: '', error: bodyError(res, '无法生成安装计划') })
|
||
}, function (error) { patch({ busy: '', error: messageOf(error) }) })
|
||
}
|
||
function toggleFolder(folder) {
|
||
var next = Object.assign({}, state.selected)
|
||
next[folder] = next[folder] !== true
|
||
patch({ selected: next })
|
||
}
|
||
function commitPlan() {
|
||
if (state.plan === null) return
|
||
var folders = Object.keys(state.selected).filter(function (folder) { return state.selected[folder] === true })
|
||
if (folders.length === 0) { patch({ notice: { tone: 'warn', text: '请至少选中一个技能。' } }); return }
|
||
patch({ busy: 'commit' })
|
||
postJson(API.market + '/commit', { planId: state.plan.planId, folders: folders }).then(function (res) {
|
||
if (res.body !== null && res.body !== undefined && res.body.ok === true) {
|
||
var skipped = Array.isArray(res.body.skipped) ? res.body.skipped.length : 0
|
||
patch({ busy: '', plan: null, detail: null, notice: { tone: 'ok', text: '已安装:' + ((res.body.installed || []).join('、') || '无') + (skipped > 0 ? ';跳过 ' + skipped + ' 项' : '') } })
|
||
if (typeof props.onInstalled === 'function') props.onInstalled()
|
||
} else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '安装失败') } })
|
||
}, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) })
|
||
}
|
||
|
||
if (!available) {
|
||
return h(Section, { title: '技能市场', hint: '本版没有配置经验证的市场来源。' },
|
||
h('p', { style: styles.hint }, '能力缺口:capabilities.features.skillsMarketplace = false,因此不显示任何搜索框或示例条目。'))
|
||
}
|
||
var providerLabel = state.provider
|
||
if (state.providers !== null) {
|
||
state.providers.forEach(function (provider) { if (provider.id === state.provider) providerLabel = provider.label })
|
||
}
|
||
var resultRows = state.entries.map(function (entry) {
|
||
return h('div', { key: entry.id, style: styles.row },
|
||
h('div', { style: styles.rowHead },
|
||
h('span', { style: styles.rowTitle }, entry.name),
|
||
h('div', { style: styles.pills },
|
||
entry.version === undefined ? null : h(Pill, { label: '版本', value: String(entry.version), tone: 'muted' }),
|
||
h(Pill, { label: '技能', value: (entry.skills || []).length, tone: 'muted' }),
|
||
entry.author === undefined ? null : h(Pill, { label: '作者', value: String(entry.author), tone: 'muted' }),
|
||
),
|
||
),
|
||
h('p', { style: styles.hint }, entry.description === '' ? '(无描述)' : entry.description),
|
||
h('div', { style: styles.actions }, h(Btn, { label: '查看详情', variant: 'small', disabled: state.busy !== '', onClick: function () { openDetail(entry) } })),
|
||
)
|
||
})
|
||
var detailBox = null
|
||
if (state.detail !== null) {
|
||
var details = Array.isArray(state.detail.skillDetails) ? state.detail.skillDetails : []
|
||
detailBox = h('div', { style: styles.nested },
|
||
h('p', { style: styles.hint }, '来源:' + String(state.detail.sourceRepo) + ' · ' + (state.detail.description || '')),
|
||
h('div', { style: { display: 'flex', flexDirection: 'column', gap: '4px' } },
|
||
details.map(function (skill) {
|
||
return h('div', { key: skill.folder, style: styles.rowHead },
|
||
h('span', { style: styles.rowTitle }, skill.name === undefined ? skill.folder : skill.name),
|
||
h('span', { style: styles.hint }, skill.ok === false ? '(无法读取 SKILL.md)' : String(skill.description || '')),
|
||
)
|
||
}),
|
||
),
|
||
h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'plan' ? '拉取中…' : '拉取安装计划', disabled: state.busy !== '', onClick: makePlan })),
|
||
)
|
||
}
|
||
var planBox = null
|
||
if (state.plan !== null) {
|
||
var candidates = Array.isArray(state.plan.candidates) ? state.plan.candidates : []
|
||
planBox = h('div', { style: styles.nested },
|
||
h('p', { style: styles.hint }, '已固定提交:' + (state.plan.commit === undefined ? '克隆时记录实际提交' : String(state.plan.commit).slice(0, 12)) + ';只安装勾选项,冲突项默认不勾,不执行仓库脚本。'),
|
||
candidates.length === 0 ? h('p', { style: styles.hint }, '该条目没有可安装的技能。') : null,
|
||
h('div', { style: { display: 'flex', flexDirection: 'column', gap: '6px' } },
|
||
candidates.map(function (candidate) {
|
||
return h('label', { key: candidate.folder, style: styles.checkboxRow },
|
||
h('input', { type: 'checkbox', checked: state.selected[candidate.folder] === true, disabled: candidate.conflict === true || candidate.problem !== undefined, onChange: function () { toggleFolder(candidate.folder) } }),
|
||
h('span', null, candidate.folder + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在,跳过' : '') + (candidate.problem === undefined ? '' : ' · ' + candidate.problem)),
|
||
)
|
||
}),
|
||
),
|
||
h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'commit' ? '安装中…' : '安装选中', disabled: state.busy !== '', onClick: commitPlan })),
|
||
)
|
||
}
|
||
return h(Section, { title: '技能市场', hint: '来自经验证的公开市场清单;安装走暂存计划、只装勾选项并固定提交。' },
|
||
h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } },
|
||
h('div', { style: styles.pills },
|
||
h(Pill, { label: '来源', value: String(providerLabel || '未配置'), tone: 'muted' }),
|
||
state.providers === null ? null : h(Pill, { label: '已接入', value: state.providers.length, tone: 'ok' }),
|
||
),
|
||
h('div', { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } },
|
||
h('div', { style: { flex: '1 1 220px', minWidth: 0 } }, h(Field, { label: '搜索市场', value: state.query, placeholder: '例如 xlsx / pdf / design', onChange: function (value) { patch({ query: value }) } })),
|
||
h(Btn, { variant: 'primary', label: state.busy === 'search' ? '搜索中…' : '搜索', disabled: state.busy !== '', onClick: function () { search(false) } }),
|
||
),
|
||
state.error === null ? null : h(Banner, { tone: 'bad' }, state.error),
|
||
state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text),
|
||
state.status === 'ready' && state.entries.length === 0 ? h('p', { style: styles.hint }, '没有匹配的市场条目。') : null,
|
||
h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, resultRows),
|
||
state.nextCursor === null ? null : h('div', { style: styles.actions }, h(Btn, { label: '加载更多', variant: 'small', disabled: state.busy !== '', onClick: function () { search(true) } })),
|
||
detailBox,
|
||
planBox,
|
||
),
|
||
)
|
||
}
|
||
|
||
// ---- P3: interactive sync conflict resolution -------------------------
|