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