1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { connect } from 'react-redux';
7 import { Grid, Button, Typography, StyleRulesCallback, WithStyles, withStyles } from "@material-ui/core";
8 import { DataExplorer } from "views-components/data-explorer/data-explorer";
9 import { DataColumns } from 'components/data-table/data-table';
10 import { SortDirection } from 'components/data-table/data-column';
11 import { ResourceUuid } from 'views-components/data-explorer/renderers';
12 import { AddIcon } from 'components/icon/icon';
13 import { ResourceName } from 'views-components/data-explorer/renderers';
14 import { createTree } from 'models/tree';
15 import { GROUPS_PANEL_ID, openCreateGroupDialog } from 'store/groups-panel/groups-panel-actions';
16 import { noop } from 'lodash/fp';
17 import { ContextMenuKind } from 'views-components/context-menu/context-menu';
18 import { getResource, ResourcesState, filterResources } from 'store/resources/resources';
19 import { GroupResource } from 'models/group';
20 import { RootState } from 'store/store';
21 import { openContextMenu } from 'store/context-menu/context-menu-actions';
22 import { ResourceKind } from 'models/resource';
23 import { LinkClass, LinkResource } from 'models/link';
24 import { ArvadosTheme } from 'common/custom-theme';
26 type CssRules = "root";
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34 export enum GroupsPanelColumnNames {
40 export const groupsPanelColumns: DataColumns<string> = [
42 name: GroupsPanelColumnNames.GROUP,
45 sortDirection: SortDirection.ASC,
46 filters: createTree(),
47 render: uuid => <ResourceName uuid={uuid} />
50 name: GroupsPanelColumnNames.UUID,
53 filters: createTree(),
54 render: uuid => <ResourceUuid uuid={uuid} />,
57 name: GroupsPanelColumnNames.MEMBERS,
60 filters: createTree(),
61 render: uuid => <GroupMembersCount uuid={uuid} />,
65 const mapStateToProps = (state: RootState) => {
67 resources: state.resources
71 const mapDispatchToProps = {
72 onContextMenu: openContextMenu,
73 onNewGroup: openCreateGroupDialog,
76 export interface GroupsPanelProps {
77 onNewGroup: () => void;
78 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
79 resources: ResourcesState;
82 export const GroupsPanel = withStyles(styles)(connect(
83 mapStateToProps, mapDispatchToProps
85 class GroupsPanel extends React.Component<GroupsPanelProps & WithStyles<CssRules>> {
89 <div className={this.props.classes.root}><DataExplorer
91 data-cy="groups-panel-data-explorer"
93 onRowDoubleClick={noop}
94 onContextMenu={this.handleContextMenu}
95 contextMenuColumn={true}
98 <Grid container justify='flex-end'>
100 data-cy="groups-panel-new-group"
103 onClick={this.props.onNewGroup}>
104 <AddIcon /> New group
111 handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
112 const resource = getResource<GroupResource>(resourceUuid)(this.props.resources);
114 this.props.onContextMenu(event, {
117 description: resource.description,
118 ownerUuid: resource.ownerUuid,
120 menuKind: ContextMenuKind.GROUPS
127 const GroupMembersCount = connect(
128 (state: RootState, props: { uuid: string }) => {
130 const permissions = filterResources((resource: LinkResource) =>
131 resource.kind === ResourceKind.LINK &&
132 resource.linkClass === LinkClass.PERMISSION &&
133 resource.headUuid === props.uuid
137 children: permissions.length,
141 )((props: {children: number}) => (<Typography children={props.children} />));