-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDocumentTable.jsx
More file actions
115 lines (105 loc) · 3.96 KB
/
DocumentTable.jsx
File metadata and controls
115 lines (105 loc) · 3.96 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
// React Imports
import React, { useContext, useMemo } from 'react';
// Dependency Imports
import { v4 as uuidv4 } from 'uuid';
// Constants Imports
import DOC_TYPES from '@constants/doc_types';
// Material UI Imports
import Box from '@mui/material/Box';
import { useMediaQuery } from '@mui/material';
// Context Imports
import { DocumentListContext } from '@contexts';
import { useSession } from '@hooks';
// Utility Imports
import { getBlobFromSolid } from '@utils';
// Theme Imports
import theme from '../../theme';
// Component Imports
import { EmptyListNotification, LoadingAnimation } from '../Notification';
import DocumentsMobile from './DocumentsMobile';
import DocumentsDesktop from './DocumentsDesktop';
/**
* DocumentTable - A component responsible for rendering a list of documents
* fetched from a Solid pod. It adapts its rendering based on the screen size
* (mobile or desktop) using the `DocumentsMobile` and `DocumentsDesktop`
* components.
*
* @memberof Documents
* @name DocumentTable
* @param {object} props - Component props
* @param {(modalType: string, docName: string, docType: string) => void} props.handleAclPermissionsModal - Function to open the ACL permissions modal
* @param {(document: object) => void} props.handleSelectDeleteDoc - Function to handle document deletion
* @returns {React.JSX.Element} - The DocumentTable component
*/
const DocumentTable = ({ handleAclPermissionsModal, handleSelectDeleteDoc }) => {
const { session } = useSession();
const { documentListObject, loadingDocuments } = useContext(DocumentListContext);
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
const isSmallScreenHeight = useMediaQuery('(max-height: 600px)');
/**
* Handles the local display of a document by opening it in a new window.
*
* @async
* @function
* @param {string} urlToOpen - The URL of the document to be opened.
* @throws {Error} Throws an error if there is an issue fetching the document blob.
* @returns {Promise<void>} A promise that resolves after the document is opened.
*/
const handleShowDocumentLocal = async (urlToOpen) => {
/**
* Fetches a Blob from a Solid pod based on the provided session and URL.
*
* @async
* @function
* @param {object} session - The Solid session object.
* @param {string} url - The URL of the document on the Solid pod.
* @returns {Promise<Blob>} A promise that resolves with the Blob of the document.
* @throws {Error} Throws an error if there is an issue fetching the document blob.
*/
const urlFileBlob = await getBlobFromSolid(session, urlToOpen);
// Open a new window to display the document using the blob URL
window.open(urlFileBlob);
};
// Maps raw document types to user-friendly display names using `DOC_TYPES`
const mappingType = (type) => DOC_TYPES[type] || type;
// Processes the document list, adding unique IDs and mapping types
const documents = useMemo(
() =>
documentListObject?.docList.map((document) => ({
id: uuidv4(),
type: mappingType(document.type),
...document
})),
[documentListObject?.docList]
);
const handlers = {
onShare: handleAclPermissionsModal,
onDelete: handleSelectDeleteDoc,
onPreview: handleShowDocumentLocal
};
if (!documents?.length)
return <EmptyListNotification type="documents" data-testid="empty-list" />;
if (loadingDocuments)
return <LoadingAnimation loadingItem="documents" data-testid="loading-animation" />;
return (
<Box
sx={{
margin: '20px 0',
width: '95vw',
height: '100%'
}}
data-testid="document-table"
>
{isMobile || isSmallScreenHeight ? (
<DocumentsMobile documents={documents} handlers={handlers} data-testid="documents-mobile" />
) : (
<DocumentsDesktop
documents={documents}
handlers={handlers}
data-testid="documents-desktop"
/>
)}
</Box>
);
};
export default DocumentTable;