refs #master Merge branch 'origin/master' into 13828-trash-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 } from '@material-ui/core';
7 import { FavoriteStar } from '../favorite-star/favorite-star';
8 import { ResourceKind } 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 { ProcessResource } from '~/models/process';
17
18
19 export const renderName = (item: { name: string; uuid: string, kind: string }) =>
20     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
21         <Grid item>
22             {renderIcon(item)}
23         </Grid>
24         <Grid item>
25             <Typography color="primary">
26                 {item.name}
27             </Typography>
28         </Grid>
29         <Grid item>
30             <Typography variant="caption">
31                 <FavoriteStar resourceUuid={item.uuid} />
32             </Typography>
33         </Grid>
34     </Grid>;
35
36 export const ResourceName = connect(
37     (state: RootState, props: { uuid: string }) => {
38         const resource = getResource(props.uuid)(state.resources) as GroupContentsResource | undefined;
39         return resource || { name: '', uuid: '', kind: '' };
40     })(renderName);
41
42 export const renderIcon = (item: { kind: string }) => {
43     switch (item.kind) {
44         case ResourceKind.PROJECT:
45             return <ProjectIcon />;
46         case ResourceKind.COLLECTION:
47             return <CollectionIcon />;
48         case ResourceKind.PROCESS:
49             return <ProcessIcon />;
50         default:
51             return <DefaultIcon />;
52     }
53 };
54
55 export const renderDate = (date?: string) => {
56     return <Typography noWrap>{formatDate(date)}</Typography>;
57 };
58
59 export const ResourceLastModifiedDate = connect(
60     (state: RootState, props: { uuid: string }) => {
61         const resource = getResource(props.uuid)(state.resources) as GroupContentsResource | undefined;
62         return { date: resource ? resource.modifiedAt : '' };
63     })((props: { date: string }) => renderDate(props.date));
64
65 export const renderFileSize = (fileSize?: number) =>
66     <Typography noWrap>
67         {formatFileSize(fileSize)}
68     </Typography>;
69
70 export const ResourceFileSize = connect(
71     (state: RootState, props: { uuid: string }) => {
72         const resource = getResource(props.uuid)(state.resources) as GroupContentsResource | undefined;
73         return {};
74     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
75
76 export const renderOwner = (owner: string) =>
77     <Typography noWrap color="primary" >
78         {owner}
79     </Typography>;
80
81 export const ResourceOwner = connect(
82     (state: RootState, props: { uuid: string }) => {
83         const resource = getResource(props.uuid)(state.resources) as GroupContentsResource | undefined;
84         return { owner: resource ? resource.ownerUuid : '' };
85     })((props: { owner: string }) => renderOwner(props.owner));
86
87 export const renderType = (type: string) =>
88     <Typography noWrap>
89         {resourceLabel(type)}
90     </Typography>;
91
92 export const ResourceType = connect(
93     (state: RootState, props: { uuid: string }) => {
94         const resource = getResource(props.uuid)(state.resources) as GroupContentsResource | undefined;
95         return { type: resource ? resource.kind : '' };
96     })((props: { type: string }) => renderType(props.type));
97
98 export const renderStatus = (item: { status?: string }) =>
99     <Typography noWrap align="center" >
100         {item.status || "-"}
101     </Typography>;
102
103 export const ProcessStatus = connect(
104     (state: RootState, props: { uuid: string }) => {
105         const resource = getResource(props.uuid)(state.resources) as ProcessResource | undefined;
106         return { status: resource ? resource.state : '-' };
107     })((props: { status: string }) => renderType(props.status));