// 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 SyncResolveSection(props) { var tuple = React.useState({ status: 'idle', data: null, busy: '', error: null, notice: null, pendingAll: null }) var state = tuple[0] var setState = tuple[1] function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } React.useEffect(function () { load() }, []) function load() { patch({ status: 'loading', error: null }) request(API.sync + '/status', { method: 'GET' }).then(function (res) { if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ status: 'ready', data: res.body }) else patch({ status: 'error', error: bodyError(res, '读取同步状态失败') }) }, function (error) { patch({ status: 'error', error: messageOf(error) }) }) } function resolve(name, action, conflictId) { patch({ busy: name + ':' + action, notice: null }) postJson(API.sync + '/resolve', { name: name, action: action, conflictId: conflictId }).then(function (res) { if (res.body !== null && res.body !== undefined && res.body.ok === true) { var stats = state.data === null ? null : state.data.stats patch({ busy: '', data: { stats: stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: name + ':' + (res.body.note || '已处理') } }) if (typeof props.onResolved === 'function') props.onResolved() } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '处理冲突失败') } }) }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) } function runNow() { patch({ busy: 'run' }) postJson(API.sync + '/run', {}).then(function (res) { if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ busy: '', data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: '已执行一次同步。' } }) else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '同步失败') } }) }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) } function resolveAll(action) { if (conflicts.length === 0) { patch({ pendingAll: null }); return } patch({ busy: 'all:' + action, notice: null }) postJson(API.sync + '/resolve-all', { action: action, conflictIds: conflicts.map(function (item) { return item.conflictId }) }).then(function (res) { if (res.body !== null && res.body !== undefined && res.body.ok === true) { var resolved = Array.isArray(res.body.resolved) ? res.body.resolved : [] var failed = Array.isArray(res.body.failed) ? res.body.failed : [] var text = '批量处理完成:成功 ' + resolved.length + ' 项' + (failed.length === 0 ? '。' : ',失败 ' + failed.length + ' 项(' + failed.map(function (item) { return item.name + ':' + item.error }).join('、') + ')。') patch({ busy: '', pendingAll: null, data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: failed.length === 0 ? 'ok' : 'warn', text: text } }) if (typeof props.onResolved === 'function') props.onResolved() } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '批量处理冲突失败') } }) }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) } var data = state.data var stats = data === null || data === undefined ? null : data.stats var conflicts = data === null || data === undefined || !Array.isArray(data.conflicts) ? [] : data.conflicts var statPills = stats === null ? h('p', { style: styles.hint }, '同步引擎未启用或尚未运行。') : h('div', { style: styles.pills }, h(Pill, { label: '运行中', value: stats.running === true ? '是' : '否', tone: stats.running === true ? 'warn' : 'muted' }), h(Pill, { label: '上次运行', value: formatIso(stats.lastRunAt), tone: 'muted' }), h(Pill, { label: '上传', value: (stats.uploaded || []).length, tone: 'muted' }), h(Pill, { label: '下载', value: (stats.downloaded || []).length, tone: 'muted' }), h(Pill, { label: '冲突', value: conflicts.length, tone: conflicts.length > 0 ? 'warn' : 'muted' }), ) var conflictRows = conflicts.map(function (conflict) { var local = conflict.local || {} var cloud = conflict.cloud return h('div', { key: conflict.name, style: styles.row }, h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, conflict.name), h('div', { style: styles.pills }, h(Pill, { label: '原因', value: String(conflict.reason), tone: 'warn' }), h(Pill, { label: '本地', value: local.exists === true ? (local.files + ' 文件') : '缺失', tone: local.exists === true ? 'muted' : 'bad' }), h(Pill, { label: '云端', value: cloud === null ? '无' : ('rev ' + String(cloud.revision === undefined ? '?' : cloud.revision)), tone: cloud === null ? 'muted' : 'ok' }), ), ), h('p', { style: styles.hint }, conflict.note), h('div', { style: styles.actions }, h(Btn, { label: '保留本地', variant: 'small', disabled: state.busy !== '' || conflict.canKeepLocal !== true, onClick: function () { resolve(conflict.name, 'keep_local', conflict.conflictId) } }), h(Btn, { label: '采用云端', variant: 'small', disabled: state.busy !== '' || conflict.canTakeCloud !== true, onClick: function () { resolve(conflict.name, 'take_cloud', conflict.conflictId) } }), h(Btn, { label: '手动合并', variant: 'small', disabled: state.busy !== '', onClick: function () { resolve(conflict.name, 'manual', conflict.conflictId) } }), ), ) }) var batchRow = null if (conflicts.length > 0) { batchRow = h('div', { style: styles.actions }, h(Btn, { label: '全部保留本地', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canKeepLocal === true }), onClick: function () { patch({ pendingAll: 'keep_local', notice: null }) } }), h(Btn, { label: '全部采用云端', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canTakeCloud === true }), onClick: function () { patch({ pendingAll: 'take_cloud', notice: null }) } }), ) } var confirmAllRow = null if (state.pendingAll !== null && conflicts.length > 0) { var pendingText = state.pendingAll === 'keep_local' ? '把全部本地版本上传为新版本' : '用云端版本覆盖全部本地(覆盖前自动备份)' confirmAllRow = h('div', { style: styles.nested }, h('p', { style: styles.hint }, '确认' + pendingText + '?共 ' + conflicts.length + ' 项。'), h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'all:keep_local' || state.busy === 'all:take_cloud' ? '处理中…' : '确认批量', disabled: state.busy !== '', onClick: function () { resolveAll(state.pendingAll) } }), h(Btn, { variant: 'small', label: '取消', disabled: state.busy !== '', onClick: function () { patch({ pendingAll: null }) } }), ), ) } return h(Section, { title: '云端同步与冲突', hint: '同步先比内容与更新时间:一致直接认领,不同则自动采用较新的一边(覆盖云端前先备份本地);只有时间相同/缺失或目录结构不支持时才逐项询问,也可用批量按钮一次处理。', aside: h(Btn, { label: state.busy === 'run' ? '同步中…' : '立即同步', variant: 'small', disabled: state.busy !== '', onClick: runNow }) }, h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), statPills, batchRow, confirmAllRow, conflicts.length === 0 ? h('p', { style: styles.hint }, '当前没有待处理冲突。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, conflictRows), ), ) } // ---- P4: custom connection / edit / scope / policy --------------------- // The MCP marketplace section was removed in 0.9.6: the host's `catalog` // method answers 405 on current dsh-mcp-connector builds, so the card only // rendered an error banner. Discover/install stays in the host's own // plugin market instead of a broken embedded panel.