### What problem does this PR solve? fix: display specific error message when previewing file error #868 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
99 lines
2.7 KiB
TypeScript
99 lines
2.7 KiB
TypeScript
import jsPreviewExcel from '@js-preview/excel';
|
|
import axios from 'axios';
|
|
import mammoth from 'mammoth';
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
export const useCatchError = (api: string) => {
|
|
const [error, setError] = useState('');
|
|
const fetchDocument = useCallback(async () => {
|
|
const ret = await axios.get(api);
|
|
const { data } = ret;
|
|
if (!(data instanceof ArrayBuffer) && data.retcode !== 0) {
|
|
setError(data.retmsg);
|
|
}
|
|
return ret;
|
|
}, [api]);
|
|
|
|
useEffect(() => {
|
|
fetchDocument();
|
|
}, [fetchDocument]);
|
|
|
|
return { fetchDocument, error };
|
|
};
|
|
|
|
export const useFetchDocument = () => {
|
|
const fetchDocument = useCallback(async (api: string) => {
|
|
const ret = await axios.get(api, { responseType: 'arraybuffer' });
|
|
return ret;
|
|
}, []);
|
|
|
|
return { fetchDocument };
|
|
};
|
|
|
|
export const useFetchExcel = (filePath: string) => {
|
|
const [status, setStatus] = useState(true);
|
|
const { fetchDocument } = useFetchDocument();
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const { error } = useCatchError(filePath);
|
|
|
|
const fetchDocumentAsync = useCallback(async () => {
|
|
let myExcelPreviewer;
|
|
if (containerRef.current) {
|
|
myExcelPreviewer = jsPreviewExcel.init(containerRef.current);
|
|
}
|
|
const jsonFile = await fetchDocument(filePath);
|
|
myExcelPreviewer
|
|
?.preview(jsonFile.data)
|
|
.then(() => {
|
|
console.log('succeed');
|
|
setStatus(true);
|
|
})
|
|
.catch((e) => {
|
|
console.warn('failed', e);
|
|
myExcelPreviewer.destroy();
|
|
setStatus(false);
|
|
});
|
|
}, [filePath, fetchDocument]);
|
|
|
|
useEffect(() => {
|
|
fetchDocumentAsync();
|
|
}, [fetchDocumentAsync]);
|
|
|
|
return { status, containerRef, error };
|
|
};
|
|
|
|
export const useFetchDocx = (filePath: string) => {
|
|
const [succeed, setSucceed] = useState(true);
|
|
const { fetchDocument } = useFetchDocument();
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const { error } = useCatchError(filePath);
|
|
|
|
const fetchDocumentAsync = useCallback(async () => {
|
|
const jsonFile = await fetchDocument(filePath);
|
|
mammoth
|
|
.convertToHtml(
|
|
{ arrayBuffer: jsonFile.data },
|
|
{ includeDefaultStyleMap: true },
|
|
)
|
|
.then((result) => {
|
|
setSucceed(true);
|
|
const docEl = document.createElement('div');
|
|
docEl.className = 'document-container';
|
|
docEl.innerHTML = result.value;
|
|
const container = containerRef.current;
|
|
if (container) {
|
|
container.innerHTML = docEl.outerHTML;
|
|
}
|
|
})
|
|
.catch(() => {
|
|
setSucceed(false);
|
|
});
|
|
}, [filePath, fetchDocument]);
|
|
|
|
useEffect(() => {
|
|
fetchDocumentAsync();
|
|
}, [fetchDocumentAsync]);
|
|
|
|
return { succeed, containerRef, error };
|
|
};
|