d0266adc539358bf0aeb48ec135dbb37fe4c5b06
[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 } 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 } from '~/store/workflow-panel/workflow-panel-actions';
22 import { CollectionResource } from "~/models/collection";
23 import { getResourceData } from "~/store/resources-data/resources-data";
24 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
25
26 export 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 export 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 export const renderDate = (date?: string) => {
65     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
66 };
67
68 export 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 // ToDo: share onClick
91 export 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 export const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
119     if (ownerUuid === getPublicUuid(uuidPrefix)) {
120         return renderStatus(ResourceStatus.PUBLIC);
121     } else {
122         return renderStatus(ResourceStatus.PRIVATE);
123     }
124 };
125
126 const renderStatus = (status: string) =>
127     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
128
129 export const ResourceWorkflowStatus = connect(
130     (state: RootState, props: { uuid: string }) => {
131         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
132         const uuidPrefix = getUuidPrefix(state);
133         return {
134             ownerUuid: resource ? resource.ownerUuid : '',
135             uuidPrefix
136         };
137     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
138
139 export const ResourceLastModifiedDate = connect(
140     (state: RootState, props: { uuid: string }) => {
141         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
142         return { date: resource ? resource.modifiedAt : '' };
143     })((props: { date: string }) => renderDate(props.date));
144
145 export const ResourceTrashDate = connect(
146     (state: RootState, props: { uuid: string }) => {
147         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
148         return { date: resource ? resource.trashAt : '' };
149     })((props: { date: string }) => renderDate(props.date));
150
151 export const ResourceDeleteDate = connect(
152     (state: RootState, props: { uuid: string }) => {
153         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
154         return { date: resource ? resource.deleteAt : '' };
155     })((props: { date: string }) => renderDate(props.date));
156
157 export const renderFileSize = (fileSize?: number) =>
158     <Typography noWrap style={{ minWidth: '45px' }}>
159         {formatFileSize(fileSize)}
160     </Typography>;
161
162 export const ResourceFileSize = connect(
163     (state: RootState, props: { uuid: string }) => {
164         const resource = getResourceData(props.uuid)(state.resourcesData);
165         return { fileSize: resource ? resource.fileSize : 0 };
166     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
167
168 export const renderOwner = (owner: string) =>
169     <Typography noWrap color="primary" >
170         {owner}
171     </Typography>;
172
173 export const ResourceOwner = connect(
174     (state: RootState, props: { uuid: string }) => {
175         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
176         return { owner: resource ? resource.ownerUuid : '' };
177     })((props: { owner: string }) => renderOwner(props.owner));
178
179 export const renderType = (type: string) =>
180     <Typography noWrap>
181         {resourceLabel(type)}
182     </Typography>;
183
184 export const ResourceType = connect(
185     (state: RootState, props: { uuid: string }) => {
186         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
187         return { type: resource ? resource.kind : '' };
188     })((props: { type: string }) => renderType(props.type));
189
190 export const ProcessStatus = compose(
191     connect((state: RootState, props: { uuid: string }) => {
192         return { process: getProcess(props.uuid)(state.resources) };
193     }),
194     withStyles({}, { withTheme: true }))
195     ((props: { process?: Process, theme: ArvadosTheme }) => {
196         const status = props.process ? getProcessStatus(props.process) : "-";
197         return <Typography
198             noWrap
199             align="center"
200             style={{ color: getProcessStatusColor(status, props.theme) }} >
201             {status}
202         </Typography>;
203     });