|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import Skeleton from "@mui/material/Skeleton"; |
| 3 | +import Tooltip from "@mui/material/Tooltip"; |
| 4 | +import { type Interpolation, type Theme } from "@emotion/react"; |
| 5 | +import { |
| 6 | + createContext, |
| 7 | + type FC, |
| 8 | + type HTMLAttributes, |
| 9 | + useContext, |
| 10 | + useEffect, |
| 11 | + useLayoutEffect, |
| 12 | + useRef, |
| 13 | + useState, |
| 14 | +} from "react"; |
| 15 | +import { watchAgentMetadata } from "api/api"; |
| 16 | +import type { |
| 17 | + WorkspaceAgent, |
| 18 | + WorkspaceAgentMetadata, |
| 19 | +} from "api/typesGenerated"; |
| 20 | +import { Stack } from "components/Stack/Stack"; |
| 21 | +import { MONOSPACE_FONT_FAMILY } from "theme/constants"; |
| 22 | + |
| 23 | +type ItemStatus = "stale" | "valid" | "loading"; |
| 24 | + |
| 25 | +export const WatchAgentMetadataContext = createContext(watchAgentMetadata); |
| 26 | + |
| 27 | +export interface AgentMetadataViewProps { |
| 28 | + metadata: WorkspaceAgentMetadata[]; |
| 29 | +} |
| 30 | + |
| 31 | +export const AgentMetadataView: FC<AgentMetadataViewProps> = ({ metadata }) => { |
| 32 | + if (metadata.length === 0) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + return ( |
| 36 | + <section css={styles.root}> |
| 37 | + {metadata.map((m) => ( |
| 38 | + <MetadataItem key={m.description.key} item={m} /> |
| 39 | + ))} |
| 40 | + </section> |
| 41 | + ); |
| 42 | +}; |
| 43 | + |
| 44 | +interface AgentMetadataProps { |
| 45 | + agent: WorkspaceAgent; |
| 46 | + storybookMetadata?: WorkspaceAgentMetadata[]; |
| 47 | +} |
| 48 | + |
| 49 | +export const AgentMetadata: FC<AgentMetadataProps> = ({ |
| 50 | + agent, |
| 51 | + storybookMetadata, |
| 52 | +}) => { |
| 53 | + const [metadata, setMetadata] = useState< |
| 54 | + WorkspaceAgentMetadata[] | undefined |
| 55 | + >(undefined); |
| 56 | + const watchAgentMetadata = useContext(WatchAgentMetadataContext); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (storybookMetadata !== undefined) { |
| 60 | + setMetadata(storybookMetadata); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let timeout: ReturnType<typeof setTimeout> | undefined = undefined; |
| 65 | + |
| 66 | + const connect = (): (() => void) => { |
| 67 | + const source = watchAgentMetadata(agent.id); |
| 68 | + |
| 69 | + source.onerror = (e) => { |
| 70 | + console.error("received error in watch stream", e); |
| 71 | + setMetadata(undefined); |
| 72 | + source.close(); |
| 73 | + |
| 74 | + timeout = setTimeout(() => { |
| 75 | + connect(); |
| 76 | + }, 3000); |
| 77 | + }; |
| 78 | + |
| 79 | + source.addEventListener("data", (e) => { |
| 80 | + const data = JSON.parse(e.data); |
| 81 | + setMetadata(data); |
| 82 | + }); |
| 83 | + return () => { |
| 84 | + if (timeout !== undefined) { |
| 85 | + clearTimeout(timeout); |
| 86 | + } |
| 87 | + source.close(); |
| 88 | + }; |
| 89 | + }; |
| 90 | + return connect(); |
| 91 | + }, [agent.id, watchAgentMetadata, storybookMetadata]); |
| 92 | + |
| 93 | + if (metadata === undefined) { |
| 94 | + return ( |
| 95 | + <section css={styles.root}> |
| 96 | + <AgentMetadataSkeleton /> |
| 97 | + </section> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <AgentMetadataView |
| 103 | + metadata={[...metadata].sort((a, b) => |
| 104 | + a.description.display_name.localeCompare(b.description.display_name), |
| 105 | + )} |
| 106 | + /> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +export const AgentMetadataSkeleton: FC = () => { |
| 111 | + return ( |
| 112 | + <Stack alignItems="baseline" direction="row" spacing={6}> |
| 113 | + <div css={styles.metadata}> |
| 114 | + <Skeleton width={40} height={12} variant="text" /> |
| 115 | + <Skeleton width={65} height={14} variant="text" /> |
| 116 | + </div> |
| 117 | + |
| 118 | + <div css={styles.metadata}> |
| 119 | + <Skeleton width={40} height={12} variant="text" /> |
| 120 | + <Skeleton width={65} height={14} variant="text" /> |
| 121 | + </div> |
| 122 | + |
| 123 | + <div css={styles.metadata}> |
| 124 | + <Skeleton width={40} height={12} variant="text" /> |
| 125 | + <Skeleton width={65} height={14} variant="text" /> |
| 126 | + </div> |
| 127 | + </Stack> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +interface MetadataItemProps { |
| 132 | + item: WorkspaceAgentMetadata; |
| 133 | +} |
| 134 | + |
| 135 | +const MetadataItem: FC<MetadataItemProps> = ({ item }) => { |
| 136 | + const staleThreshold = Math.max( |
| 137 | + item.description.interval + item.description.timeout * 2, |
| 138 | + // In case there is intense backpressure, we give a little bit of slack. |
| 139 | + 5, |
| 140 | + ); |
| 141 | + |
| 142 | + const status: ItemStatus = (() => { |
| 143 | + const year = dayjs(item.result.collected_at).year(); |
| 144 | + if (year <= 1970 || isNaN(year)) { |
| 145 | + return "loading"; |
| 146 | + } |
| 147 | + // There is a special circumstance for metadata with `interval: 0`. It is |
| 148 | + // expected that they run once and never again, so never display them as |
| 149 | + // stale. |
| 150 | + if (item.result.age > staleThreshold && item.description.interval > 0) { |
| 151 | + return "stale"; |
| 152 | + } |
| 153 | + return "valid"; |
| 154 | + })(); |
| 155 | + |
| 156 | + // Stale data is as good as no data. Plus, we want to build confidence in our |
| 157 | + // users that what's shown is real. If times aren't correctly synced this |
| 158 | + // could be buggy. But, how common is that anyways? |
| 159 | + const value = |
| 160 | + status === "loading" ? ( |
| 161 | + <Skeleton width={65} height={12} variant="text" css={styles.skeleton} /> |
| 162 | + ) : status === "stale" ? ( |
| 163 | + <Tooltip title="This data is stale and no longer up to date"> |
| 164 | + <StaticWidth css={[styles.metadataValue, styles.metadataStale]}> |
| 165 | + {item.result.value} |
| 166 | + </StaticWidth> |
| 167 | + </Tooltip> |
| 168 | + ) : ( |
| 169 | + <StaticWidth |
| 170 | + css={[ |
| 171 | + styles.metadataValue, |
| 172 | + item.result.error.length === 0 |
| 173 | + ? styles.metadataValueSuccess |
| 174 | + : styles.metadataValueError, |
| 175 | + ]} |
| 176 | + > |
| 177 | + {item.result.value} |
| 178 | + </StaticWidth> |
| 179 | + ); |
| 180 | + |
| 181 | + return ( |
| 182 | + <div css={styles.metadata}> |
| 183 | + <div css={styles.metadataLabel}>{item.description.display_name}</div> |
| 184 | + <div>{value}</div> |
| 185 | + </div> |
| 186 | + ); |
| 187 | +}; |
| 188 | + |
| 189 | +const StaticWidth: FC<HTMLAttributes<HTMLDivElement>> = ({ |
| 190 | + children, |
| 191 | + ...attrs |
| 192 | +}) => { |
| 193 | + const ref = useRef<HTMLDivElement>(null); |
| 194 | + |
| 195 | + useLayoutEffect(() => { |
| 196 | + // Ignore this in storybook |
| 197 | + if (!ref.current || process.env.STORYBOOK === "true") { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + const currentWidth = ref.current.getBoundingClientRect().width; |
| 202 | + ref.current.style.width = "auto"; |
| 203 | + const autoWidth = ref.current.getBoundingClientRect().width; |
| 204 | + ref.current.style.width = |
| 205 | + autoWidth > currentWidth ? `${autoWidth}px` : `${currentWidth}px`; |
| 206 | + }, [children]); |
| 207 | + |
| 208 | + return ( |
| 209 | + <div ref={ref} {...attrs}> |
| 210 | + {children} |
| 211 | + </div> |
| 212 | + ); |
| 213 | +}; |
| 214 | + |
| 215 | +// These are more or less copied from |
| 216 | +// site/src/components/Resources/ResourceCard.tsx |
| 217 | +const styles = { |
| 218 | + root: { |
| 219 | + display: "flex", |
| 220 | + alignItems: "baseline", |
| 221 | + flexWrap: "wrap", |
| 222 | + gap: 32, |
| 223 | + rowGap: 16, |
| 224 | + }, |
| 225 | + |
| 226 | + metadata: { |
| 227 | + lineHeight: "1.6", |
| 228 | + display: "flex", |
| 229 | + flexDirection: "column", |
| 230 | + overflow: "visible", |
| 231 | + flexShrink: 0, |
| 232 | + }, |
| 233 | + |
| 234 | + metadataLabel: (theme) => ({ |
| 235 | + color: theme.palette.text.secondary, |
| 236 | + textOverflow: "ellipsis", |
| 237 | + overflow: "hidden", |
| 238 | + whiteSpace: "nowrap", |
| 239 | + fontSize: 13, |
| 240 | + }), |
| 241 | + |
| 242 | + metadataValue: { |
| 243 | + textOverflow: "ellipsis", |
| 244 | + overflow: "hidden", |
| 245 | + whiteSpace: "nowrap", |
| 246 | + maxWidth: "16em", |
| 247 | + fontSize: 14, |
| 248 | + }, |
| 249 | + |
| 250 | + metadataValueSuccess: (theme) => ({ |
| 251 | + color: theme.experimental.roles.success.fill.outline, |
| 252 | + }), |
| 253 | + |
| 254 | + metadataValueError: (theme) => ({ |
| 255 | + color: theme.palette.error.main, |
| 256 | + }), |
| 257 | + |
| 258 | + metadataStale: (theme) => ({ |
| 259 | + color: theme.palette.text.disabled, |
| 260 | + cursor: "pointer", |
| 261 | + }), |
| 262 | + |
| 263 | + skeleton: { |
| 264 | + marginTop: 4, |
| 265 | + }, |
| 266 | + |
| 267 | + inlineCommand: (theme) => ({ |
| 268 | + fontFamily: MONOSPACE_FONT_FAMILY, |
| 269 | + display: "inline-block", |
| 270 | + fontWeight: 600, |
| 271 | + margin: 0, |
| 272 | + borderRadius: 4, |
| 273 | + color: theme.palette.text.primary, |
| 274 | + }), |
| 275 | +} satisfies Record<string, Interpolation<Theme>>; |
0 commit comments