Compare commits

...
2 Commits
Author SHA1 Message Date
workbuddy-mac13 2ff0181d96 fix(view): Office 视图改用 sidebarRight.mounted 取会话,面板化渲染
CI / ci (push) Blocked by required conditions
CI / ci-windows (push) Blocked by required conditions
CI / plugin-mount (push) Blocked by required conditions
问题:顶栏 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,不因会话打开而自动扫)
2026-11-28 23:50:27 +08:00
workbuddy-mac13 2480d1d8d5 perf(view): Office 视图改为按需加载
宿主渲染会话 view 时会「恢复上次激活的 view」→ 我们的 Office view 在会话打开时
就会被挂载,而桥接扫目录要 1.2–2.7s,于是每个会话都要付一次扫描代价。

改为按需:首屏只显示「加载本会话的 Office 文档」按钮,点击后才请求
POST /office/api/files;列表出来��提供「刷新」按钮(reload 计数触发重取)。
编辑/预览仍全部交给原生右栏。
2026-11-28 21:12:39 +08:00
+129 -31
View File
@@ -6,10 +6,17 @@
* editor already live (the bridge moved them there in v0.7). This view only lists * 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. * and hands over — it never renders an editor of its own.
* *
* Registered as a `conversation.view`, so it appears in the session header tab * Which session the view belongs to: the host renders every `conversation.view`
* strip next to the host's own views (对话 / 上下文 / 编队 / 轨迹 …). The host * registration into the session header tab strip, but it passes NONE of the
* passes the session it renders; `order` places it after the context/fleet * session identity to the view component (only `inspectCall` / `viewRequest` /
* views (20 / 30) instead of at the front of the strip. * `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' import { createElement, useEffect, useState, type ReactNode } from 'react'
@@ -26,12 +33,18 @@ interface OfficeFile {
updatedAt?: string 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 { export interface OfficeViewProps {
sessionId?: string sessionId?: string
ctx: Context 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. * Read the file list out of the bridge's answer.
* *
@@ -56,18 +69,79 @@ function displayName(file: OfficeFile): string {
return '(unnamed)' 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. * The session's Office documents.
* *
* @param props - the host's session id and our client context. * @param props - the host's session id (the host sends none) and our client context.
* @returns the list view (loading / empty / rows). * @returns the list view (idle / loading / empty / rows).
*/ */
export function OfficeView(props: OfficeViewProps): ReactNode { export function OfficeView(props: OfficeViewProps): ReactNode {
const { sessionId, ctx } = props const { ctx } = props
const [sessionId, setSessionId] = useState<string | undefined>(undefined)
const [loaded, setLoaded] = useState(false)
const [reload, setReload] = useState(0)
const [files, setFiles] = useState<OfficeFile[] | null>(null) const [files, setFiles] = useState<OfficeFile[] | null>(null)
const [failed, setFailed] = useState(false) const [failed, setFailed] = useState(false)
// Follow the mounted seat: the host sends no session identity to the view.
useEffect(() => { 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) { if (sessionId === undefined) {
setFiles([]) setFiles([])
return return
@@ -93,42 +167,66 @@ export function OfficeView(props: OfficeViewProps): ReactNode {
return () => { return () => {
cancelled = true cancelled = true
} }
}, [sessionId]) }, [loaded, reload, sessionId])
/** Open the file in the native right sidebar (preview / edit live there). */ /** Open the file in the native right sidebar (preview / edit live there). */
function open(file: OfficeFile): void { function open(file: OfficeFile): void {
if (sessionId === undefined || typeof file.path !== 'string') return if (sessionId === undefined || typeof file.path !== 'string') return
const surface = ctx.get('sidebarRight') as const surface = ctx.get('sidebarRight') as SidebarRightFace | undefined
| { openResource?: (input: { sessionId: string; address: string; revealIfOpened: boolean }) => void }
| undefined
const address = fileAddressFor(sessionId, addressBaseOf(ctx, sessionId), file.path) 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 panel(action('加载本会话的 Office 文档', () => setLoaded(true)), 'idle')
}
if (files === null) { 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) { if (files.length === 0) {
return createElement( return panel(
'div', createElement(
{ className: 'apesi-office-view', 'data-apesi-office': failed ? 'failed' : 'empty' }, 'span',
failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档', { style: { opacity: 0.7 } },
failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档',
),
failed ? 'failed' : 'empty',
) )
} }
return createElement( return panel(
'div', [
{ className: 'apesi-office-view', 'data-apesi-office': 'list' },
files.map((file, index) =>
createElement( createElement(
'button', 'div',
{ { style: { display: 'flex', gap: '8px', alignItems: 'center' } },
key: `${file.path ?? ''}:${index}`, action('刷新', () => {
type: 'button', setFiles(null)
className: 'apesi-office-row', setReload(value => value + 1)
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',
) )
} }