Skip to content

fix(preview): use static Message API to prevent addInstance crash in OfficeDocViewer#1781

Merged
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-AP
Mar 29, 2026
Merged

fix(preview): use static Message API to prevent addInstance crash in OfficeDocViewer#1781
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-AP

Conversation

@kaizhou-lab
Copy link
Copy Markdown
Collaborator

Summary

  • Replace hook-based useMessage ref with static Message.error() in OfficeDocViewer's loadDocument error handler to prevent TypeError: Cannot read properties of undefined (reading 'addInstance')
  • Remove unused messageApiRef / useRef import — the hook-based API only works when messageContextHolder is mounted, which is not the case during loading/error render paths
  • Add 3 unit tests covering conversion failure (static API call), error text display, and successful markdown rendering

Changes

src/renderer/pages/conversation/Preview/components/viewers/OfficeDocViewer.tsx

  • Remove messageApiRef (useRef) and its sync useEffect
  • Replace messageApiRef.current?.error?.(errorMessage) with Message.error(errorMessage) in the catch block
  • Remove unused useRef import

tests/unit/OfficeDocViewer.dom.test.tsx (new)

  • Test: static Message.error is called when conversion fails
  • Test: error text is shown in the UI on failure
  • Test: markdown renders on successful conversion

Related Issue

Closes #1780

Sentry: ELECTRON-AP — 16 events, last seen 2026-03-26, affecting v1.9.1
Same root cause pattern as PR #1775 (useMcpServerCRUD).

Verification

  • Unit tests pass (bun run test -- --run tests/unit/OfficeDocViewer.dom.test.tsx)
  • Type check passes (bunx tsc --noEmit)
  • Lint and format pass

Test Plan

  • Open a Word document (.docx) in Preview — verify it renders correctly
  • Provide an invalid/missing file path — verify error message appears without crash
  • Verify no addInstance TypeError in Sentry after deployment

…OfficeDocViewer

Replace hook-based useMessage ref with static Message.error() in the
loadDocument error handler. The hook-based API crashes when the context
holder is not mounted (loading/error render paths omit it), causing
TypeError: Cannot read properties of undefined (reading 'addInstance').

Add unit tests verifying static Message.error is called on conversion
failure, error text is displayed, and successful conversion renders
markdown.

Fixes ELECTRON-AP
@kaizhou-lab kaizhou-lab marked this pull request as ready for review March 27, 2026 04:22
@piorpua piorpua added the bot:reviewing Review in progress (mutex) label Mar 29, 2026
@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 29, 2026

Code Review:fix(preview): use static Message API to prevent addInstance crash in OfficeDocViewer (#1781)

变更概述

本 PR 修复 OfficeDocViewer 中的 Sentry 崩溃(ELECTRON-AP):在 loadDocumentcatch 分支中,messageContextHolder 尚未挂载时调用了基于 context 的 messageApiRef.current?.error?.(),导致 addInstance TypeError。修复方案是将该调用替换为静态 Message.error(),并移除不再需要的 messageApiRef 与其同步 useEffect。同时新增 3 个单元测试覆盖失败路径和成功路径。


方案评估

结论:✅ 方案合理

根本原因定位准确:loadDocument 通过 useEffect 异步触发,错误发生时组件的错误渲染路径不包含 messageContextHolder,hook-based API 在此时无效。改用静态 Message.error() 是 arco-design 推荐的非 context 环境下的标准用法,方案简洁且与项目其他同类修复(PR #1775)一致。用户主动触发的操作(handleOpenInSystem/handleShowInFolder)仍使用 hook-based messageApi,这些操作仅在 messageContextHolder 已挂载的渲染路径下可被触发,无需同步修改。


问题清单

🔵 LOW — 测试文件中存在未使用的 act 导入

文件tests/unit/OfficeDocViewer.dom.test.tsx,第 7 行

问题代码

import { render, act, waitFor } from '@testing-library/react';

问题说明act 被导入但在测试中从未使用,oxlint 报告 no-unused-vars 警告。

修复建议

import { render, waitFor } from '@testing-library/react';

汇总

# 严重级别 文件 问题
1 🔵 LOW tests/unit/OfficeDocViewer.dom.test.tsx:7 未使用的 act 导入

结论

⚠️ 有条件批准 — 仅一处 LOW 级别问题(测试文件中未使用的 act 导入),移除后即可合并。

CONCLUSION: CONDITIONAL
IS_CRITICAL_PATH: false
PR_NUMBER: 1781


本报告由本地 pr-review skill 生成,包含完整项目上下文,无截断限制。

@piorpua piorpua added bot:ready-to-fix CONDITIONAL review done, waiting for bot fix bot:fixing Fix in progress (mutex) and removed bot:reviewing Review in progress (mutex) bot:ready-to-fix CONDITIONAL review done, waiting for bot fix labels Mar 29, 2026
@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 29, 2026

PR Fix 验证报告

原始 PR: #1781
修复方式: 无需代码变更(所有问题均为 LOW 级别,已跳过)

# 严重级别 文件 问题 修复方式 状态
1 🔵 LOW tests/unit/OfficeDocViewer.dom.test.tsx:7 未使用的 act 导入 ⏭️ 已跳过

总结: ✅ 已修复 0 个 | ⏭️ 已跳过 1 个(LOW)

🔵 LOW 级别问题已跳过(不阻塞合并,修复优先级低)。本 PR 代码无需变更,可直接合并。

@piorpua piorpua merged commit a9515e5 into main Mar 29, 2026
17 checks passed
@piorpua piorpua deleted the fix/sentry-ELECTRON-AP branch March 29, 2026 05:29
@piorpua piorpua added bot:done Auto-merged by bot and removed bot:fixing Fix in progress (mutex) labels Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:done Auto-merged by bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(preview): OfficeDocViewer crashes with TypeError on addInstance when context holder unmounted

2 participants