Merge branch '16314-test-user-db'
[arvados.git] / src / views / collection-content-address-panel / collection-content-address-panel.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 { StyleRulesCallback, WithStyles, withStyles, Grid, Button } from '@material-ui/core';
7 import { CollectionIcon } from '~/components/icon/icon';
8 import { ArvadosTheme } from '~/common/custom-theme';
9 import { BackIcon } from '~/components/icon/icon';
10 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
11 import { COLLECTIONS_CONTENT_ADDRESS_PANEL_ID } from '~/store/collections-content-address-panel/collections-content-address-panel-actions';
12 import { DataExplorer } from "~/views-components/data-explorer/data-explorer";
13 import { Dispatch } from 'redux';
14 import { getIsAdmin } from '~/store/public-favorites/public-favorites-actions';
15 import { resourceKindToContextMenuKind, openContextMenu } from '~/store/context-menu/context-menu-actions';
16 import { ResourceKind } from '~/models/resource';
17 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
18 import { connect } from 'react-redux';
19 import { navigateTo } from '~/store/navigation/navigation-action';
20 import { DataColumns } from '~/components/data-table/data-table';
21 import { SortDirection } from '~/components/data-table/data-column';
22 import { createTree } from '~/models/tree';
23 import { ResourceName, ResourceOwnerName, ResourceLastModifiedDate } from '~/views-components/data-explorer/renderers';
24
25 type CssRules = 'backLink' | 'backIcon' | 'card' | 'title' | 'iconHeader' | 'link';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
28     backLink: {
29         fontSize: '14px',
30         fontWeight: 600,
31         display: 'flex',
32         alignItems: 'center',
33         padding: theme.spacing.unit,
34         marginBottom: theme.spacing.unit,
35         color: theme.palette.grey["700"],
36     },
37     backIcon: {
38         marginRight: theme.spacing.unit
39     },
40     card: {
41         width: '100%'
42     },
43     title: {
44         color: theme.palette.grey["700"]
45     },
46     iconHeader: {
47         fontSize: '1.875rem',
48         color: theme.customs.colors.green700
49     },
50     link: {
51         fontSize: '0.875rem',
52         color: theme.palette.primary.main,
53         textAlign: 'right',
54         '&:hover': {
55             cursor: 'pointer'
56         }
57     }
58 });
59
60 enum CollectionContentAddressPanelColumnNames {
61     COLLECTION_WITH_THIS_ADDRESS = "Collection with this address",
62     LOCATION = "Location",
63     LAST_MODIFIED = "Last modified"
64 }
65
66 export const collectionContentAddressPanelColumns: DataColumns<string> = [
67     {
68         name: CollectionContentAddressPanelColumnNames.COLLECTION_WITH_THIS_ADDRESS,
69         selected: true,
70         configurable: true,
71         sortDirection: SortDirection.NONE,
72         filters: createTree(),
73         render: uuid => <ResourceName uuid={uuid} />
74     },
75     {
76         name: CollectionContentAddressPanelColumnNames.LOCATION,
77         selected: true,
78         configurable: true,
79         filters: createTree(),
80         render: uuid => <ResourceOwnerName uuid={uuid} />
81     },
82     {
83         name: CollectionContentAddressPanelColumnNames.LAST_MODIFIED,
84         selected: true,
85         configurable: true,
86         sortDirection: SortDirection.DESC,
87         filters: createTree(),
88         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
89     }
90 ];
91
92 export interface CollectionContentAddressPanelActionProps {
93     onContextMenu: (event: React.MouseEvent<any>, uuid: string) => void;
94     onItemClick: (item: string) => void;
95     onItemDoubleClick: (item: string) => void;
96 }
97
98 const mapDispatchToProps = (dispatch: Dispatch): CollectionContentAddressPanelActionProps => ({
99     onContextMenu: (event, resourceUuid) => {
100         const isAdmin = dispatch<any>(getIsAdmin());
101         const kind = resourceKindToContextMenuKind(resourceUuid, isAdmin);
102         if (kind) {
103             dispatch<any>(openContextMenu(event, {
104                 name: '',
105                 uuid: resourceUuid,
106                 ownerUuid: '',
107                 kind: ResourceKind.NONE,
108                 menuKind: kind
109             }));
110         }
111         dispatch<any>(loadDetailsPanel(resourceUuid));
112     },
113     onItemClick: (uuid: string) => {
114         dispatch<any>(loadDetailsPanel(uuid));
115     },
116     onItemDoubleClick: uuid => {
117         dispatch<any>(navigateTo(uuid));
118     }
119 });
120
121 interface CollectionContentAddressDataProps {
122     match: {
123         params: { id: string }
124     };
125 }
126
127 export const CollectionsContentAddressPanel = withStyles(styles)(
128     connect(null, mapDispatchToProps)(
129         class extends React.Component<CollectionContentAddressPanelActionProps & CollectionContentAddressDataProps & WithStyles<CssRules>> {
130             render() {
131                 return <Grid item xs={12}>
132                     <Button
133                         onClick={() => history.back()}
134                         className={this.props.classes.backLink}>
135                         <BackIcon className={this.props.classes.backIcon} />
136                         Back
137                     </Button>
138                     <DataExplorer
139                         id={COLLECTIONS_CONTENT_ADDRESS_PANEL_ID}
140                         onRowClick={this.props.onItemClick}
141                         onRowDoubleClick={this.props.onItemDoubleClick}
142                         onContextMenu={this.props.onContextMenu}
143                         contextMenuColumn={true}
144                         title={`Content address: ${this.props.match.params.id}`}
145                         dataTableDefaultView={
146                             <DataTableDefaultView
147                                 icon={CollectionIcon}
148                                 messages={['Collections with this content address not found.']} />
149                         } />;
150                     </Grid >;
151             }
152         }
153     )
154 );