fix(view): Office 视图改用 sidebarRight.mounted 取会话,面板化渲染
问题:顶栏 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,不因会话打开而自动扫)
This commit is contained in:
1 parent
2480d1d8d5
commit
2ff0181d96
1 file changed
+122
-54
+122
-54
@@ -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<string | undefined>(undefined)
|
||||
const [loaded, setLoaded] = useState(false)
|
||||
const [reload, setReload] = useState(0)
|
||||
const [files, setFiles] = useState<OfficeFile[] | null>(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',
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user