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