From 2ff0181d969e26ad7d975ba0edf54fd317f6d8c3 Mon Sep 17 00:00:00 2001 From: workbuddy-mac13 Date: Sat, 28 Nov 2026 23:50:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(view):=20Office=20=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E6=94=B9=E7=94=A8=20sidebarRight.mounted=20=E5=8F=96=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=EF=BC=8C=E9=9D=A2=E6=9D=BF=E5=8C=96=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:顶栏 Office 标签下只挂着一个像 tooltip 的小按钮,主内容区空。 根因(宿主契约): - 宿主渲染 conversation.view 时只传 {inspectCall, viewRequest, openView, completeViewRequest},**不传 sessionId**;DOM 上也没有 session 属性 → 组件拿不到会话,列表必然为空 - 宿主没有 activeSessionId;官方规范是 **ctx.sidebarRight.mounted** (DSH 0.1.7 起 session 列表不再有 current 字段) 修复: - 订阅 ctx.get('sidebarRight').mounted 取当前会话 id,切换会话时重置按需加载状态 - 点击文档改用 native controller 的 openResource(address, {revealIfOpened:true}) (原先的调用形状不对) - 外层从裸 button 改为带 padding 与 --dsw-* token 的面板容器,读取为页面而非浮层 - 保留按需加载(bridge 扫目录 1.2–2.7s,不因会话打开而自动扫) --- src/client/office-view.tsx | 176 +++++++++++++++++++++++++------------ 1 file changed, 122 insertions(+), 54 deletions(-) diff --git a/src/client/office-view.tsx b/src/client/office-view.tsx index 72d902cc..f42626e0 100644 --- a/src/client/office-view.tsx +++ b/src/client/office-view.tsx @@ -6,10 +6,17 @@ * editor already live (the bridge moved them there in v0.7). This view only lists * and hands over — it never renders an editor of its own. * - * Registered as a `conversation.view`, so it appears in the session header tab - * strip next to the host's own views (对话 / 上下文 / 编队 / 轨迹 …). The host - * passes the session it renders; `order` places it after the context/fleet - * views (20 / 30) instead of at the front of the strip. + * Which session the view belongs to: the host renders every `conversation.view` + * registration into the session header tab strip, but it passes NONE of the + * session identity to the view component (only `inspectCall` / `viewRequest` / + * `openView` / `completeViewRequest`). The sanctioned feed for "which session's + * seat is on screen" is `ctx.sidebarRight.mounted` — the session list carries no + * "current" id since DSH 0.1.7 — so the view reads the mounted seat and follows + * it when the user switches conversations. + * + * The list is pulled ON DEMAND: the bridge scans the session's workspace, which + * takes seconds. The host restores the last active view when a session opens, + * so a view that fetched on mount would pay that scan for every session. */ import { createElement, useEffect, useState, type ReactNode } from 'react' @@ -26,12 +33,18 @@ interface OfficeFile { updatedAt?: string } -/** The view's props: the host's session plus our own client context. */ +/** The view's props: the host's session id (unused — the host sends none) and our client context. */ export interface OfficeViewProps { sessionId?: string ctx: Context } +/** The slice of the native right-sidebar controller this view needs. */ +interface SidebarRightFace { + mounted?: { getSnapshot(): string | undefined; subscribe(listener: () => void): () => void } + openResource?(address: string, options?: { revealIfOpened?: boolean; preferNewPane?: boolean }): void +} + /** * Read the file list out of the bridge's answer. * @@ -56,23 +69,77 @@ function displayName(file: OfficeFile): string { return '(unnamed)' } +/** The view's shell: a padded panel with a heading, so it reads as a page, not a tooltip. */ +function panel(children: ReactNode, state: string): ReactNode { + return createElement( + 'div', + { + className: 'apesi-office-view', + 'data-apesi-office': state, + style: { + padding: '12px 16px', + display: 'flex', + flexDirection: 'column', + gap: '8px', + alignItems: 'flex-start', + color: 'var(--dsw-alias-label-primary, inherit)', + font: 'var(--dsw-font-body, inherit)', + }, + }, + children, + ) +} + +/** A plain action button in the plugin's own skin tokens. */ +function action(label: string, onClick: () => void): ReactNode { + return createElement( + 'button', + { + type: 'button', + onClick, + style: { + padding: '6px 14px', + borderRadius: '6px', + border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.35))', + background: 'var(--dsw-alias-bg-container, transparent)', + color: 'var(--dsw-alias-label-primary, inherit)', + font: 'inherit', + cursor: 'pointer', + }, + }, + label, + ) +} + /** * The session's Office documents. * - * @param props - the host's session id and our client context. - * @returns the list view (loading / empty / rows). + * @param props - the host's session id (the host sends none) and our client context. + * @returns the list view (idle / loading / empty / rows). */ export function OfficeView(props: OfficeViewProps): ReactNode { - const { sessionId, ctx } = props - // The list is pulled ON DEMAND: the bridge scans the session's workspace, - // which takes seconds. The host renders a session's active view on open - // (it restores the last active view), so a view that fetched on mount - // would pay that scan for every session the user opens. + const { ctx } = props + const [sessionId, setSessionId] = useState(undefined) const [loaded, setLoaded] = useState(false) const [reload, setReload] = useState(0) const [files, setFiles] = useState(null) const [failed, setFailed] = useState(false) + // Follow the mounted seat: the host sends no session identity to the view. + useEffect(() => { + const surface = ctx.get('sidebarRight') as SidebarRightFace | undefined + const mounted = surface?.mounted + if (mounted === undefined) return + setSessionId(mounted.getSnapshot()) + return mounted.subscribe(() => { + const next = mounted.getSnapshot() + setSessionId(previous => (previous === next ? previous : next)) + // A different conversation means a different document set. + setLoaded(false) + setFiles(null) + }) + }, [ctx]) + useEffect(() => { if (!loaded) return if (sessionId === undefined) { @@ -105,60 +172,61 @@ export function OfficeView(props: OfficeViewProps): ReactNode { /** Open the file in the native right sidebar (preview / edit live there). */ function open(file: OfficeFile): void { if (sessionId === undefined || typeof file.path !== 'string') return - const surface = ctx.get('sidebarRight') as - | { openResource?: (input: { sessionId: string; address: string; revealIfOpened: boolean }) => void } - | undefined + const surface = ctx.get('sidebarRight') as SidebarRightFace | undefined const address = fileAddressFor(sessionId, addressBaseOf(ctx, sessionId), file.path) - surface?.openResource?.({ sessionId, address, revealIfOpened: true }) + surface?.openResource?.(address, { revealIfOpened: true }) } + if (sessionId === undefined) { + return panel(createElement('span', { style: { opacity: 0.7 } }, '没有已挂载的会话'), 'no-session') + } if (!loaded) { - return createElement( - 'div', - { className: 'apesi-office-view', 'data-apesi-office': 'idle' }, - createElement( - 'button', - { type: 'button', onClick: () => setLoaded(true) }, - '加载本会话的 Office 文档', - ), - ) + return panel(action('加载本会话的 Office 文档', () => setLoaded(true)), 'idle') } if (files === null) { - return createElement('div', { className: 'apesi-office-view', 'data-apesi-office': 'loading' }, '加载中…') + return panel(createElement('span', { style: { opacity: 0.7 } }, '加载中…'), 'loading') } if (files.length === 0) { - return createElement( - 'div', - { className: 'apesi-office-view', 'data-apesi-office': failed ? 'failed' : 'empty' }, - failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档', + return panel( + createElement( + 'span', + { style: { opacity: 0.7 } }, + failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档', + ), + failed ? 'failed' : 'empty', ) } - return createElement( - 'div', - { className: 'apesi-office-view', 'data-apesi-office': 'list' }, - createElement( - 'button', - { - type: 'button', - className: 'apesi-office-refresh', - onClick: () => { + return panel( + [ + createElement( + 'div', + { style: { display: 'flex', gap: '8px', alignItems: 'center' } }, + action('刷新', () => { setFiles(null) setReload(value => value + 1) - }, - }, - '刷新', - ), - files.map((file, index) => - createElement( - 'button', - { - key: `${file.path ?? ''}:${index}`, - type: 'button', - className: 'apesi-office-row', - onClick: () => open(file), - }, - createElement('span', { className: 'apesi-office-name' }, displayName(file)), + }), ), - ), + ...files.map((file, index) => + createElement( + 'button', + { + key: `${file.path ?? ''}:${index}`, + type: 'button', + onClick: () => open(file), + style: { + padding: '4px 8px', + border: 'none', + background: 'transparent', + color: 'var(--dsw-alias-label-primary, inherit)', + font: 'inherit', + textAlign: 'left', + cursor: 'pointer', + }, + }, + displayName(file), + ), + ), + ], + 'list', ) }