data-table-columns-change
[arvados-workbench2.git] / src / views-components / data-explorer / renderers.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Grid, Typography, withStyles, Tooltip, IconButton, Checkbox } from '@material-ui/core';
7 import { FavoriteStar, PublicFavoriteStar } from '../favorite-star/favorite-star';
8 import { ResourceKind, TrashableResource } from '~/models/resource';
9 import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, WorkflowIcon, ShareIcon } from '~/components/icon/icon';
10 import { formatDate, formatFileSize } from '~/common/formatters';
11 import { resourceLabel } from '~/common/labels';
12 import { connect, DispatchProp } from 'react-redux';
13 import { RootState } from '~/store/store';
14 import { getResource } from '~/store/resources/resources';
15 import { GroupContentsResource } from '~/services/groups-service/groups-service';
16 import { getProcess, Process, getProcessStatus, getProcessStatusColor } from '~/store/processes/process';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { compose, Dispatch } from 'redux';
19 import { WorkflowResource } from '~/models/workflow';
20 import { ResourceStatus } from '~/views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from '~/store/workflow-panel/workflow-panel-actions';
22 import { getResourceData } from "~/store/resources-data/resources-data";
23 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
24 import { UserResource } from '~/models/user';
25 import { toggleIsActive, toggleIsAdmin } from '~/store/users/users-actions';
26 import { LinkResource } from '~/models/link';
27 import { navigateTo } from '~/store/navigation/navigation-action';
28 import { withResourceData } from '~/views-components/data-explorer/with-resources';
29 import { extractUuidKind } from '~/models/resource';
30
31 const renderName = (item: { name: string; uuid: string, kind: string }) =>
32     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
33         <Grid item>
34             {renderIcon(item.kind)}
35         </Grid>
36         <Grid item>
37             <Typography color="primary" style={{ width: 'auto' }}>
38                 {item.name}
39             </Typography>
40         </Grid>
41         <Grid item>
42             <Typography variant="caption">
43                 <FavoriteStar resourceUuid={item.uuid} />
44                 <PublicFavoriteStar resourceUuid={item.uuid} />
45             </Typography>
46         </Grid>
47     </Grid>;
48
49 export const ResourceName = connect(
50     (state: RootState, props: { uuid: string }) => {
51         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
52         return resource || { name: '', uuid: '', kind: '' };
53     })(renderName);
54
55 const renderIcon = (kind: string) => {
56     switch (kind) {
57         case ResourceKind.PROJECT:
58             return <ProjectIcon />;
59         case ResourceKind.COLLECTION:
60             return <CollectionIcon />;
61         case ResourceKind.PROCESS:
62             return <ProcessIcon />;
63         case ResourceKind.WORKFLOW:
64             return <WorkflowIcon />;
65         default:
66             return <DefaultIcon />;
67     }
68 };
69
70 const renderDate = (date?: string) => {
71     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
72 };
73
74 const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
75     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
76         <Grid item>
77             {renderIcon(item.kind)}
78         </Grid>
79         <Grid item>
80             <Typography color="primary" style={{ width: '100px' }}>
81                 {item.name}
82             </Typography>
83         </Grid>
84     </Grid>;
85
86 export const ResourceWorkflowName = connect(
87     (state: RootState, props: { uuid: string }) => {
88         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
89         return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
90     })(renderWorkflowName);
91
92 const getPublicUuid = (uuidPrefix: string) => {
93     return `${uuidPrefix}-tpzed-anonymouspublic`;
94 };
95
96 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
97     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
98     return (
99         <div>
100             {!isPublic && uuid &&
101                 <Tooltip title="Share">
102                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
103                         <ShareIcon />
104                     </IconButton>
105                 </Tooltip>
106             }
107         </div>
108     );
109 };
110
111 export const ResourceShare = connect(
112     (state: RootState, props: { uuid: string }) => {
113         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
114         const uuidPrefix = getUuidPrefix(state);
115         return {
116             uuid: resource ? resource.uuid : '',
117             ownerUuid: resource ? resource.ownerUuid : '',
118             uuidPrefix
119         };
120     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
121         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
122
123 const renderFirstName = (item: { firstName: string }) => {
124     return <Typography noWrap>{item.firstName}</Typography>;
125 };
126
127 // User Resources
128 export const ResourceFirstName = connect(
129     (state: RootState, props: { uuid: string }) => {
130         const resource = getResource<UserResource>(props.uuid)(state.resources);
131         return resource || { firstName: '' };
132     })(renderFirstName);
133
134 const renderLastName = (item: { lastName: string }) =>
135     <Typography noWrap>{item.lastName}</Typography>;
136
137 export const ResourceLastName = connect(
138     (state: RootState, props: { uuid: string }) => {
139         const resource = getResource<UserResource>(props.uuid)(state.resources);
140         return resource || { lastName: '' };
141     })(renderLastName);
142
143 const renderUuid = (item: { uuid: string }) =>
144     <Typography noWrap>{item.uuid}</Typography>;
145
146 export const ResourceUuid = connect(
147     (state: RootState, props: { uuid: string }) => {
148         const resource = getResource<UserResource>(props.uuid)(state.resources);
149         return resource || { uuid: '' };
150     })(renderUuid);
151
152 const renderEmail = (item: { email: string }) =>
153     <Typography noWrap>{item.email}</Typography>;
154
155 export const ResourceEmail = connect(
156     (state: RootState, props: { uuid: string }) => {
157         const resource = getResource<UserResource>(props.uuid)(state.resources);
158         return resource || { email: '' };
159     })(renderEmail);
160
161 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
162     <Checkbox
163         color="primary"
164         checked={props.isActive}
165         onClick={() => props.toggleIsActive(props.uuid)} />;
166
167 export const ResourceIsActive = connect(
168     (state: RootState, props: { uuid: string }) => {
169         const resource = getResource<UserResource>(props.uuid)(state.resources);
170         return resource || { isActive: false };
171     }, { toggleIsActive }
172 )(renderIsActive);
173
174 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
175     <Checkbox
176         color="primary"
177         checked={props.isAdmin}
178         onClick={() => props.toggleIsAdmin(props.uuid)} />;
179
180 export const ResourceIsAdmin = connect(
181     (state: RootState, props: { uuid: string }) => {
182         const resource = getResource<UserResource>(props.uuid)(state.resources);
183         return resource || { isAdmin: false };
184     }, { toggleIsAdmin }
185 )(renderIsAdmin);
186
187 const renderUsername = (item: { username: string }) =>
188     <Typography noWrap>{item.username}</Typography>;
189
190 export const ResourceUsername = connect(
191     (state: RootState, props: { uuid: string }) => {
192         const resource = getResource<UserResource>(props.uuid)(state.resources);
193         return resource || { username: '' };
194     })(renderUsername);
195
196 // Common methods
197 const renderCommonData = (data: string) =>
198     <Typography noWrap>{data}</Typography>;
199
200 const renderCommonDate = (date: string) =>
201     <Typography noWrap>{formatDate(date)}</Typography>;
202
203 export const CommonUuid = withResourceData('uuid', renderCommonData);
204
205 // Api Client Authorizations
206 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
207
208 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
209
210 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
211
212 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
213
214 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
215
216 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
217
218 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
219
220 export const TokenScopes = withResourceData('scopes', renderCommonData);
221
222 export const TokenUserId = withResourceData('userId', renderCommonData);
223
224 // Compute Node Resources
225 const renderNodeInfo = (data: string) => {
226     return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
227 };
228
229 const clusterColors = [
230     ['#f44336', '#fff'],
231     ['#2196f3', '#fff'],
232     ['#009688', '#fff'],
233     ['#cddc39', '#fff'],
234     ['#ff9800', '#fff']
235 ];
236
237 export const ResourceCluster = (props: { uuid: string }) => {
238     const CLUSTER_ID_LENGTH = 5;
239     const pos = props.uuid.indexOf('-');
240     const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
241     const ci = pos >= CLUSTER_ID_LENGTH ? (props.uuid.charCodeAt(0) + props.uuid.charCodeAt(1)) % clusterColors.length : 0;
242     return <Typography>
243         <div style={{
244             backgroundColor: clusterColors[ci][0],
245             color: clusterColors[ci][1],
246             padding: "2px 7px",
247             borderRadius: 3
248         }}>{clusterId}</div>
249     </Typography>;
250 };
251
252 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
253
254 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
255
256 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
257
258 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
259
260 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
261
262 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
263
264 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
265
266 // Links Resources
267 const renderLinkName = (item: { name: string }) =>
268     <Typography noWrap>{item.name || '(none)'}</Typography>;
269
270 export const ResourceLinkName = connect(
271     (state: RootState, props: { uuid: string }) => {
272         const resource = getResource<LinkResource>(props.uuid)(state.resources);
273         return resource || { name: '' };
274     })(renderLinkName);
275
276 const renderLinkClass = (item: { linkClass: string }) =>
277     <Typography noWrap>{item.linkClass}</Typography>;
278
279 export const ResourceLinkClass = connect(
280     (state: RootState, props: { uuid: string }) => {
281         const resource = getResource<LinkResource>(props.uuid)(state.resources);
282         return resource || { linkClass: '' };
283     })(renderLinkClass);
284
285 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
286     const currentLabel = resourceLabel(item.tailKind);
287     const isUnknow = currentLabel === "Unknown";
288     return (<div>
289         {!isUnknow ? (
290             renderLink(dispatch, item.tailUuid, currentLabel)
291         ) : (
292                 <Typography noWrap color="default">
293                     {item.tailUuid}
294                 </Typography>
295             )}
296     </div>);
297 };
298
299 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
300     <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
301         {label}: {uuid}
302     </Typography>;
303
304 export const ResourceLinkTail = connect(
305     (state: RootState, props: { uuid: string }) => {
306         const resource = getResource<LinkResource>(props.uuid)(state.resources);
307         return {
308             item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
309         };
310     })((props: { item: any } & DispatchProp<any>) =>
311         renderLinkTail(props.dispatch, props.item));
312
313 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
314     renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
315
316 export const ResourceLinkHead = connect(
317     (state: RootState, props: { uuid: string }) => {
318         const resource = getResource<LinkResource>(props.uuid)(state.resources);
319         return {
320             item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
321         };
322     })((props: { item: any } & DispatchProp<any>) =>
323         renderLinkHead(props.dispatch, props.item));
324
325 export const ResourceLinkUuid = connect(
326     (state: RootState, props: { uuid: string }) => {
327         const resource = getResource<LinkResource>(props.uuid)(state.resources);
328         return resource || { uuid: '' };
329     })(renderUuid);
330
331 // Process Resources
332 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
333     return (
334         <div>
335             {uuid &&
336                 <Tooltip title="Run process">
337                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
338                         <ProcessIcon />
339                     </IconButton>
340                 </Tooltip>}
341         </div>
342     );
343 };
344
345 export const ResourceRunProcess = connect(
346     (state: RootState, props: { uuid: string }) => {
347         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
348         return {
349             uuid: resource ? resource.uuid : ''
350         };
351     })((props: { uuid: string } & DispatchProp<any>) =>
352         resourceRunProcess(props.dispatch, props.uuid));
353
354 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
355     if (ownerUuid === getPublicUuid(uuidPrefix)) {
356         return renderStatus(ResourceStatus.PUBLIC);
357     } else {
358         return renderStatus(ResourceStatus.PRIVATE);
359     }
360 };
361
362 const renderStatus = (status: string) =>
363     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
364
365 export const ResourceWorkflowStatus = connect(
366     (state: RootState, props: { uuid: string }) => {
367         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
368         const uuidPrefix = getUuidPrefix(state);
369         return {
370             ownerUuid: resource ? resource.ownerUuid : '',
371             uuidPrefix
372         };
373     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
374
375 export const ResourceLastModifiedDate = connect(
376     (state: RootState, props: { uuid: string }) => {
377         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
378         return { date: resource ? resource.modifiedAt : '' };
379     })((props: { date: string }) => renderDate(props.date));
380
381 export const ResourceTrashDate = connect(
382     (state: RootState, props: { uuid: string }) => {
383         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
384         return { date: resource ? resource.trashAt : '' };
385     })((props: { date: string }) => renderDate(props.date));
386
387 export const ResourceDeleteDate = connect(
388     (state: RootState, props: { uuid: string }) => {
389         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
390         return { date: resource ? resource.deleteAt : '' };
391     })((props: { date: string }) => renderDate(props.date));
392
393 export const renderFileSize = (fileSize?: number) =>
394     <Typography noWrap style={{ minWidth: '45px' }}>
395         {formatFileSize(fileSize)}
396     </Typography>;
397
398 export const ResourceFileSize = connect(
399     (state: RootState, props: { uuid: string }) => {
400         const resource = getResourceData(props.uuid)(state.resourcesData);
401         return { fileSize: resource ? resource.fileSize : 0 };
402     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
403
404 const renderOwner = (owner: string) =>
405     <Typography noWrap color="primary" >
406         {owner}
407     </Typography>;
408
409 export const ResourceOwner = connect(
410     (state: RootState, props: { uuid: string }) => {
411         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
412         return { owner: resource ? resource.ownerUuid : '' };
413     })((props: { owner: string }) => renderOwner(props.owner));
414
415 const renderType = (type: string) =>
416     <Typography noWrap>
417         {resourceLabel(type)}
418     </Typography>;
419
420 export const ResourceType = connect(
421     (state: RootState, props: { uuid: string }) => {
422         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
423         return { type: resource ? resource.kind : '' };
424     })((props: { type: string }) => renderType(props.type));
425
426 export const ProcessStatus = compose(
427     connect((state: RootState, props: { uuid: string }) => {
428         return { process: getProcess(props.uuid)(state.resources) };
429     }),
430     withStyles({}, { withTheme: true }))
431     ((props: { process?: Process, theme: ArvadosTheme }) => {
432         const status = props.process ? getProcessStatus(props.process) : "-";
433         return <Typography
434             noWrap
435             align="center"
436             style={{ color: getProcessStatusColor(status, props.theme) }} >
437             {status}
438         </Typography>;
439     });