Files
apes-sidebar/src/client/SidechatImages.tsx
T
Menghuan1918 72f79f7bd6 refactor(ui): 侧边对话的附件与图片状态接入统一组件层
- SidechatImages:加载态改用 Notice(inline, loading),不可用改用 StatusLine(plain, role=status);
  缩略图仍是 blob URL 的 <a target=_blank><img>,行为不变。
- SidechatAttachments:读取失败的占位改用 StatusLine(plain);文件附件从手写 chip 换成
  ListRow(leading=FileTypeIcon、title=name、meta=size),外层 span 只保留宽度约束与悬浮 title。
- SideChatView.module.css:删除随之失效的 .sidechatAttachmentFailed / .sidechatAttachmentName /
  .sidechatAttachmentSize,.sidechatAttachmentFile 收成布局壳。
- 行为、文案、可访问性名称与 data-* 未变;测试断言零改动。

缺口(未在 ui/** 造件,按任务约束只报告):非交互的文件 chip 仍缺共享件,
当前由 SidechatAttachments.FileChip 与 SideChatView.tsx 的附件卡手写两处。
2026-11-25 00:58:06 +08:00

37 lines
2.4 KiB
TypeScript

import { useEffect, useState } from 'react'
import type { SidechatImageOccurrence } from '../sidechat-images.ts'
import { hostRouteUrl } from './host-route-url.ts'
import { t } from './locales.ts'
import { Notice, StatusLine } from './ui/index.ts'
import css from './SideChatView.module.css'
function SavedImage({ childId, messageId, image, visible }: { childId: string; messageId: string; image: SidechatImageOccurrence; visible: boolean }): React.ReactNode {
const [state, setState] = useState<{ url: string } | { unavailable: true } | null>(null)
useEffect(() => {
if (!visible) return
const controller = new AbortController()
let url: string | undefined
setState(null)
const params = new URLSearchParams({ childId, messageId, blockIndex: String(image.blockIndex) })
void fetch(hostRouteUrl(`sidebar/sidechat-image?${params}`).href, { credentials: 'include', signal: controller.signal })
.then(async response => {
if (!response.ok) throw new Error('Image unavailable')
const blob = await response.blob()
if (controller.signal.aborted) return
url = URL.createObjectURL(blob)
setState({ url })
}).catch(() => { if (!controller.signal.aborted) setState({ unavailable: true }) })
return () => { controller.abort(); if (url !== undefined) URL.revokeObjectURL(url) }
}, [childId, messageId, image.blockIndex, visible])
if (state === null) return <Notice kind="loading" tone="inline">{t('sideChatImageLoading')}</Notice>
if ('unavailable' in state) return <StatusLine plain role="status" text={t('sideChatImageUnavailable')} />
return <a href={state.url} target="_blank" rel="noreferrer"><img className={css.sidechatImage} src={state.url} alt={image.name ?? t('sideChatImage')} onError={() => { setState({ unavailable: true }) }} /></a>
}
export function SidechatImages({ childId, messageId, images, visible }: { childId?: string; messageId?: string; images?: readonly SidechatImageOccurrence[]; visible: boolean }): React.ReactNode {
if (images === undefined || images.length === 0) return null
return <div className={css.sidechatImages}>{images.map(image => childId === undefined || messageId === undefined
? <StatusLine key={image.blockIndex} plain text={t('sideChatImageUnavailable')} />
: <SavedImage key={`${messageId}:${image.blockIndex}`} childId={childId} messageId={messageId} image={image} visible={visible} />)}</div>
}