72644f93af2e9cea7c5a93e965cff53793f8f026
[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 } 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">
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         default:
52             return <DefaultIcon />;
53     }
54 };
55
56 export const renderDate = (date?: string) => {
57     return <Typography noWrap>{formatDate(date)}</Typography>;
58 };
59
60 export const ResourceLastModifiedDate = connect(
61     (state: RootState, props: { uuid: string }) => {
62         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
63         return { date: resource ? resource.modifiedAt : '' };
64     })((props: { date: string }) => renderDate(props.date));
65
66 export const ResourceTrashDate = connect(
67     (state: RootState, props: { uuid: string }) => {
68         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
69         return { date: resource ? resource.trashAt : '' };
70     })((props: { date: string }) => renderDate(props.date));
71
72 export const ResourceDeleteDate = connect(
73     (state: RootState, props: { uuid: string }) => {
74         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
75         return { date: resource ? resource.deleteAt : '' };
76     })((props: { date: string }) => renderDate(props.date));
77
78 export const renderFileSize = (fileSize?: number) =>
79     <Typography noWrap>
80         {formatFileSize(fileSize)}
81     </Typography>;
82
83 export const ResourceFileSize = connect(
84     (state: RootState, props: { uuid: string }) => {
85         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
86         return {};
87     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
88
89 export const renderOwner = (owner: string) =>
90     <Typography noWrap color="primary" >
91         {owner}
92     </Typography>;
93
94 export const ResourceOwner = connect(
95     (state: RootState, props: { uuid: string }) => {
96         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
97         return { owner: resource ? resource.ownerUuid : '' };
98     })((props: { owner: string }) => renderOwner(props.owner));
99
100 export const renderType = (type: string) =>
101     <Typography noWrap>
102         {resourceLabel(type)}
103     </Typography>;
104
105 export const ResourceType = connect(
106     (state: RootState, props: { uuid: string }) => {
107         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
108         return { type: resource ? resource.kind : '' };
109     })((props: { type: string }) => renderType(props.type));
110
111 export const ProcessStatus = compose(
112     connect((state: RootState, props: { uuid: string }) => {
113         return { process: getProcess(props.uuid)(state.resources) };
114     }),
115     withStyles({}, { withTheme: true }))
116     ((props: { process?: Process, theme: ArvadosTheme }) => {
117         const status = props.process ? getProcessStatus(props.process) : "-";
118         return <Typography
119             noWrap
120             align="center"
121             style={{ color: getProcessStatusColor(status, props.theme) }} >
122             {status}
123         </Typography>;
124     });