Skip to content

Commit 2b48f8e

Browse files
author
Bob Strahan
committed
fix(ui): improve text confidence view and viewer behavior in document UI
1 parent 7e7e7db commit 2b48f8e

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ SPDX-License-Identifier: MIT-0
1010
- **Text Confidence View for Document Pages**
1111
- Added support for displaying OCR text confidence data through new `TextConfidenceUri` field
1212
- New "Text Confidence View" option in the UI pages panel alongside existing Markdown and Text views
13+
- Fixed issues with view persistence - Text Confidence View button now always visible with appropriate messaging when content unavailable
14+
- Fixed view toggle behavior - switching between views no longer closes the viewer window
15+
- Reordered view buttons to: Markdown View, Text Confidence View, Text View for better user experience
1316

1417

1518
### Fixed

src/ui/src/components/document-viewer/MarkdownJsonViewer.jsx

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const FileEditorView = ({
103103
fileType = 'text',
104104
viewMode,
105105
onViewModeChange,
106-
textConfidenceUri,
106+
isConfidenceAvailable,
107107
}) => {
108108
const [isValid, setIsValid] = useState(true);
109109
const [jsonData, setJsonData] = useState(null);
@@ -147,8 +147,8 @@ const FileEditorView = ({
147147
onChange={onViewModeChange}
148148
options={[
149149
{ id: 'markdown', text: 'Markdown View' },
150+
{ id: 'confidence', text: 'Text Confidence View' },
150151
{ id: 'text', text: 'Text View' },
151-
...(textConfidenceUri ? [{ id: 'confidence', text: 'Text Confidence View' }] : []),
152152
]}
153153
/>
154154

@@ -159,7 +159,11 @@ const FileEditorView = ({
159159
)}
160160
</SpaceBetween>
161161

162-
{viewMode === 'markdown' || viewMode === 'confidence' ? (
162+
{viewMode === 'confidence' && !isConfidenceAvailable ? (
163+
<Box color="text-status-inactive" padding="l" textAlign="center">
164+
Text confidence content is not available for this page.
165+
</Box>
166+
) : viewMode === 'markdown' || viewMode === 'confidence' ? (
163167
<MarkdownViewer
164168
simple
165169
content={
@@ -197,12 +201,28 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
197201
const [isEditing, setIsEditing] = useState(false);
198202
const [editedContent, setEditedContent] = useState(null);
199203
const [viewMode, setViewMode] = useState('markdown');
204+
const [loadedUri, setLoadedUri] = useState(null);
205+
const [isViewerOpen, setIsViewerOpen] = useState(false);
206+
207+
const fetchContent = async (forceRefetch = false) => {
208+
// Determine which URI to fetch based on view mode
209+
const uriToFetch = viewMode === 'confidence' ? textConfidenceUri : fileUri;
210+
211+
// If confidence view is selected but no URI available, don't fetch
212+
if (viewMode === 'confidence' && !textConfidenceUri) {
213+
setFileContent(null);
214+
setLoadedUri(null);
215+
return;
216+
}
217+
218+
// Skip fetch if we already have the content for this URI (unless forced)
219+
if (!forceRefetch && loadedUri === uriToFetch && fileContent) {
220+
return;
221+
}
200222

201-
const fetchContent = async () => {
202223
setIsLoading(true);
203224
setError(null);
204225
try {
205-
const uriToFetch = viewMode === 'confidence' && textConfidenceUri ? textConfidenceUri : fileUri;
206226
logger.info('Fetching content:', uriToFetch);
207227

208228
const response = await API.graphql({
@@ -221,6 +241,7 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
221241
}
222242
logger.debug('Received content:', `${fetchedContent.substring(0, 100)}...`);
223243
setFileContent(fetchedContent);
244+
setLoadedUri(uriToFetch);
224245
} catch (err) {
225246
logger.error('Error fetching content:', err);
226247
setError(`Failed to load ${fileType} content. Please try again.`);
@@ -317,6 +338,8 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
317338
setFileContent(null);
318339
setEditedContent(null);
319340
setIsEditing(false);
341+
setIsViewerOpen(false);
342+
setLoadedUri(null);
320343
};
321344

322345
if (!fileUri) {
@@ -328,16 +351,33 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
328351
}
329352

330353
const handleViewModeChange = ({ detail }) => {
331-
setViewMode(detail.selectedId);
332-
// Clear content when switching views to force re-fetch
333-
setFileContent(null);
334-
setEditedContent(null);
354+
const newMode = detail.selectedId;
355+
setViewMode(newMode);
356+
357+
// Determine if we need to fetch new content
358+
const newUri = newMode === 'confidence' ? textConfidenceUri : fileUri;
359+
360+
// Only fetch if switching to a different URI
361+
if (newUri && newUri !== loadedUri) {
362+
fetchContent();
363+
}
364+
};
365+
366+
// Effect to fetch content when viewer opens or view mode changes
367+
React.useEffect(() => {
368+
if (isViewerOpen) {
369+
fetchContent();
370+
}
371+
}, [isViewerOpen, viewMode]);
372+
373+
const openViewer = () => {
374+
setIsViewerOpen(true);
335375
};
336376

337377
return (
338378
<Box className="w-full">
339-
{!fileContent && (
340-
<Button onClick={fetchContent} loading={isLoading} disabled={isLoading}>
379+
{!isViewerOpen && (
380+
<Button onClick={openViewer} loading={isLoading} disabled={isLoading}>
341381
{buttonText}
342382
</Button>
343383
)}
@@ -354,7 +394,7 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
354394
</Box>
355395
)}
356396

357-
{fileContent && (
397+
{isViewerOpen && (
358398
<SpaceBetween size="s" className="json-viewer-container" style={{ width: '100%', minWidth: '700px' }}>
359399
<Box>
360400
<SpaceBetween direction="horizontal" size="xs">
@@ -375,13 +415,13 @@ const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', but
375415
</Box>
376416
<div style={{ width: '100%' }}>
377417
<FileEditorView
378-
fileContent={isEditing ? editedContent : fileContent}
418+
fileContent={isLoading ? null : isEditing ? editedContent : fileContent}
379419
onChange={handleContentChange}
380420
isReadOnly={!isEditing}
381421
fileType={fileType}
382422
viewMode={viewMode}
383423
onViewModeChange={handleViewModeChange}
384-
textConfidenceUri={textConfidenceUri}
424+
isConfidenceAvailable={!!textConfidenceUri}
385425
/>
386426
</div>
387427
</SpaceBetween>

0 commit comments

Comments
 (0)