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, Card, Chip, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip, IconButton } from '@material-ui/core';
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { ArvadosTheme } from 'common/custom-theme';
10 import { compose, Dispatch } from 'redux';
11 import { loadVirtualMachinesAdminData, openAddVirtualMachineLoginDialog, openRemoveVirtualMachineLoginDialog, openEditVirtualMachineLoginDialog } from 'store/virtual-machines/virtual-machines-actions';
12 import { RootState } from 'store/store';
13 import { ListResults } from 'services/common-service/common-service';
14 import { MoreVerticalIcon, AddUserIcon } from 'components/icon/icon';
15 import { VirtualMachineLogins, VirtualMachinesResource } from 'models/virtual-machines';
16 import { openVirtualMachinesContextMenu } from 'store/context-menu/context-menu-actions';
17 import { ResourceUuid, VirtualMachineHostname, VirtualMachineLogin } from 'views-components/data-explorer/renderers';
19 type CssRules = 'moreOptionsButton' | 'moreOptions' | 'chipsRoot' | 'vmTableWrapper';
21 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
32 margin: `0px -${theme.spacing.unit / 2}px`,
39 const mapStateToProps = (state: RootState) => {
41 userUuid: state.auth.user!.uuid,
42 ...state.virtualMachines
46 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'onOptionsMenuOpen' | 'onAddLogin' | 'onDeleteLogin' | 'onLoginEdit'> => ({
47 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesAdminData()),
48 onOptionsMenuOpen: (event, virtualMachine) => {
49 dispatch<any>(openVirtualMachinesContextMenu(event, virtualMachine));
51 onAddLogin: (uuid: string) => {
52 dispatch<any>(openAddVirtualMachineLoginDialog(uuid));
54 onDeleteLogin: (uuid: string) => {
55 dispatch<any>(openRemoveVirtualMachineLoginDialog(uuid));
57 onLoginEdit: (uuid: string) => {
58 dispatch<any>(openEditVirtualMachineLoginDialog(uuid));
62 interface VirtualMachinesPanelDataProps {
63 virtualMachines: ListResults<any>;
64 logins: VirtualMachineLogins;
65 links: ListResults<any>;
69 interface VirtualMachinesPanelActionProps {
70 loadVirtualMachinesData: () => string;
71 onOptionsMenuOpen: (event: React.MouseEvent<HTMLElement>, virtualMachine: VirtualMachinesResource) => void;
72 onAddLogin: (uuid: string) => void;
73 onDeleteLogin: (uuid: string) => void;
74 onLoginEdit: (uuid: string) => void;
77 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
79 export const VirtualMachineAdminPanel = compose(
81 connect(mapStateToProps, mapDispatchToProps))(
82 class extends React.Component<VirtualMachineProps> {
84 this.props.loadVirtualMachinesData();
88 const { virtualMachines } = this.props;
90 <Grid container spacing={16}>
91 {virtualMachines.items.length > 0 && <CardContentWithVirtualMachines {...this.props} />}
98 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
101 <CardContent className={props.classes.vmTableWrapper}>
102 {virtualMachinesTable(props)}
107 const virtualMachinesTable = (props: VirtualMachineProps) =>
108 <Table data-cy="vm-admin-table">
111 <TableCell>Uuid</TableCell>
112 <TableCell>Host name</TableCell>
113 <TableCell>Logins</TableCell>
119 {props.virtualMachines.items.map((machine, index) =>
120 <TableRow key={index}>
121 <TableCell><ResourceUuid uuid={machine.uuid} /></TableCell>
122 <TableCell><VirtualMachineHostname uuid={machine.uuid} /></TableCell>
124 <Grid container spacing={8} className={props.classes.chipsRoot}>
125 {props.links.items.filter((link) => (link.headUuid === machine.uuid)).map((permission, i) => (
127 <Chip label={<VirtualMachineLogin linkUuid={permission.uuid} />} onDelete={event => props.onDeleteLogin(permission.uuid)} onClick={event => props.onLoginEdit(permission.uuid)} />
133 <Tooltip title="Add Login Permission" disableFocusListener>
134 <IconButton onClick={event => props.onAddLogin(machine.uuid)} className={props.classes.moreOptionsButton}>
139 <TableCell className={props.classes.moreOptions}>
140 <Tooltip title="More options" disableFocusListener>
141 <IconButton onClick={event => props.onOptionsMenuOpen(event, machine)} className={props.classes.moreOptionsButton}>