auth
[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 } 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 } 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 renderWorkflowName = (item: { name: string; uuid: string, kind: string }) =>
61     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
62         <Grid item>
63             {renderIcon(item)}
64         </Grid>
65         <Grid item>
66             <Typography color="primary" style={{ width: '100px' }}>
67                 {item.name}
68             </Typography>
69         </Grid>
70     </Grid>;
71
72 export const RosurceWorkflowName = connect(
73     (state: RootState, props: { uuid: string }) => {
74         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
75         return resource || { name: '', uuid: '', kind: '' };
76     })(renderWorkflowName);
77
78 export const renderDate = (date?: string) => {
79     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
80 };
81
82 export const renderWorkflowStatus = (ownerUuid?: string) => {
83     if (ownerUuid === 'qr1hi-j7d0g-2ax8o1pscovq2lg') {
84         return <Typography noWrap style={{ width: '60px' }}>{ResourceStatus.PUBLIC}</Typography>;
85     } else {
86         return <Typography noWrap style={{ width: '60px' }}>{ResourceStatus.PRIVATE}</Typography>;
87     }
88 };
89
90 export const ResourceWorkflowStatus = connect(
91     (state: RootState, props: { uuid: string }) => {
92         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
93         return { ownerUuid: resource ? resource.ownerUuid : '' };
94     })((props: { ownerUuid?: string }) => renderWorkflowStatus(props.ownerUuid));
95
96 export const ResourceLastModifiedDate = connect(
97     (state: RootState, props: { uuid: string }) => {
98         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
99         return { date: resource ? resource.modifiedAt : '' };
100     })((props: { date: string }) => renderDate(props.date));
101
102 export const ResourceTrashDate = connect(
103     (state: RootState, props: { uuid: string }) => {
104         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
105         return { date: resource ? resource.trashAt : '' };
106     })((props: { date: string }) => renderDate(props.date));
107
108 export const ResourceDeleteDate = connect(
109     (state: RootState, props: { uuid: string }) => {
110         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
111         return { date: resource ? resource.deleteAt : '' };
112     })((props: { date: string }) => renderDate(props.date));
113
114 export const renderFileSize = (fileSize?: number) =>
115     <Typography noWrap style={{ minWidth: '45px' }}>
116         {formatFileSize(fileSize)}
117     </Typography>;
118
119 export const ResourceFileSize = connect(
120     (state: RootState, props: { uuid: string }) => {
121         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
122         return {};
123     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
124
125 export const renderOwner = (owner: string) =>
126     <Typography noWrap color="primary" >
127         {owner}
128     </Typography>;
129
130 export const ResourceOwner = connect(
131     (state: RootState, props: { uuid: string }) => {
132         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
133         return { owner: resource ? resource.ownerUuid : '' };
134     })((props: { owner: string }) => renderOwner(props.owner));
135
136 export const renderType = (type: string) =>
137     <Typography noWrap>
138         {resourceLabel(type)}
139     </Typography>;
140
141 export const ResourceType = connect(
142     (state: RootState, props: { uuid: string }) => {
143         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
144         return { type: resource ? resource.kind : '' };
145     })((props: { type: string }) => renderType(props.type));
146
147 export const ProcessStatus = compose(
148     connect((state: RootState, props: { uuid: string }) => {
149         return { process: getProcess(props.uuid)(state.resources) };
150     }),
151     withStyles({}, { withTheme: true }))
152     ((props: { process?: Process, theme: ArvadosTheme }) => {
153         const status = props.process ? getProcessStatus(props.process) : "-";
154         return <Typography
155             noWrap
156             align="center"
157             style={{ color: getProcessStatusColor(status, props.theme) }} >
158             {status}
159         </Typography>;
160     });