Merge branch '14452-my-account'
[arvados.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 } 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
27 const renderName = (item: { name: string; uuid: string, kind: string }) =>
28     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
29         <Grid item>
30             {renderIcon(item)}
31         </Grid>
32         <Grid item>
33             <Typography color="primary" style={{ width: '450px' }}>
34                 {item.name}
35             </Typography>
36         </Grid>
37         <Grid item>
38             <Typography variant="caption">
39                 <FavoriteStar resourceUuid={item.uuid} />
40             </Typography>
41         </Grid>
42     </Grid>;
43
44 export const ResourceName = connect(
45     (state: RootState, props: { uuid: string }) => {
46         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
47         return resource || { name: '', uuid: '', kind: '' };
48     })(renderName);
49
50 const renderIcon = (item: { kind: string }) => {
51     switch (item.kind) {
52         case ResourceKind.PROJECT:
53             return <ProjectIcon />;
54         case ResourceKind.COLLECTION:
55             return <CollectionIcon />;
56         case ResourceKind.PROCESS:
57             return <ProcessIcon />;
58         case ResourceKind.WORKFLOW:
59             return <WorkflowIcon />;
60         default:
61             return <DefaultIcon />;
62     }
63 };
64
65 const renderDate = (date?: string) => {
66     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
67 };
68
69 const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
70     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
71         <Grid item>
72             {renderIcon(item)}
73         </Grid>
74         <Grid item>
75             <Typography color="primary" style={{ width: '100px' }}>
76                 {item.name}
77             </Typography>
78         </Grid>
79     </Grid>;
80
81 export const RosurceWorkflowName = connect(
82     (state: RootState, props: { uuid: string }) => {
83         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
84         return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
85     })(renderWorkflowName);
86
87 const getPublicUuid = (uuidPrefix: string) => {
88     return `${uuidPrefix}-tpzed-anonymouspublic`;
89 };
90
91 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
92     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
93     return (
94         <div>
95             {!isPublic && uuid &&
96                 <Tooltip title="Share">
97                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
98                         <ShareIcon />
99                     </IconButton>
100                 </Tooltip>
101             }
102         </div>
103     );
104 };
105
106 export const ResourceShare = connect(
107     (state: RootState, props: { uuid: string }) => {
108         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
109         const uuidPrefix = getUuidPrefix(state);
110         return {
111             uuid: resource ? resource.uuid : '',
112             ownerUuid: resource ? resource.ownerUuid : '',
113             uuidPrefix
114         };
115     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
116         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
117
118 const renderFirstName = (item: { firstName: string }) => {
119     return <Typography noWrap>{item.firstName}</Typography>;
120 };
121
122 export const ResourceFirstName = connect(
123     (state: RootState, props: { uuid: string }) => {
124         const resource = getResource<UserResource>(props.uuid)(state.resources);
125         return resource || { firstName: '' };
126     })(renderFirstName);
127
128 const renderLastName = (item: { lastName: string }) =>
129     <Typography noWrap>{item.lastName}</Typography>;
130
131 export const ResourceLastName = connect(
132     (state: RootState, props: { uuid: string }) => {
133         const resource = getResource<UserResource>(props.uuid)(state.resources);
134         return resource || { lastName: '' };
135     })(renderLastName);
136
137 const renderUuid = (item: { uuid: string }) =>
138     <Typography noWrap>{item.uuid}</Typography>;
139
140 export const ResourceUuid = connect(
141     (state: RootState, props: { uuid: string }) => {
142         const resource = getResource<UserResource>(props.uuid)(state.resources);
143         return resource || { uuid: '' };
144     })(renderUuid);
145
146 const renderEmail = (item: { email: string }) =>
147     <Typography noWrap>{item.email}</Typography>;
148
149 export const ResourceEmail = connect(
150     (state: RootState, props: { uuid: string }) => {
151         const resource = getResource<UserResource>(props.uuid)(state.resources);
152         return resource || { email: '' };
153     })(renderEmail);
154
155 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
156     <Checkbox
157         color="primary"
158         checked={props.isActive}
159         onClick={() => props.toggleIsActive(props.uuid)} />;
160
161 export const ResourceIsActive = connect(
162     (state: RootState, props: { uuid: string }) => {
163         const resource = getResource<UserResource>(props.uuid)(state.resources);
164         return resource || { isActive: false };
165     }, { toggleIsActive }
166 )(renderIsActive);
167
168 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
169     <Checkbox
170         color="primary"
171         checked={props.isAdmin}
172         onClick={() => props.toggleIsAdmin(props.uuid)} />;
173
174 export const ResourceIsAdmin = connect(
175     (state: RootState, props: { uuid: string }) => {
176         const resource = getResource<UserResource>(props.uuid)(state.resources);
177         return resource || { isAdmin: false };
178     }, { toggleIsAdmin }
179 )(renderIsAdmin);
180
181 const renderUsername = (item: { username: string }) =>
182     <Typography noWrap>{item.username}</Typography>;
183
184 export const ResourceUsername = connect(
185     (state: RootState, props: { uuid: string }) => {
186         const resource = getResource<UserResource>(props.uuid)(state.resources);
187         return resource || { username: '' };
188     })(renderUsername);
189
190 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
191     return (
192         <div>
193             {uuid &&
194                 <Tooltip title="Run process">
195                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
196                         <ProcessIcon />
197                     </IconButton>
198                 </Tooltip>}
199         </div>
200     );
201 };
202
203 export const ResourceRunProcess = connect(
204     (state: RootState, props: { uuid: string }) => {
205         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
206         return {
207             uuid: resource ? resource.uuid : ''
208         };
209     })((props: { uuid: string } & DispatchProp<any>) =>
210         resourceRunProcess(props.dispatch, props.uuid));
211
212 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
213     if (ownerUuid === getPublicUuid(uuidPrefix)) {
214         return renderStatus(ResourceStatus.PUBLIC);
215     } else {
216         return renderStatus(ResourceStatus.PRIVATE);
217     }
218 };
219
220 const renderStatus = (status: string) =>
221     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
222
223 export const ResourceWorkflowStatus = connect(
224     (state: RootState, props: { uuid: string }) => {
225         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
226         const uuidPrefix = getUuidPrefix(state);
227         return {
228             ownerUuid: resource ? resource.ownerUuid : '',
229             uuidPrefix
230         };
231     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
232
233 export const ResourceLastModifiedDate = connect(
234     (state: RootState, props: { uuid: string }) => {
235         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
236         return { date: resource ? resource.modifiedAt : '' };
237     })((props: { date: string }) => renderDate(props.date));
238
239 export const ResourceTrashDate = connect(
240     (state: RootState, props: { uuid: string }) => {
241         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
242         return { date: resource ? resource.trashAt : '' };
243     })((props: { date: string }) => renderDate(props.date));
244
245 export const ResourceDeleteDate = connect(
246     (state: RootState, props: { uuid: string }) => {
247         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
248         return { date: resource ? resource.deleteAt : '' };
249     })((props: { date: string }) => renderDate(props.date));
250
251 export const renderFileSize = (fileSize?: number) =>
252     <Typography noWrap style={{ minWidth: '45px' }}>
253         {formatFileSize(fileSize)}
254     </Typography>;
255
256 export const ResourceFileSize = connect(
257     (state: RootState, props: { uuid: string }) => {
258         const resource = getResourceData(props.uuid)(state.resourcesData);
259         return { fileSize: resource ? resource.fileSize : 0 };
260     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
261
262 const renderOwner = (owner: string) =>
263     <Typography noWrap color="primary" >
264         {owner}
265     </Typography>;
266
267 export const ResourceOwner = connect(
268     (state: RootState, props: { uuid: string }) => {
269         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
270         return { owner: resource ? resource.ownerUuid : '' };
271     })((props: { owner: string }) => renderOwner(props.owner));
272
273 const renderType = (type: string) =>
274     <Typography noWrap>
275         {resourceLabel(type)}
276     </Typography>;
277
278 export const ResourceType = connect(
279     (state: RootState, props: { uuid: string }) => {
280         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
281         return { type: resource ? resource.kind : '' };
282     })((props: { type: string }) => renderType(props.type));
283
284 export const ProcessStatus = compose(
285     connect((state: RootState, props: { uuid: string }) => {
286         return { process: getProcess(props.uuid)(state.resources) };
287     }),
288     withStyles({}, { withTheme: true }))
289     ((props: { process?: Process, theme: ArvadosTheme }) => {
290         const status = props.process ? getProcessStatus(props.process) : "-";
291         return <Typography
292             noWrap
293             align="center"
294             style={{ color: getProcessStatusColor(status, props.theme) }} >
295             {status}
296         </Typography>;
297     });