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