c96813df95bb25f8b4f773804ed6eda5df7255b2
[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, 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 { 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<GroupContentsResource>(props.uuid)(state.resources);
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<GroupContentsResource>(props.uuid)(state.resources);
62         return { date: resource ? resource.modifiedAt : '' };
63     })((props: { date: string }) => renderDate(props.date));
64
65 export const ResourceTrashDate = connect(
66     (state: RootState, props: { uuid: string }) => {
67         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
68         return { date: resource ? resource.trashAt : '' };
69     })((props: { date: string }) => renderDate(props.date));
70
71 export const ResourceDeleteDate = connect(
72     (state: RootState, props: { uuid: string }) => {
73         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
74         return { date: resource ? resource.deleteAt : '' };
75     })((props: { date: string }) => renderDate(props.date));
76
77 export const renderFileSize = (fileSize?: number) =>
78     <Typography noWrap>
79         {formatFileSize(fileSize)}
80     </Typography>;
81
82 export const ResourceFileSize = connect(
83     (state: RootState, props: { uuid: string }) => {
84         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
85         return {};
86     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
87
88 export const renderOwner = (owner: string) =>
89     <Typography noWrap color="primary" >
90         {owner}
91     </Typography>;
92
93 export const ResourceOwner = connect(
94     (state: RootState, props: { uuid: string }) => {
95         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
96         return { owner: resource ? resource.ownerUuid : '' };
97     })((props: { owner: string }) => renderOwner(props.owner));
98
99 export const renderType = (type: string) =>
100     <Typography noWrap>
101         {resourceLabel(type)}
102     </Typography>;
103
104 export const ResourceType = connect(
105     (state: RootState, props: { uuid: string }) => {
106         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
107         return { type: resource ? resource.kind : '' };
108     })((props: { type: string }) => renderType(props.type));
109
110 export const renderStatus = (item: { status?: string }) =>
111     <Typography noWrap align="center" >
112         {item.status || "-"}
113     </Typography>;
114
115 export const ProcessStatus = connect(
116     (state: RootState, props: { uuid: string }) => {
117         const resource = getResource<ProcessResource>(props.uuid)(state.resources);
118         return { status: resource ? resource.state : '-' };
119     })((props: { status: string }) => renderType(props.status));