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 '@mui/material';
8 import { CustomStyleRulesCallback } from 'common/custom-theme';
9 import { WithStyles } from '@mui/styles';
10 import withStyles from '@mui/styles/withStyles';
11 import { ArvadosTheme } from 'common/custom-theme';
12 import { compose, Dispatch } from 'redux';
13 import { loadVirtualMachinesAdminData, openAddVirtualMachineLoginDialog, openRemoveVirtualMachineLoginDialog, openEditVirtualMachineLoginDialog } from 'store/virtual-machines/virtual-machines-actions';
14 import { RootState } from 'store/store';
15 import { ListResults } from 'services/common-service/common-service';
16 import { MoreVerticalIcon, AddUserIcon } from 'components/icon/icon';
17 import { VirtualMachineLogins, VirtualMachinesResource } from 'models/virtual-machines';
18 import { openVirtualMachinesContextMenu } from 'store/context-menu/context-menu-actions';
19 import { ResourceUuid, VirtualMachineHostname, VirtualMachineLogin } from 'views-components/data-explorer/renderers';
21 type CssRules = 'moreOptionsButton' | 'moreOptions' | 'chipsRoot' | 'vmTableWrapper';
23 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34 margin: `0px -${theme.spacing(0.5)}`,
41 const mapStateToProps = (state: RootState) => {
43 userUuid: state.auth.user!.uuid,
44 ...state.virtualMachines
48 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'onOptionsMenuOpen' | 'onAddLogin' | 'onDeleteLogin' | 'onLoginEdit'> => ({
49 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesAdminData()),
50 onOptionsMenuOpen: (event, virtualMachine) => {
51 dispatch<any>(openVirtualMachinesContextMenu(event, virtualMachine));
53 onAddLogin: (uuid: string) => {
54 dispatch<any>(openAddVirtualMachineLoginDialog(uuid));
56 onDeleteLogin: (uuid: string) => {
57 dispatch<any>(openRemoveVirtualMachineLoginDialog(uuid));
59 onLoginEdit: (uuid: string) => {
60 dispatch<any>(openEditVirtualMachineLoginDialog(uuid));
64 interface VirtualMachinesPanelDataProps {
65 virtualMachines: ListResults<any>;
66 logins: VirtualMachineLogins;
67 links: ListResults<any>;
71 interface VirtualMachinesPanelActionProps {
72 loadVirtualMachinesData: () => string;
73 onOptionsMenuOpen: (event: React.MouseEvent<HTMLElement>, virtualMachine: VirtualMachinesResource) => void;
74 onAddLogin: (uuid: string) => void;
75 onDeleteLogin: (uuid: string) => void;
76 onLoginEdit: (uuid: string) => void;
79 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
81 export const VirtualMachineAdminPanel = compose(
83 connect(mapStateToProps, mapDispatchToProps))(
84 class extends React.Component<VirtualMachineProps> {
86 this.props.loadVirtualMachinesData();
90 const { virtualMachines } = this.props;
92 <Grid container spacing={2}>
93 {virtualMachines.items.length > 0 && <CardContentWithVirtualMachines {...this.props} />}
100 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
103 <CardContent className={props.classes.vmTableWrapper}>
104 {virtualMachinesTable(props)}
109 const virtualMachinesTable = (props: VirtualMachineProps) =>
110 <Table data-cy="vm-admin-table">
113 <TableCell>Uuid</TableCell>
114 <TableCell>Host name</TableCell>
115 <TableCell>Logins</TableCell>
121 {props.virtualMachines.items.map((machine, index) =>
122 <TableRow key={index}>
123 <TableCell><ResourceUuid uuid={machine.uuid} /></TableCell>
124 <TableCell><VirtualMachineHostname uuid={machine.uuid} /></TableCell>
126 <Grid container spacing={1} className={props.classes.chipsRoot}>
127 {props.links.items.filter((link) => (link.headUuid === machine.uuid)).map((permission, i) => (
129 <Chip label={<VirtualMachineLogin linkUuid={permission.uuid} />} onDelete={event => props.onDeleteLogin(permission.uuid)} onClick={event => props.onLoginEdit(permission.uuid)} />
135 <Tooltip title="Add Login Permission" disableFocusListener>
137 onClick={event => props.onAddLogin(machine.uuid)}
138 className={props.classes.moreOptionsButton}
144 <TableCell className={props.classes.moreOptions}>
145 <Tooltip title="More options" disableFocusListener>
147 onClick={event => props.onOptionsMenuOpen(event, machine)}
148 className={props.classes.moreOptionsButton}