Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06594d9

Browse files
committed
feat: support agent metadata terminals
1 parent be40dc8 commit 06594d9

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

examples/templates/jfrog/docker/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ resource "coder_agent" "main" {
109109
JFROG_IDE_ACCESS_TOKEN : "${artifactory_scoped_token.me.access_token}"
110110
JFROG_IDE_STORE_CONNECTION : "true"
111111
}
112+
113+
metadata {
114+
key = "cpu"
115+
display_name = "CPU"
116+
script = "coder stat cpu"
117+
timeout = 1
118+
interval = 1
119+
}
112120
}
113121

114122
resource "coder_app" "code-server" {

site/src/components/Resources/AgentMetadata.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,16 @@ Example.args = {
103103
key: "big",
104104
},
105105
},
106+
{
107+
result: {
108+
...resultDefaults,
109+
value: "blah blah",
110+
},
111+
description: {
112+
...descriptionDefaults,
113+
display_name: "terminal:hello world",
114+
key: "term",
115+
},
116+
},
106117
],
107118
}

site/src/components/Resources/AgentMetadata.tsx

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import makeStyles from "@mui/styles/makeStyles"
22
import { watchAgentMetadata } from "api/api"
3+
import Popover from "@mui/material/Popover"
34
import { WorkspaceAgent, WorkspaceAgentMetadata } from "api/typesGenerated"
45
import { Stack } from "components/Stack/Stack"
56
import dayjs from "dayjs"
67
import {
78
createContext,
89
FC,
10+
Ref,
911
useContext,
1012
useEffect,
1113
useRef,
@@ -21,6 +23,42 @@ type ItemStatus = "stale" | "valid" | "loading"
2123

2224
export const WatchAgentMetadataContext = createContext(watchAgentMetadata)
2325

26+
const MetadataTerminalPopover: FC<{
27+
id: string
28+
body: string
29+
}> = ({ id, body }) => {
30+
const styles = useStyles()
31+
32+
const ref = useRef<HTMLDivElement>(null)
33+
const [open, setOpen] = useState(false)
34+
35+
return (
36+
<>
37+
<div
38+
className={styles.inlineCommand}
39+
ref={ref}
40+
onMouseEnter={() => setOpen(true)}
41+
onMouseLeave={() => setOpen(false)}
42+
>
43+
View Terminal
44+
</div>
45+
46+
<Popover
47+
id={id}
48+
open={open}
49+
anchorEl={ref.current}
50+
anchorOrigin={{
51+
vertical: "bottom",
52+
horizontal: "left",
53+
}}
54+
classes={{ paper: styles.popover }}
55+
>
56+
<Box p={1}>{body}</Box>
57+
</Popover>
58+
</>
59+
)
60+
}
61+
2462
const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
2563
const styles = useStyles()
2664

@@ -31,6 +69,13 @@ const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
3169
throw new Error("Metadata item description is undefined")
3270
}
3371

72+
const terminalPrefix = "terminal:"
73+
const isTerminal = item.description.display_name.startsWith(terminalPrefix)
74+
75+
const displayName = isTerminal
76+
? item.description.display_name.slice(terminalPrefix.length)
77+
: item.description.display_name
78+
3479
const staleThreshold = Math.max(
3580
item.description.interval + item.description.timeout * 2,
3681
5,
@@ -87,10 +132,15 @@ const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
87132

88133
return (
89134
<div className={styles.metadata}>
90-
<div className={styles.metadataLabel}>
91-
{item.description.display_name}
92-
</div>
93-
<Box>{value}</Box>
135+
<div className={styles.metadataLabel}>{displayName}</div>
136+
{isTerminal ? (
137+
<MetadataTerminalPopover
138+
id={`metadata-terminal-${item.description.key}`}
139+
body={item.description.script}
140+
/>
141+
) : (
142+
<Box>{value}</Box>
143+
)}
94144
</div>
95145
)
96146
}
@@ -104,6 +154,7 @@ export const AgentMetadataView: FC<AgentMetadataViewProps> = ({ metadata }) => {
104154
if (metadata.length === 0) {
105155
return <></>
106156
}
157+
107158
return (
108159
<div className={styles.root}>
109160
<Stack alignItems="baseline" direction="row" spacing={6}>
@@ -227,6 +278,13 @@ const useStyles = makeStyles((theme) => ({
227278
scrollPadding: theme.spacing(0, 4),
228279
},
229280

281+
popover: {
282+
padding: 0,
283+
width: theme.spacing(38),
284+
color: theme.palette.text.secondary,
285+
marginTop: theme.spacing(0.5),
286+
},
287+
230288
metadata: {
231289
fontSize: 12,
232290
lineHeight: "normal",

0 commit comments

Comments
 (0)