Repository navigation
Improve docx preview - #15907
Improve docx preview#15907
Conversation
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughDocPreviewer and its hooks migrate from Mammoth-based DOCX→HTML rendering to an editor-based preview using ChangesDOCX Viewer Replacement
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
web/src/components/document-preview/doc-preview.tsx (3)
166-221: ⚖️ Poor tradeoffUser-facing strings should use i18n.
Multiple hardcoded English strings are not internationalized:
- "Loading..." / "Page X" (toolbar)
- "Zoom out" / "Zoom in" (aria-labels)
- "Preview is not available for this Word document"
- "
@extend-ai/react-docxsupports modern .docx files only."As per coding guidelines, use
useTranslationfromreact-i18nextfor all user-facing text.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/components/document-preview/doc-preview.tsx` around lines 166 - 221, The component currently renders several hardcoded user-facing strings in doc-preview.tsx (e.g., the toolbar texts "Loading...", `Page ${pageCount || '-'}`, aria-labels "Zoom out"/"Zoom in", and error panel strings "Preview is not available for this Word document" and "`@extend-ai/react-docx` supports modern .docx files only.")—replace these with i18n lookups by importing and calling useTranslation from react-i18next at the top of the component, define keys (e.g., toolbar.loading, toolbar.page, toolbar.zoomOut, toolbar.zoomIn, error.previewUnavailable, error.supportNote) and use t('...') where loading, pageCount, zoomScale, handleZoomOut, and handleZoomIn are used so all visible text and aria-labels come from translation strings.Source: Coding guidelines
166-169: 💤 Low valueAmbiguous page count label.
Page ${pageCount || '-'}displays "Page 5" for 5 total pages, which could be mistaken for "currently viewing page 5." Consider "5 pages" or "Page 1 of 5" for clarity.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/components/document-preview/doc-preview.tsx` around lines 166 - 169, The page count label is ambiguous; update the JSX in the doc-preview component (the span that currently renders {loading ? 'Loading...' : error ? '' : `Page ${pageCount || '-'}`}) to show a clearer total or context-aware label: either render `${pageCount || '-'} pages` when only total is known, or if a current page variable exists (e.g., currentPage or pageIndex), render `Page ${currentPage} of ${pageCount}`; keep the loading and error branches unchanged and ensure you handle falsy pageCount by showing '-' as before.
36-55: 💤 Low value
clampZoomhas a logic error for intermediate (non-step) scale values.When
scaleisn't already inZOOM_STEPS,findIndexlocates the adjacent step, thendirectionis added again—resulting in skipping a step. For example, zooming in from 110% would jump to 150% instead of 125%.This path is currently unreachable since
zoomScaleis always initialized and reset to step values, but the logic is incorrect if that invariant ever changes.Suggested fix
const clampZoom = (scale: number, direction: 1 | -1): number => { let idx = ZOOM_STEPS.indexOf(scale as (typeof ZOOM_STEPS)[number]); if (idx < 0) { + // scale is between steps: find adjacent step without adding direction again if (direction > 0) { idx = ZOOM_STEPS.findIndex((v) => v > scale); + if (idx < 0) idx = ZOOM_STEPS.length - 1; + return ZOOM_STEPS[idx]; } else { for (let i = ZOOM_STEPS.length - 1; i >= 0; i--) { if (ZOOM_STEPS[i] < scale) { - idx = i; - break; + return ZOOM_STEPS[i]; } } + return ZOOM_STEPS[0]; } } - idx = Math.max( - 0, - Math.min(ZOOM_STEPS.length - 1, idx < 0 ? 0 : idx + direction), - ); + idx = Math.max(0, Math.min(ZOOM_STEPS.length - 1, idx + direction)); return ZOOM_STEPS[idx] ?? scale; };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/components/document-preview/doc-preview.tsx` around lines 36 - 55, clampZoom incorrectly computes the next zoom step when scale is not exactly in ZOOM_STEPS because it finds an adjacent index then adds direction again (causing a skip); change clampZoom to compute the target index directly: if scale exists in ZOOM_STEPS use foundIndex + direction, otherwise if direction === 1 set targetIndex = first index where ZOOM_STEPS[i] > scale, and if direction === -1 set targetIndex = last index where ZOOM_STEPS[i] < scale; then clamp targetIndex between 0 and ZOOM_STEPS.length-1 and return ZOOM_STEPS[targetIndex] (use the function name clampZoom and the constant ZOOM_STEPS to locate the code).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/components/document-preview/doc-preview.tsx`:
- Around line 140-145: The effect in useEffect that checks status === 'Only
.docx files are supported' is brittle; change the component to rely on a stable
flag or status code rather than a magic string: add or read a typed status value
(e.g., errorCode or UploadStatus enum) from the editor state or propagate a
constant like DOCX_UNSUPPORTED_CODE and update the effect to check that (or use
a deterministic helper like isDocxUnsupportedStatus(status)); then call
setError(...) and setLoading(false) when that code/helper indicates the
unsupported-docx condition (update any producers of status to emit the
code/constant as needed) using the existing setError and setLoading functions.
---
Nitpick comments:
In `@web/src/components/document-preview/doc-preview.tsx`:
- Around line 166-221: The component currently renders several hardcoded
user-facing strings in doc-preview.tsx (e.g., the toolbar texts "Loading...",
`Page ${pageCount || '-'}`, aria-labels "Zoom out"/"Zoom in", and error panel
strings "Preview is not available for this Word document" and
"`@extend-ai/react-docx` supports modern .docx files only.")—replace these with
i18n lookups by importing and calling useTranslation from react-i18next at the
top of the component, define keys (e.g., toolbar.loading, toolbar.page,
toolbar.zoomOut, toolbar.zoomIn, error.previewUnavailable, error.supportNote)
and use t('...') where loading, pageCount, zoomScale, handleZoomOut, and
handleZoomIn are used so all visible text and aria-labels come from translation
strings.
- Around line 166-169: The page count label is ambiguous; update the JSX in the
doc-preview component (the span that currently renders {loading ? 'Loading...' :
error ? '' : `Page ${pageCount || '-'}`}) to show a clearer total or
context-aware label: either render `${pageCount || '-'} pages` when only total
is known, or if a current page variable exists (e.g., currentPage or pageIndex),
render `Page ${currentPage} of ${pageCount}`; keep the loading and error
branches unchanged and ensure you handle falsy pageCount by showing '-' as
before.
- Around line 36-55: clampZoom incorrectly computes the next zoom step when
scale is not exactly in ZOOM_STEPS because it finds an adjacent index then adds
direction again (causing a skip); change clampZoom to compute the target index
directly: if scale exists in ZOOM_STEPS use foundIndex + direction, otherwise if
direction === 1 set targetIndex = first index where ZOOM_STEPS[i] > scale, and
if direction === -1 set targetIndex = last index where ZOOM_STEPS[i] < scale;
then clamp targetIndex between 0 and ZOOM_STEPS.length-1 and return
ZOOM_STEPS[targetIndex] (use the function name clampZoom and the constant
ZOOM_STEPS to locate the code).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e3649764-0a1d-4874-ab14-3f828ae87fb8
⛔ Files ignored due to path filters (1)
web/package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (3)
web/package.jsonweb/src/components/document-preview/doc-preview.tsxweb/src/components/document-preview/hooks.ts
💤 Files with no reviewable changes (1)
- web/src/components/document-preview/hooks.ts
| useEffect(() => { | ||
| if (url) { | ||
| fetchDocument(); | ||
| if (status === 'Only .docx files are supported') { | ||
| setError(status); | ||
| setLoading(false); | ||
| } | ||
| }, [url]); | ||
| }, [status]); |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
@extend-ai/react-docx useDocxEditor status API values
💡 Result:
The @extend-ai/react-docx package (typically published under the scoped package name @eigenpal/docx-editor-react) does not have a single API called useDocxEditor that provides a status API value. Instead, the editor functionality is managed through various specialized hooks and a main component ref [1][2][3]. If you are looking for status information, it is typically accessed through specific hooks or component properties rather than a generic status API on a useDocxEditor hook: 1. AutoSave Status: The useAutoSave hook provides a status member, which returns an AutoSaveStatus type [1]. 2. Agent Status: If you are using the agent integration via @eigenpal/docx-editor-agents/react, the status field for agent messages can take the values 'streaming' or 'done' [4]. 3. Editor State/Ref: You can access editor state and methods via DocxEditorRef by passing a ref to the DocxEditor component [2][3]. This allows you to interact with the document, tracked changes, comments, and selection state directly [2][3]. For the most accurate technical implementation, please refer to the official API documentation for the specific hook or feature you are implementing, as functionality is modularized into subpaths like /hooks, /ui, and /plugin-api [1][2][5][3].
Citations:
- 1: https://www.docx-editor.dev/docs/1.x/api/react/hooks
- 2: https://www.docx-editor.dev/docs/1.x/api/react
- 3: https://www.docx-editor.dev/docs/props
- 4: https://www.docx-editor.dev/docs/1.x/api/agents/react
- 5: https://www.docx-editor.dev/docs/1.x/api/react/ui
Fragile magic-string comparison for editor status in doc preview.
In web/src/components/document-preview/doc-preview.tsx (lines ~140-145), the effect branches on status === 'Only .docx files are supported', coupling behavior to an exact error-message string. If the editor message changes, this error handling can silently stop working. Prefer an exposed typed status/error code (or a less brittle check) instead of exact string equality.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/components/document-preview/doc-preview.tsx` around lines 140 - 145,
The effect in useEffect that checks status === 'Only .docx files are supported'
is brittle; change the component to rely on a stable flag or status code rather
than a magic string: add or read a typed status value (e.g., errorCode or
UploadStatus enum) from the editor state or propagate a constant like
DOCX_UNSUPPORTED_CODE and update the effect to check that (or use a
deterministic helper like isDocxUnsupportedStatus(status)); then call
setError(...) and setLoading(false) when that code/helper indicates the
unsupported-docx condition (update any producers of status to emit the
code/constant as needed) using the existing setError and setLoading functions.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15907 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| "@ant-design/icons": "^5.2.6", | ||
| "@antv/g2": "^5.2.10", | ||
| "@antv/g6": "^5.1.0", | ||
| "@extend-ai/react-docx": "^0.6.7", |
There was a problem hiding this comment.
It is recommended to delete any remaining mammoth libraries.
No description provided.