|
| 1 | +import { makeStyles, Theme } from "@material-ui/core/styles" |
| 2 | +import Table from "@material-ui/core/Table" |
| 3 | +import TableBody from "@material-ui/core/TableBody" |
| 4 | +import TableCell from "@material-ui/core/TableCell" |
| 5 | +import TableHead from "@material-ui/core/TableHead" |
| 6 | +import TableRow from "@material-ui/core/TableRow" |
| 7 | +import useTheme from "@material-ui/styles/useTheme" |
| 8 | +import React from "react" |
| 9 | +import { WorkspaceResource } from "../../api/typesGenerated" |
| 10 | +import { getDisplayAgentStatus } from "../../util/workspace" |
| 11 | +import { TableHeaderRow } from "../TableHeaders/TableHeaders" |
| 12 | +import { WorkspaceSection } from "../WorkspaceSection/WorkspaceSection" |
| 13 | + |
| 14 | +const Language = { |
| 15 | + resources: "Resources", |
| 16 | + resourceLabel: "Resource", |
| 17 | + agentsLabel: "Agents", |
| 18 | + agentLabel: "Agent", |
| 19 | + statusLabel: "Status", |
| 20 | +} |
| 21 | + |
| 22 | +interface ResourcesProps { |
| 23 | + resources?: WorkspaceResource[] |
| 24 | + getResourcesError?: Error |
| 25 | +} |
| 26 | + |
| 27 | +export const Resources: React.FC<ResourcesProps> = ({ resources, getResourcesError }) => { |
| 28 | + const styles = useStyles() |
| 29 | + const theme: Theme = useTheme() |
| 30 | + |
| 31 | + return ( |
| 32 | + <WorkspaceSection title={Language.resources} contentsProps={{ className: styles.sectionContents }}> |
| 33 | + {getResourcesError ? ( |
| 34 | + { getResourcesError } |
| 35 | + ) : ( |
| 36 | + <Table className={styles.table}> |
| 37 | + <TableHead> |
| 38 | + <TableHeaderRow> |
| 39 | + <TableCell>{Language.resourceLabel}</TableCell> |
| 40 | + <TableCell className={styles.agentColumn}>{Language.agentLabel}</TableCell> |
| 41 | + <TableCell>{Language.statusLabel}</TableCell> |
| 42 | + </TableHeaderRow> |
| 43 | + </TableHead> |
| 44 | + <TableBody> |
| 45 | + {resources?.map((resource) => { |
| 46 | + { |
| 47 | + /* We need to initialize the agents to display the resource */ |
| 48 | + } |
| 49 | + const agents = resource.agents ?? [null] |
| 50 | + return agents.map((agent, agentIndex) => { |
| 51 | + { |
| 52 | + /* If there is no agent, just display the resource name */ |
| 53 | + } |
| 54 | + if (!agent) { |
| 55 | + return ( |
| 56 | + <TableRow> |
| 57 | + <TableCell className={styles.resourceNameCell}>{resource.name}</TableCell> |
| 58 | + <TableCell colSpan={2}></TableCell> |
| 59 | + </TableRow> |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <TableRow key={`${resource.id}-${agent.id}`}> |
| 65 | + {/* We only want to display the name in the first row because we are using rowSpan */} |
| 66 | + {/* The rowspan should be the same than the number of agents */} |
| 67 | + {agentIndex === 0 && ( |
| 68 | + <TableCell className={styles.resourceNameCell} rowSpan={agents.length}> |
| 69 | + {resource.name} |
| 70 | + </TableCell> |
| 71 | + )} |
| 72 | + |
| 73 | + <TableCell className={styles.agentColumn}> |
| 74 | + <span style={{ color: theme.palette.text.secondary }}>{agent.name}</span> |
| 75 | + </TableCell> |
| 76 | + <TableCell> |
| 77 | + <span style={{ color: getDisplayAgentStatus(theme, agent).color }}> |
| 78 | + {getDisplayAgentStatus(theme, agent).status} |
| 79 | + </span> |
| 80 | + </TableCell> |
| 81 | + </TableRow> |
| 82 | + ) |
| 83 | + }) |
| 84 | + })} |
| 85 | + </TableBody> |
| 86 | + </Table> |
| 87 | + )} |
| 88 | + </WorkspaceSection> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +const useStyles = makeStyles((theme) => ({ |
| 93 | + sectionContents: { |
| 94 | + margin: 0, |
| 95 | + }, |
| 96 | + |
| 97 | + table: { |
| 98 | + border: 0, |
| 99 | + }, |
| 100 | + |
| 101 | + resourceNameCell: { |
| 102 | + borderRight: `1px solid ${theme.palette.divider}`, |
| 103 | + }, |
| 104 | + |
| 105 | + // Adds some left spacing |
| 106 | + agentColumn: { |
| 107 | + paddingLeft: `${theme.spacing(2)}px !important`, |
| 108 | + }, |
| 109 | +})) |
0 commit comments