refs #14525 fix breadcamps and workflow share icon
[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 } 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 } 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
25 export const renderName = (item: { name: string; uuid: string, kind: string }) =>
26     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
27         <Grid item>
28             {renderIcon(item)}
29         </Grid>
30         <Grid item>
31             <Typography color="primary" style={{ width: '450px' }}>
32                 {item.name}
33             </Typography>
34         </Grid>
35         <Grid item>
36             <Typography variant="caption">
37                 <FavoriteStar resourceUuid={item.uuid} />
38             </Typography>
39         </Grid>
40     </Grid>;
41
42 export const ResourceName = connect(
43     (state: RootState, props: { uuid: string }) => {
44         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
45         return resource || { name: '', uuid: '', kind: '' };
46     })(renderName);
47
48 export const renderIcon = (item: { kind: string }) => {
49     switch (item.kind) {
50         case ResourceKind.PROJECT:
51             return <ProjectIcon />;
52         case ResourceKind.COLLECTION:
53             return <CollectionIcon />;
54         case ResourceKind.PROCESS:
55             return <ProcessIcon />;
56         case ResourceKind.WORKFLOW:
57             return <WorkflowIcon />;
58         default:
59             return <DefaultIcon />;
60     }
61 };
62
63 export const renderDate = (date?: string) => {
64     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
65 };
66
67 export const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
68     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
69         <Grid item>
70             {renderIcon(item)}
71         </Grid>
72         <Grid item>
73             <Typography color="primary" style={{ width: '100px' }}>
74                 {item.name}
75             </Typography>
76         </Grid>
77     </Grid>;
78
79 export const RosurceWorkflowName = connect(
80     (state: RootState, props: { uuid: string }) => {
81         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
82         return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
83     })(renderWorkflowName);
84
85 const getPublicUuid = (uuidPrefix: string) => {
86     return `${uuidPrefix}-tpzed-anonymouspublic`;
87 };
88
89 // ToDo: share onClick
90 export const resourceShare = (uuidPrefix: string, ownerUuid?: string) => {
91     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
92     return (
93         <div>
94             { isPublic &&
95                 <Tooltip title="Share">
96                     <IconButton onClick={() => undefined}>
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             ownerUuid: resource ? resource.ownerUuid : '',
111             uuidPrefix
112         };
113     })((props: { ownerUuid?: string, uuidPrefix: string }) => resourceShare(props.uuidPrefix, props.ownerUuid));
114
115 export const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
116     if (ownerUuid === getPublicUuid(uuidPrefix)) {
117         return renderStatus(ResourceStatus.PUBLIC);
118     } else {
119         return renderStatus(ResourceStatus.PRIVATE);
120     }
121 };
122
123 const renderStatus = (status: string) =>
124     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
125
126 export const ResourceWorkflowStatus = connect(
127     (state: RootState, props: { uuid: string }) => {
128         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
129         const uuidPrefix = getUuidPrefix(state);
130         return {
131             ownerUuid: resource ? resource.ownerUuid : '',
132             uuidPrefix
133         };
134     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
135
136 export const ResourceLastModifiedDate = connect(
137     (state: RootState, props: { uuid: string }) => {
138         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
139         return { date: resource ? resource.modifiedAt : '' };
140     })((props: { date: string }) => renderDate(props.date));
141
142 export const ResourceTrashDate = connect(
143     (state: RootState, props: { uuid: string }) => {
144         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
145         return { date: resource ? resource.trashAt : '' };
146     })((props: { date: string }) => renderDate(props.date));
147
148 export const ResourceDeleteDate = connect(
149     (state: RootState, props: { uuid: string }) => {
150         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
151         return { date: resource ? resource.deleteAt : '' };
152     })((props: { date: string }) => renderDate(props.date));
153
154 export const renderFileSize = (fileSize?: number) =>
155     <Typography noWrap style={{ minWidth: '45px' }}>
156         {formatFileSize(fileSize)}
157     </Typography>;
158
159 export const ResourceFileSize = connect(
160     (state: RootState, props: { uuid: string }) => {
161         const resource = getResourceData(props.uuid)(state.resourcesData);
162         return { fileSize: resource ? resource.fileSize : 0 };
163     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
164
165 export const renderOwner = (owner: string) =>
166     <Typography noWrap color="primary" >
167         {owner}
168     </Typography>;
169
170 export const ResourceOwner = connect(
171     (state: RootState, props: { uuid: string }) => {
172         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
173         return { owner: resource ? resource.ownerUuid : '' };
174     })((props: { owner: string }) => renderOwner(props.owner));
175
176 export const renderType = (type: string) =>
177     <Typography noWrap>
178         {resourceLabel(type)}
179     </Typography>;
180
181 export const ResourceType = connect(
182     (state: RootState, props: { uuid: string }) => {
183         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
184         return { type: resource ? resource.kind : '' };
185     })((props: { type: string }) => renderType(props.type));
186
187 export const ProcessStatus = compose(
188     connect((state: RootState, props: { uuid: string }) => {
189         return { process: getProcess(props.uuid)(state.resources) };
190     }),
191     withStyles({}, { withTheme: true }))
192     ((props: { process?: Process, theme: ArvadosTheme }) => {
193         const status = props.process ? getProcessStatus(props.process) : "-";
194         return <Typography
195             noWrap
196             align="center"
197             style={{ color: getProcessStatusColor(status, props.theme) }} >
198             {status}
199         </Typography>;
200     });