-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
318 lines (291 loc) · 9 KB
/
Copy pathindex.tsx
File metadata and controls
318 lines (291 loc) · 9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import './index.scss';
import { schema as schemaDefn } from '@bcgsc-pori/graphkb-schema';
import CloseIcon from '@mui/icons-material/Close';
import EditIcon from '@mui/icons-material/Edit';
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
import {
Collapse,
Divider,
Drawer,
IconButton,
List,
ListItem,
ListItemText,
ListSubheader,
Typography,
} from '@mui/material';
import React, { ReactNode, useEffect, useState } from 'react';
import { Link } from 'react-router';
import { GeneralRecordType } from '@/components/types';
import schema from '@/services/schema';
import util from '@/services/util';
import { useAuth } from '../Auth';
import LinkEmbeddedPropList from './LinkEmbeddedPropList';
import RelationshipList from './RelationshipList';
import SetPropsList from './SetPropsList';
import TextRow from './TextRow';
/**
* Takes properties list to be displayed in detail drawer and promotes an inputted
* property to top of the list. For display purposes.
*
* @property {Array.<PropertyModel>} properties array of property models to be rearranged
* @property {string} propToBeMovedToTop property to be promoted to top of array for display
*/
const movePropToTop = (properties, propToBeMovedToTop) => {
const propIndex = properties.findIndex((prop) => prop.name === propToBeMovedToTop);
const updatedProperties = [...properties];
if (propIndex !== 0 && propIndex !== -1) {
updatedProperties.splice(propIndex, 1);
updatedProperties.unshift(properties[propIndex]);
}
return updatedProperties;
};
interface DetailDrawerProps {
/** Function triggered on @mui/Drawer onClose event. */
isEdge?: boolean;
/** Ontology to be displayed in drawer. */
node?: GeneralRecordType;
onClose?: () => void;
}
/**
* Component used to display record details in a side drawer. Dynamically
* generates display based on record, and its corresponding schema entry.
*/
function DetailDrawer(props: DetailDrawerProps) {
const {
node,
onClose,
isEdge = false,
} = props;
const auth = useAuth();
const [opened, setOpened] = useState<string[]>([]);
/**
* Toggles collapsed list item.
* @param key - list item key.
*/
const handleExpand = (key: string) => {
if (opened.includes(key)) {
opened.splice(opened.indexOf(key), 1);
} else {
opened.push(key);
}
setOpened([...opened]);
};
const [linkOpen, setLinkOpen] = useState<string | null>(null);
/**
* Toggles collapsed link list item.
* @param key - list item key.
*/
const handleLinkExpand = (key: string) => {
if (linkOpen === key) {
setLinkOpen(null);
setOpened(opened.filter((o) => !o.includes(key)));
} else {
setLinkOpen(key);
}
};
/**
* Formats properties, varying structure based on property type. Base function
* that other formatting functions call.
* @param {Object} record - Record being displayed for its details.
* @param {Array.<Object>} properties - List of property models to display.
* @param {boolean} isNested - Nested flag.
*/
const formatProps = (record, properties, isNested) => {
const identifiers = ['displayName', '@rid', 'sourceId'];
const updatedProperties = movePropToTop(properties, 'displayName');
return updatedProperties.map((prop) => {
const { type } = prop;
let { name } = prop;
let value = record[name];
if (!value) return null;
if (type === 'embeddedset' || type === 'linkset') {
const formattedSetProps = (
<SetPropsList
handleExpand={handleExpand}
identifiers={identifiers}
opened={opened}
prop={prop}
value={value}
/>
);
return formattedSetProps;
}
if ((type === 'link' || type === 'embedded') && value['@class']) {
const linkEmbeddedProps = (
<LinkEmbeddedPropList
// eslint-disable-next-line no-use-before-define
formatOtherProps={formatOtherProps}
handleExpand={handleExpand}
identifiers={identifiers}
isNested={isNested}
opened={opened}
prop={prop}
value={value}
/>
);
return linkEmbeddedProps;
}
if (name === 'displayNameTemplate') {
name = 'Statement';
value = schema.getLabel(node, { truncate: false });
}
return (
<TextRow
handleExpand={handleExpand}
isNested={isNested}
isStatic
name={name}
opened={opened}
value={value}
/>
);
});
};
/**
* Closes all expanded list properties.
* @param {Object} prevProps - Component's previous props.
*/
useEffect(() => {
if (!node) {
setOpened([]);
setLinkOpen(null);
}
}, [node]);
/**
* Formats record metadata.
* @param {Object} record - Record to be formatted.
* @param {boolean} isNested - Nested flag.
*/
const formatMetadata = (record, isNested) => formatProps(record, schema.getMetadata(), isNested);
/**
* Formats non-identifying, non-metadata properties of the input record.
* @param {Object} node - Record being displayed.
* @param isNested - Nested flag indicating if record is embedded
*/
const formatOtherProps = (record, isNested?: boolean) => {
const identifiers = ['@class', '@rid'];
let properties = Object.keys(record)
.map((key) => ({ name: key, type: util.parseKBType(record[key]) }));
if (record['@class'] && schemaDefn.getProperties(record['@class'])) {
properties = schema.getProperties(record['@class']);
}
const propsList = Object.values(properties)
.filter((prop) => !identifiers.map((id) => id.split('.')[0]).includes(prop.name)
&& !prop.name.startsWith('in_')
&& !prop.name.startsWith('out_'));
return formatProps(record, propsList, isNested);
};
const drawerIsOpen = Boolean(node);
let content: ReactNode = null;
if (drawerIsOpen) {
const recordId = node?.['@rid']?.replace(/^#/, '');
const recordClass = node?.['@class'];
const otherProps = formatOtherProps(node);
const metadata = formatMetadata(node, true);
const metadataIsOpen = opened.includes('metadata');
let preview;
let errorMessage;
try {
preview = schema.getLabel(node);
// Only for kbp nodes so far.
} catch (e) {
preview = 'Invalid variant';
errorMessage = (e as Error).message;
}
content = (
<div className="detail-drawer__content">
<div className="detail-drawer__heading">
<div>
<Typography variant="h2">
{preview}
</Typography>
{errorMessage && (
<Typography color="error" variant="subtitle2">
{errorMessage}
</Typography>
)}
</div>
{auth.hasWriteAccess && (
<Link target="_blank" to={`/edit/${recordClass}/${recordId}`}>
<IconButton>
<EditIcon />
</IconButton>
</Link>
)}
<Link target="_blank" to={`/view/${recordClass}/${recordId}`}>
<IconButton>
<OpenInNewIcon />
</IconButton>
</Link>
<IconButton onClick={() => onClose?.()}>
<CloseIcon />
</IconButton>
</div>
<Divider />
{otherProps}
<ListItem
dense
onClick={() => handleExpand('metadata')}
>
<ListItemText
primary={<Typography>Metadata</Typography>}
/>
{!metadataIsOpen ? <ExpandMoreIcon /> : <ExpandLessIcon />}
</ListItem>
<Collapse in={!!metadataIsOpen} unmountOnExit>
<List
className="detail-drawer__nested-list"
dense
disablePadding
>
{metadata}
</List>
</Collapse>
<Divider />
{!isEdge && (
<>
<ListSubheader className="detail-drawer__relationships-subheader">
Relationships
</ListSubheader>
{!isEdge ? (
<RelationshipList
formatMetadata={formatMetadata}
formatOtherProps={formatOtherProps}
handleLinkExpand={handleLinkExpand}
linkOpen={linkOpen}
record={node}
/>
) : (
<ListItem dense>
<ListItemText
inset
primary="None"
primaryTypographyProps={{ color: 'textSecondary' }}
/>
</ListItem>
)}
</>
)}
</div>
);
}
return (
<Drawer
anchor="right"
classes={{
paper: `detail-drawer ${!drawerIsOpen
? 'detail-drawer--closed'
: ''
}`,
}}
open={drawerIsOpen}
variant="permanent"
>
{content}
</Drawer>
);
}
export default DetailDrawer;