Skip to content

refactor(site): Redesign the agent row #7226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove popovers
  • Loading branch information
BrunoQuaresma committed Apr 20, 2023
commit 843f55b0e69fd7acb2b08e2796af28e61af9242f
151 changes: 3 additions & 148 deletions site/src/components/Resources/AgentMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,124 +1,16 @@
import Popover from "@material-ui/core/Popover"
import makeStyles from "@material-ui/core/styles/makeStyles"
import { watchAgentMetadata } from "api/api"
import { WorkspaceAgent, WorkspaceAgentMetadata } from "api/typesGenerated"
import { Stack } from "components/Stack/Stack"
import {
HelpTooltipText,
HelpTooltipTitle,
} from "components/Tooltips/HelpTooltip"
import dayjs from "dayjs"
import {
createContext,
FC,
PropsWithChildren,
useContext,
useEffect,
useRef,
useState,
} from "react"
import { humanDuration } from "utils/duration"
import { createContext, FC, useContext, useEffect, useState } from "react"
import { Skeleton } from "@material-ui/lab"
import { MONOSPACE_FONT_FAMILY } from "theme/constants"

type ItemStatus = "stale" | "valid" | "loading"

export const WatchAgentMetadataContext = createContext(watchAgentMetadata)

const MetadataItemValue: FC<
PropsWithChildren<{ item: WorkspaceAgentMetadata; status: ItemStatus }>
> = ({ item, children, status }) => {
const [isOpen, setIsOpen] = useState(false)
const anchorRef = useRef<HTMLDivElement>(null)
const styles = useStyles()
const updatesInSeconds = -(item.description.interval - item.result.age)

return (
<>
<div
ref={anchorRef}
onMouseEnter={() => setIsOpen(true)}
role="presentation"
>
{children}
</div>

<Popover
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
transformOrigin={{
vertical: "top",
horizontal: "left",
}}
open={isOpen}
anchorEl={anchorRef.current}
onClose={() => setIsOpen(false)}
PaperProps={{
onMouseEnter: () => setIsOpen(true),
onMouseLeave: () => setIsOpen(false),
}}
classes={{ paper: styles.metadataPopover }}
>
{item.result.error.length > 0 ? (
<>
<div className={styles.metadataPopoverContent}>
<HelpTooltipTitle>
{item.description.display_name}
</HelpTooltipTitle>
<HelpTooltipText>
An error happened while executing the command{" "}
<pre className={styles.inlineCommand}>
`{item.description.script}`
</pre>
</HelpTooltipText>
</div>
<div className={styles.metadataPopoverCode}>
<pre>{item.result.error}</pre>
</div>
</>
) : (
<>
<div className={styles.metadataPopoverContent}>
<HelpTooltipTitle>
{item.description.display_name}
</HelpTooltipTitle>
{status === "stale" ? (
<HelpTooltipText>
This item is now stale because the agent hasn{"'"}t reported a
new value in {humanDuration(item.result.age, "s")}.
</HelpTooltipText>
) : (
<></>
)}
{status === "valid" ? (
<HelpTooltipText>
The agent collected this value{" "}
{humanDuration(item.result.age, "s")} ago and will update it
in {humanDuration(Math.min(updatesInSeconds, 0), "s")}.
</HelpTooltipText>
) : (
<></>
)}
{status === "loading" ? (
<HelpTooltipText>
This value is loading for the first time...
</HelpTooltipText>
) : (
<></>
)}
</div>
<div className={styles.metadataPopoverCode}>
<pre>{item.description.script}</pre>
</div>
</>
)}
</Popover>
</>
)
}

const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
const styles = useStyles()

Expand Down Expand Up @@ -172,12 +64,10 @@ const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {

return (
<div className={styles.metadata}>
<div className={styles.metadataLabel} role="presentation">
<div className={styles.metadataLabel}>
{item.description.display_name}
</div>
<MetadataItemValue item={item} status={status}>
{value}
</MetadataItemValue>
<div>{value}</div>
</div>
)
}
Expand Down Expand Up @@ -307,41 +197,6 @@ const useStyles = makeStyles((theme) => ({
color: theme.palette.error.main,
},

metadataPopover: {
marginTop: theme.spacing(0.5),

color: theme.palette.text.secondary,
pointerEvents: "auto",
width: "320px",
borderRadius: 4,

"& .MuiButton-root": {
padding: theme.spacing(1, 2),
borderRadius: 0,
border: 0,

"&:hover": {
background: theme.palette.action.hover,
},
},
},

metadataPopoverContent: {
padding: theme.spacing(2.5),
},

metadataPopoverCode: {
padding: theme.spacing(2.5),
fontFamily: MONOSPACE_FONT_FAMILY,
background: theme.palette.background.default,
color: theme.palette.text.primary,

"& pre": {
padding: 0,
margin: 0,
},
},

skeleton: {
marginTop: theme.spacing(0.5),
},
Expand Down