workflow-view-without-working-services
[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
20 export const renderName = (item: { name: string; uuid: string, kind: string }) =>
21     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
22         <Grid item>
23             {renderIcon(item)}
24         </Grid>
25         <Grid item>
26             <Typography color="primary" style={{ width: '450px' }}>
27                 {item.name}
28             </Typography>
29         </Grid>
30         <Grid item>
31             <Typography variant="caption">
32                 <FavoriteStar resourceUuid={item.uuid} />
33             </Typography>
34         </Grid>
35     </Grid>;
36
37 export const ResourceName = connect(
38     (state: RootState, props: { uuid: string }) => {
39         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
40         return resource || { name: '', uuid: '', kind: '' };
41     })(renderName);
42
43 export const renderIcon = (item: { kind: string }) => {
44     switch (item.kind) {
45         case ResourceKind.PROJECT:
46             return <ProjectIcon />;
47         case ResourceKind.COLLECTION:
48             return <CollectionIcon />;
49         case ResourceKind.PROCESS:
50             return <ProcessIcon />;
51         case ResourceKind.WORKFLOW:
52             return <WorkflowIcon />;
53         default:
54             return <DefaultIcon />;
55     }
56 };
57
58 export const renderDate = (date?: string) => {
59     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
60 };
61
62 export const ResourceLastModifiedDate = connect(
63     (state: RootState, props: { uuid: string }) => {
64         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
65         return { date: resource ? resource.modifiedAt : '' };
66     })((props: { date: string }) => renderDate(props.date));
67
68 export const ResourceTrashDate = connect(
69     (state: RootState, props: { uuid: string }) => {
70         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
71         return { date: resource ? resource.trashAt : '' };
72     })((props: { date: string }) => renderDate(props.date));
73
74 export const ResourceDeleteDate = connect(
75     (state: RootState, props: { uuid: string }) => {
76         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
77         return { date: resource ? resource.deleteAt : '' };
78     })((props: { date: string }) => renderDate(props.date));
79
80 export const renderFileSize = (fileSize?: number) =>
81     <Typography noWrap style={{ minWidth: '45px' }}>
82         {formatFileSize(fileSize)}
83     </Typography>;
84
85 export const ResourceFileSize = connect(
86     (state: RootState, props: { uuid: string }) => {
87         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
88         return {};
89     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
90
91 export const renderOwner = (owner: string) =>
92     <Typography noWrap color="primary" >
93         {owner}
94     </Typography>;
95
96 export const ResourceOwner = connect(
97     (state: RootState, props: { uuid: string }) => {
98         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
99         return { owner: resource ? resource.ownerUuid : '' };
100     })((props: { owner: string }) => renderOwner(props.owner));
101
102 export const renderType = (type: string) =>
103     <Typography noWrap>
104         {resourceLabel(type)}
105     </Typography>;
106
107 export const ResourceType = connect(
108     (state: RootState, props: { uuid: string }) => {
109         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
110         return { type: resource ? resource.kind : '' };
111     })((props: { type: string }) => renderType(props.type));
112
113 export const ProcessStatus = compose(
114     connect((state: RootState, props: { uuid: string }) => {
115         return { process: getProcess(props.uuid)(state.resources) };
116     }),
117     withStyles({}, { withTheme: true }))
118     ((props: { process?: Process, theme: ArvadosTheme }) => {
119         const status = props.process ? getProcessStatus(props.process) : "-";
120         return <Typography
121             noWrap
122             align="center"
123             style={{ color: getProcessStatusColor(status, props.theme) }} >
124             {status}
125         </Typography>;
126     });