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