Skip to content

Commit f55397e

Browse files
committedJan 18, 2024
Remove unecessary checks
1 parent 223f36b commit f55397e

File tree

1 file changed

+61
-71
lines changed

1 file changed

+61
-71
lines changed
 

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

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,71 +24,6 @@ type ItemStatus = "stale" | "valid" | "loading";
2424

2525
export const WatchAgentMetadataContext = createContext(watchAgentMetadata);
2626

27-
interface MetadataItemProps {
28-
item: WorkspaceAgentMetadata;
29-
}
30-
31-
const MetadataItem: FC<MetadataItemProps> = ({ item }) => {
32-
if (item.result === undefined) {
33-
throw new Error("Metadata item result is undefined");
34-
}
35-
if (item.description === undefined) {
36-
throw new Error("Metadata item description is undefined");
37-
}
38-
39-
const staleThreshold = Math.max(
40-
item.description.interval + item.description.timeout * 2,
41-
// In case there is intense backpressure, we give a little bit of slack.
42-
5,
43-
);
44-
45-
const status: ItemStatus = (() => {
46-
const year = dayjs(item.result.collected_at).year();
47-
if (year <= 1970 || isNaN(year)) {
48-
return "loading";
49-
}
50-
// There is a special circumstance for metadata with `interval: 0`. It is
51-
// expected that they run once and never again, so never display them as
52-
// stale.
53-
if (item.result.age > staleThreshold && item.description.interval > 0) {
54-
return "stale";
55-
}
56-
return "valid";
57-
})();
58-
59-
// Stale data is as good as no data. Plus, we want to build confidence in our
60-
// users that what's shown is real. If times aren't correctly synced this
61-
// could be buggy. But, how common is that anyways?
62-
const value =
63-
status === "loading" ? (
64-
<Skeleton width={65} height={12} variant="text" css={styles.skeleton} />
65-
) : status === "stale" ? (
66-
<Tooltip title="This data is stale and no longer up to date">
67-
<StaticWidth css={[styles.metadataValue, styles.metadataStale]}>
68-
{item.result.value}
69-
</StaticWidth>
70-
</Tooltip>
71-
) : (
72-
<StaticWidth
73-
css={[
74-
styles.metadataValue,
75-
item.result.error.length === 0
76-
? styles.metadataValueSuccess
77-
: styles.metadataValueError,
78-
]}
79-
>
80-
{item.result.value}
81-
</StaticWidth>
82-
);
83-
84-
return (
85-
<div css={styles.metadata}>
86-
<div css={styles.metadataLabel}>{item.description.display_name}</div>
87-
<div>{value}</div>
88-
</div>
89-
);
90-
};
91-
9227
export interface AgentMetadataViewProps {
9328
metadata: WorkspaceAgentMetadata[];
9429
}
@@ -99,12 +34,9 @@ export const AgentMetadataView: FC<AgentMetadataViewProps> = ({ metadata }) => {
9934
}
10035
return (
10136
<section css={styles.root}>
102-
{metadata.map((m) => {
103-
if (m.description === undefined) {
104-
throw new Error("Metadata item description is undefined");
105-
}
106-
return <MetadataItem key={m.description.key} item={m} />;
107-
})}
37+
{metadata.map((m) => (
38+
<MetadataItem key={m.description.key} item={m} />
39+
))}
10840
</section>
10941
);
11042
};
@@ -196,6 +128,64 @@ export const AgentMetadataSkeleton: FC = () => {
196128
);
197129
};
198130

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+
199189
const StaticWidth: FC<HTMLAttributes<HTMLDivElement>> = ({
200190
children,
201191
...attrs

0 commit comments

Comments
 (0)