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