|
| 1 | +import CircularProgress from "@material-ui/core/CircularProgress" |
| 2 | +import { makeStyles, Theme, useTheme } from "@material-ui/core/styles" |
| 3 | +import ErrorIcon from "@material-ui/icons/ErrorOutline" |
| 4 | +import PlayIcon from "@material-ui/icons/PlayArrowOutlined" |
| 5 | +import StopIcon from "@material-ui/icons/StopOutlined" |
| 6 | +import { WorkspaceBuild } from "api/typesGenerated" |
| 7 | +import React from "react" |
| 8 | +import { MONOSPACE_FONT_FAMILY } from "theme/constants" |
| 9 | +import { combineClasses } from "util/combineClasses" |
| 10 | +import { getWorkspaceStatus } from "util/workspace" |
| 11 | + |
| 12 | +const StatusLanguage = { |
| 13 | + loading: "Loading", |
| 14 | + started: "Running", |
| 15 | + starting: "Starting", |
| 16 | + stopping: "Stopping", |
| 17 | + stopped: "Stopped", |
| 18 | + deleting: "Deleting", |
| 19 | + deleted: "Deleted", |
| 20 | + canceling: "Canceling action", |
| 21 | + canceled: "Canceled action", |
| 22 | + failed: "Failed", |
| 23 | + queued: "Queued", |
| 24 | +} |
| 25 | + |
| 26 | +const LoadingIcon: React.FC = () => { |
| 27 | + return <CircularProgress size={10} style={{ color: "#FFF" }} /> |
| 28 | +} |
| 29 | + |
| 30 | +export const getStatus = ( |
| 31 | + theme: Theme, |
| 32 | + build: WorkspaceBuild, |
| 33 | +): { |
| 34 | + borderColor: string |
| 35 | + backgroundColor: string |
| 36 | + text: string |
| 37 | + icon: React.ReactNode |
| 38 | +} => { |
| 39 | + const status = getWorkspaceStatus(build) |
| 40 | + switch (status) { |
| 41 | + case undefined: |
| 42 | + return { |
| 43 | + borderColor: theme.palette.text.secondary, |
| 44 | + backgroundColor: theme.palette.text.secondary, |
| 45 | + text: StatusLanguage.loading, |
| 46 | + icon: <LoadingIcon />, |
| 47 | + } |
| 48 | + case "started": |
| 49 | + return { |
| 50 | + borderColor: theme.palette.success.main, |
| 51 | + backgroundColor: theme.palette.success.dark, |
| 52 | + text: StatusLanguage.started, |
| 53 | + icon: <PlayIcon />, |
| 54 | + } |
| 55 | + case "starting": |
| 56 | + return { |
| 57 | + borderColor: theme.palette.success.main, |
| 58 | + backgroundColor: theme.palette.success.dark, |
| 59 | + text: StatusLanguage.starting, |
| 60 | + icon: <LoadingIcon />, |
| 61 | + } |
| 62 | + case "stopping": |
| 63 | + return { |
| 64 | + borderColor: theme.palette.warning.main, |
| 65 | + backgroundColor: theme.palette.warning.dark, |
| 66 | + text: StatusLanguage.stopping, |
| 67 | + icon: <LoadingIcon />, |
| 68 | + } |
| 69 | + case "stopped": |
| 70 | + return { |
| 71 | + borderColor: theme.palette.warning.main, |
| 72 | + backgroundColor: theme.palette.warning.dark, |
| 73 | + text: StatusLanguage.stopped, |
| 74 | + icon: <StopIcon />, |
| 75 | + } |
| 76 | + case "deleting": |
| 77 | + return { |
| 78 | + borderColor: theme.palette.warning.main, |
| 79 | + backgroundColor: theme.palette.warning.dark, |
| 80 | + text: StatusLanguage.deleting, |
| 81 | + icon: <LoadingIcon />, |
| 82 | + } |
| 83 | + case "deleted": |
| 84 | + return { |
| 85 | + borderColor: theme.palette.error.main, |
| 86 | + backgroundColor: theme.palette.error.dark, |
| 87 | + text: StatusLanguage.deleted, |
| 88 | + icon: <ErrorIcon />, |
| 89 | + } |
| 90 | + case "canceling": |
| 91 | + return { |
| 92 | + borderColor: theme.palette.warning.main, |
| 93 | + backgroundColor: theme.palette.warning.dark, |
| 94 | + text: StatusLanguage.canceling, |
| 95 | + icon: <LoadingIcon />, |
| 96 | + } |
| 97 | + case "canceled": |
| 98 | + return { |
| 99 | + borderColor: theme.palette.warning.main, |
| 100 | + backgroundColor: theme.palette.warning.dark, |
| 101 | + text: StatusLanguage.canceled, |
| 102 | + icon: <ErrorIcon />, |
| 103 | + } |
| 104 | + case "error": |
| 105 | + return { |
| 106 | + borderColor: theme.palette.error.main, |
| 107 | + backgroundColor: theme.palette.error.dark, |
| 108 | + text: StatusLanguage.failed, |
| 109 | + icon: <ErrorIcon />, |
| 110 | + } |
| 111 | + case "queued": |
| 112 | + return { |
| 113 | + borderColor: theme.palette.info.main, |
| 114 | + backgroundColor: theme.palette.info.dark, |
| 115 | + text: StatusLanguage.queued, |
| 116 | + icon: <LoadingIcon />, |
| 117 | + } |
| 118 | + } |
| 119 | + throw new Error("unknown text " + status) |
| 120 | +} |
| 121 | + |
| 122 | +export type WorkspaceStatusBadgeProps = { |
| 123 | + build: WorkspaceBuild |
| 124 | + className?: string |
| 125 | +} |
| 126 | + |
| 127 | +export const WorkspaceStatusBadge: React.FC<WorkspaceStatusBadgeProps> = ({ build, className }) => { |
| 128 | + const styles = useStyles() |
| 129 | + const theme = useTheme() |
| 130 | + const { text, icon, ...colorStyles } = getStatus(theme, build) |
| 131 | + return ( |
| 132 | + <div |
| 133 | + className={combineClasses([styles.wrapper, className])} |
| 134 | + style={{ ...colorStyles }} |
| 135 | + role="status" |
| 136 | + > |
| 137 | + <div className={styles.iconWrapper}>{icon}</div> |
| 138 | + {text} |
| 139 | + </div> |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +const useStyles = makeStyles((theme) => ({ |
| 144 | + wrapper: { |
| 145 | + fontFamily: MONOSPACE_FONT_FAMILY, |
| 146 | + display: "inline-flex", |
| 147 | + alignItems: "center", |
| 148 | + borderWidth: 1, |
| 149 | + borderStyle: "solid", |
| 150 | + borderRadius: 99999, |
| 151 | + fontSize: 14, |
| 152 | + fontWeight: 500, |
| 153 | + color: "#FFF", |
| 154 | + height: theme.spacing(3), |
| 155 | + paddingLeft: theme.spacing(0.75), |
| 156 | + paddingRight: theme.spacing(1.5), |
| 157 | + whiteSpace: "nowrap", |
| 158 | + }, |
| 159 | + |
| 160 | + iconWrapper: { |
| 161 | + marginRight: theme.spacing(0.5), |
| 162 | + width: theme.spacing(2), |
| 163 | + height: theme.spacing(2), |
| 164 | + lineHeight: 0, |
| 165 | + display: "flex", |
| 166 | + alignItems: "center", |
| 167 | + justifyContent: "center", |
| 168 | + |
| 169 | + "& > svg": { |
| 170 | + width: theme.spacing(2), |
| 171 | + height: theme.spacing(2), |
| 172 | + }, |
| 173 | + }, |
| 174 | +})) |
0 commit comments