|
| 1 | +import { fade, makeStyles, Theme } from "@material-ui/core/styles" |
| 2 | +import TableRow from "@material-ui/core/TableRow" |
| 3 | +import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight" |
| 4 | +import useTheme from "@material-ui/styles/useTheme" |
| 5 | +import { useActor } from "@xstate/react" |
| 6 | +import dayjs from "dayjs" |
| 7 | +import relativeTime from "dayjs/plugin/relativeTime" |
| 8 | +import { FC } from "react" |
| 9 | +import { useNavigate } from "react-router-dom" |
| 10 | +import { getDisplayStatus } from "../../util/workspace" |
| 11 | +import { WorkspaceItemMachineRef } from "../../xServices/workspaces/workspacesXService" |
| 12 | +import { AvatarData } from "../AvatarData/AvatarData" |
| 13 | +import { TableCellLink } from "../TableCellLink/TableCellLink" |
| 14 | +import { OutdatedHelpTooltip } from "../Tooltips" |
| 15 | + |
| 16 | +dayjs.extend(relativeTime) |
| 17 | + |
| 18 | +const Language = { |
| 19 | + upToDateLabel: "Up to date", |
| 20 | + outdatedLabel: "Outdated", |
| 21 | +} |
| 22 | + |
| 23 | +export const WorkspacesRow: FC<{ workspaceRef: WorkspaceItemMachineRef }> = ({ workspaceRef }) => { |
| 24 | + const styles = useStyles() |
| 25 | + const navigate = useNavigate() |
| 26 | + const theme: Theme = useTheme() |
| 27 | + const [workspaceState, send] = useActor(workspaceRef) |
| 28 | + const { data: workspace } = workspaceState.context |
| 29 | + const status = getDisplayStatus(theme, workspace.latest_build) |
| 30 | + const workspacePageLink = `/@${workspace.owner_name}/${workspace.name}` |
| 31 | + |
| 32 | + return ( |
| 33 | + <TableRow |
| 34 | + hover |
| 35 | + data-testid={`workspace-${workspace.id}`} |
| 36 | + tabIndex={0} |
| 37 | + onKeyDown={(event) => { |
| 38 | + if (event.key === "Enter") { |
| 39 | + navigate(workspacePageLink) |
| 40 | + } |
| 41 | + }} |
| 42 | + className={styles.clickableTableRow} |
| 43 | + > |
| 44 | + <TableCellLink to={workspacePageLink}> |
| 45 | + <AvatarData title={workspace.name} subtitle={workspace.owner_name} /> |
| 46 | + </TableCellLink> |
| 47 | + <TableCellLink to={workspacePageLink}>{workspace.template_name}</TableCellLink> |
| 48 | + <TableCellLink to={workspacePageLink}> |
| 49 | + {workspace.outdated ? ( |
| 50 | + <span className={styles.outdatedLabel}> |
| 51 | + {Language.outdatedLabel} |
| 52 | + <OutdatedHelpTooltip |
| 53 | + onUpdateVersion={() => { |
| 54 | + send("UPDATE_VERSION") |
| 55 | + }} |
| 56 | + /> |
| 57 | + </span> |
| 58 | + ) : ( |
| 59 | + <span style={{ color: theme.palette.text.secondary }}>{Language.upToDateLabel}</span> |
| 60 | + )} |
| 61 | + </TableCellLink> |
| 62 | + <TableCellLink to={workspacePageLink}> |
| 63 | + <span data-chromatic="ignore" style={{ color: theme.palette.text.secondary }}> |
| 64 | + {dayjs().to(dayjs(workspace.latest_build.created_at))} |
| 65 | + </span> |
| 66 | + </TableCellLink> |
| 67 | + <TableCellLink to={workspacePageLink}> |
| 68 | + <span style={{ color: status.color }}>{status.status}</span> |
| 69 | + </TableCellLink> |
| 70 | + <TableCellLink to={workspacePageLink}> |
| 71 | + <div className={styles.arrowCell}> |
| 72 | + <KeyboardArrowRight className={styles.arrowRight} /> |
| 73 | + </div> |
| 74 | + </TableCellLink> |
| 75 | + </TableRow> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +const useStyles = makeStyles((theme) => ({ |
| 80 | + clickableTableRow: { |
| 81 | + "&:hover td": { |
| 82 | + backgroundColor: fade(theme.palette.primary.light, 0.1), |
| 83 | + }, |
| 84 | + |
| 85 | + "&:focus": { |
| 86 | + outline: `1px solid ${theme.palette.secondary.dark}`, |
| 87 | + }, |
| 88 | + |
| 89 | + "& .MuiTableCell-root:last-child": { |
| 90 | + paddingRight: theme.spacing(2), |
| 91 | + }, |
| 92 | + }, |
| 93 | + arrowRight: { |
| 94 | + color: fade(theme.palette.primary.contrastText, 0.7), |
| 95 | + width: 20, |
| 96 | + height: 20, |
| 97 | + }, |
| 98 | + arrowCell: { |
| 99 | + display: "flex", |
| 100 | + }, |
| 101 | + outdatedLabel: { |
| 102 | + color: theme.palette.error.main, |
| 103 | + display: "flex", |
| 104 | + alignItems: "center", |
| 105 | + gap: theme.spacing(0.5), |
| 106 | + }, |
| 107 | +})) |
0 commit comments