-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDocumentPreview.tsx
More file actions
161 lines (148 loc) · 5.08 KB
/
DocumentPreview.tsx
File metadata and controls
161 lines (148 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, { FC, ReactElement, useContext, useEffect, useState } from 'react';
import { SkeletonText } from 'carbon-components-react';
import { settings } from 'carbon-components';
import { QueryResult, QueryResultPassage, QueryTableResult } from 'ibm-watson/discovery/v2';
import { SearchContext } from 'components/DiscoverySearch/DiscoverySearch';
import { PreviewToolbar } from './components/PreviewToolbar/PreviewToolbar';
import SimpleDocument from './components/SimpleDocument/SimpleDocument';
import withErrorBoundary, { WithErrorBoundaryProps } from 'utils/hoc/withErrorBoundary';
import { defaultMessages, Messages } from './messages';
import HtmlView from './components/HtmlView/HtmlView';
import PdfViewerWithHighlight from './components/PdfViewerHighlight/PdfViewerWithHighlight';
import { isCsvFile, isJsonFile } from './utils/documentData';
const { ZOOM_IN, ZOOM_OUT } = PreviewToolbar;
interface Props extends WithErrorBoundaryProps {
/**
* Document data, as that returned by a query. Overrides result from SearchContext
*/
document?: QueryResult;
/**
* PDF file data as base64-encoded string
*/
file?: string;
/**
* Passage or table to highlight in document. Reference to item with
* `document.document_passages` or `document.table_results`
*/
highlight?: QueryResultPassage | QueryTableResult;
/**
* i18n messages for the component
*/
messages?: Messages;
}
const SCALE_FACTOR = 1.2;
const DocumentPreview: FC<Props> = ({
document,
file,
highlight,
messages = defaultMessages,
didCatch
}) => {
const { selectedResult } = useContext(SearchContext);
// document prop takes precedence over that in context
const doc = document || selectedResult.document;
highlight = highlight || selectedResult.element || undefined;
const [scale, setScale] = useState(1);
const [currentPage, setCurrentPage] = useState(1);
const [loading, setLoading] = useState(true);
const [hideToolbarControls, setHideToolbarControls] = useState(false);
useEffect(() => {
// reset state if document changes
setCurrentPage(1);
//reset scale if document changes
setScale(1);
// TODO disable for now, until we can properly fix https://github.ibm.com/Watson-Discovery/docviz-squad-issue-tracker/issues/143
// setLoading(true);
}, [doc]);
const base = `${settings.prefix}--document-preview`;
return (
<div className={`${base}`}>
{(doc || file) && !didCatch ? (
<>
<PreviewToolbar
loading={loading}
hideControls={hideToolbarControls}
current={currentPage}
total={0}
onChange={setCurrentPage}
onZoom={(zoom: any): void => {
if (zoom === ZOOM_IN || zoom === ZOOM_OUT) {
setScale(zoom === ZOOM_IN ? scale * SCALE_FACTOR : scale / SCALE_FACTOR);
} else {
setScale(1);
}
}}
/>
<div className={`${base}__document`}>
<PreviewDocument
document={doc}
highlight={highlight}
setLoading={setLoading}
setHideToolbarControls={setHideToolbarControls}
loading={loading}
hideToolbarControls={hideToolbarControls}
/>
</div>
{loading && (
<div className={`${base}__skeleton`}>
<SkeletonText paragraph={true} lineCount={40} />
</div>
)}
</>
) : didCatch ? (
<div className={`${base}__error`}>{messages.errorMessage}</div>
) : (
<div className={`${base}__error`}>{messages.noDataMessage}</div>
)}
</div>
);
};
interface PreviewDocumentProps {
document?: QueryResult | null;
highlight?: any;
setLoading: (loading: boolean) => void;
setHideToolbarControls?: (disabled: boolean) => void;
loading: boolean;
hideToolbarControls: boolean;
}
function PreviewDocument({
document,
loading,
setLoading,
hideToolbarControls,
setHideToolbarControls,
highlight
}: PreviewDocumentProps): ReactElement | null {
if (document) {
const isJsonType = isJsonFile(document);
const isCsvType = isCsvFile(document);
if (document.html && !isJsonType && !isCsvType) {
return (
<HtmlView
document={document}
highlight={highlight}
setHideToolbarControls={setHideToolbarControls}
setLoading={setLoading}
/>
);
}
return (
<SimpleDocument
document={document}
highlight={highlight}
hideToolbarControls={hideToolbarControls}
setHideToolbarControls={setHideToolbarControls}
loading={loading}
setLoading={setLoading}
/>
);
}
return null;
}
//Replace any with a proper TS check
const ErrorBoundDocumentPreview: any = withErrorBoundary(DocumentPreview);
ErrorBoundDocumentPreview.PreviewToolbar = PreviewToolbar;
ErrorBoundDocumentPreview.PreviewDocument = PreviewDocument;
ErrorBoundDocumentPreview.PdfViewerWithHighlight = PdfViewerWithHighlight;
export default ErrorBoundDocumentPreview;
export { ErrorBoundDocumentPreview as DocumentPreview };