Skip to content

Commit 0f5a1ad

Browse files
authored
feat: add spinner to latencies when refetching (#8278)
* feat: add spinner to latencies when refetching
1 parent 9a7705c commit 0f5a1ad

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

site/src/components/AppLink/AppLink.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const Template: Story<AppLinkProps> = (args) => (
2929
clearProxy: () => {
3030
return
3131
},
32-
refetchProxyLatencies: () => {
33-
return
32+
refetchProxyLatencies: (): Date => {
33+
return new Date()
3434
},
3535
}}
3636
>

site/src/components/Navbar/NavbarView.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
188188
}) => {
189189
const buttonRef = useRef<HTMLButtonElement>(null)
190190
const [isOpen, setIsOpen] = useState(false)
191+
const [refetchDate, setRefetchDate] = useState<Date>()
191192
const selectedProxy = proxyContextValue.proxy.proxy
192193
const refreshLatencies = proxyContextValue.refetchProxyLatencies
193194
const closeMenu = () => setIsOpen(false)
@@ -196,6 +197,26 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
196197
const isLoadingLatencies = Object.keys(latencies).length === 0
197198
const isLoading = proxyContextValue.isLoading || isLoadingLatencies
198199
const permissions = usePermissions()
200+
const proxyLatencyLoading = (proxy: TypesGen.Region): boolean => {
201+
if (!refetchDate) {
202+
// Only show loading if the user manually requested a refetch
203+
return false
204+
}
205+
206+
const latency = latencies?.[proxy.id]
207+
// Only show a loading spinner if:
208+
// - A latency exists. This means the latency was fetched at some point, so the
209+
// loader *should* be resolved.
210+
// - The proxy is healthy. If it is not, the loader might never resolve.
211+
// - The latency reported is older than the refetch date. This means the latency
212+
// is stale and we should show a loading spinner until the new latency is
213+
// fetched.
214+
if (proxy.healthy && latency && latency.at < refetchDate) {
215+
return true
216+
}
217+
218+
return false
219+
}
199220

200221
if (isLoading) {
201222
return (
@@ -234,6 +255,7 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
234255
{selectedProxy.display_name}
235256
<ProxyStatusLatency
236257
latency={latencies?.[selectedProxy.id]?.latencyMS}
258+
isLoading={proxyLatencyLoading(selectedProxy)}
237259
/>
238260
</Box>
239261
) : (
@@ -277,7 +299,10 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
277299
/>
278300
</Box>
279301
{proxy.display_name}
280-
<ProxyStatusLatency latency={latencies?.[proxy.id]?.latencyMS} />
302+
<ProxyStatusLatency
303+
latency={latencies?.[proxy.id]?.latencyMS}
304+
isLoading={proxyLatencyLoading(proxy)}
305+
/>
281306
</Box>
282307
</MenuItem>
283308
))}
@@ -298,7 +323,8 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
298323
// Stop the menu from closing
299324
e.stopPropagation()
300325
// Refresh the latencies.
301-
refreshLatencies()
326+
const refetchDate = refreshLatencies()
327+
setRefetchDate(refetchDate)
302328
}}
303329
>
304330
Refresh Latencies

site/src/components/ProxyStatusLatency/ProxyStatusLatency.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@ import Box from "@mui/material/Box"
44
import Tooltip from "@mui/material/Tooltip"
55
import { FC } from "react"
66
import { getLatencyColor } from "utils/latency"
7+
import CircularProgress from "@mui/material/CircularProgress"
78

8-
export const ProxyStatusLatency: FC<{ latency?: number }> = ({ latency }) => {
9+
export const ProxyStatusLatency: FC<{
10+
latency?: number
11+
isLoading?: boolean
12+
}> = ({ latency, isLoading }) => {
913
const theme = useTheme()
1014
const color = getLatencyColor(theme, latency)
1115

16+
if (isLoading) {
17+
return (
18+
<Tooltip title="Loading latency...">
19+
<CircularProgress
20+
size="14px"
21+
sx={{
22+
// Always use the no latency color for loading.
23+
color: getLatencyColor(theme, undefined),
24+
}}
25+
/>
26+
</Tooltip>
27+
)
28+
}
29+
1230
if (!latency) {
1331
return (
1432
<Tooltip title="Latency not available">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ const TemplateFC = (
6868
clearProxy: () => {
6969
return
7070
},
71-
refetchProxyLatencies: () => {
72-
return
71+
refetchProxyLatencies: (): Date => {
72+
return new Date()
7373
},
7474
}}
7575
>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Example.args = {
3333
clearProxy: () => {
3434
return
3535
},
36-
refetchProxyLatencies: () => {
37-
return
36+
refetchProxyLatencies: (): Date => {
37+
return new Date()
3838
},
3939
}}
4040
>
@@ -106,8 +106,8 @@ BunchOfMetadata.args = {
106106
clearProxy: () => {
107107
return
108108
},
109-
refetchProxyLatencies: () => {
110-
return
109+
refetchProxyLatencies: (): Date => {
110+
return new Date()
111111
},
112112
}}
113113
>

site/src/components/Workspace/Workspace.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const meta: Meta<typeof Workspace> = {
4242
setProxy: () => {
4343
return
4444
},
45-
refetchProxyLatencies: () => {
46-
return
45+
refetchProxyLatencies: (): Date => {
46+
return new Date()
4747
},
4848
}}
4949
>

site/src/contexts/ProxyContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface ProxyContextValue {
5353
proxyLatencies: Record<string, ProxyLatencyReport>
5454
// refetchProxyLatencies will trigger refreshing of the proxy latencies. By default the latencies
5555
// are loaded once.
56-
refetchProxyLatencies: () => void
56+
refetchProxyLatencies: () => Date
5757
// setProxy is a function that sets the user's selected proxy. This function should
5858
// only be called if the user is manually selecting a proxy. This value is stored in local
5959
// storage and will persist across reloads and tabs.

0 commit comments

Comments
 (0)