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

Skip to content

Commit 5272ae6

Browse files
committed
feat: add spinner to latencies when refetching
1 parent eb0497f commit 5272ae6

File tree

7 files changed

+59
-14
lines changed

7 files changed

+59
-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: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Skeleton from "@mui/material/Skeleton"
2424
import { BUTTON_SM_HEIGHT } from "theme/theme"
2525
import { ProxyStatusLatency } from "components/ProxyStatusLatency/ProxyStatusLatency"
2626
import { usePermissions } from "hooks/usePermissions"
27+
import { ProxyLatencyReport } from "contexts/useProxyLatency"
2728

2829
export const USERS_LINK = `/users?filter=${encodeURIComponent("status:active")}`
2930

@@ -188,6 +189,7 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
188189
}) => {
189190
const buttonRef = useRef<HTMLButtonElement>(null)
190191
const [isOpen, setIsOpen] = useState(false)
192+
const [refetchDate, setRefetchDate] = useState<Date>()
191193
const selectedProxy = proxyContextValue.proxy.proxy
192194
const refreshLatencies = proxyContextValue.refetchProxyLatencies
193195
const closeMenu = () => setIsOpen(false)
@@ -196,6 +198,26 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
196198
const isLoadingLatencies = Object.keys(latencies).length === 0
197199
const isLoading = proxyContextValue.isLoading || isLoadingLatencies
198200
const permissions = usePermissions()
201+
const proxyLatencyLoading = (proxy: TypesGen.Region): boolean => {
202+
if (!refetchDate) {
203+
// Only show loading if the user manually requested a refetch
204+
return false
205+
}
206+
207+
const latency = latencies?.[proxy.id]
208+
// Only show a loading spinner if:
209+
// - A latency exists. This means the latency was fetched at some point, so the
210+
// loader *should* be resolved.
211+
// - The proxy is healthy. If it is not, the loader might never resolve.
212+
// - The latency reported is older than the refetch date. This means the latency
213+
// is stale and we should show a loading spinner until the new latency is
214+
// fetched.
215+
if (proxy.healthy && latency && latency.at < refetchDate) {
216+
return true
217+
}
218+
219+
return false
220+
}
199221

200222
if (isLoading) {
201223
return (
@@ -234,6 +256,7 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
234256
{selectedProxy.display_name}
235257
<ProxyStatusLatency
236258
latency={latencies?.[selectedProxy.id]?.latencyMS}
259+
isLoading={proxyLatencyLoading(selectedProxy)}
237260
/>
238261
</Box>
239262
) : (
@@ -277,7 +300,10 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
277300
/>
278301
</Box>
279302
{proxy.display_name}
280-
<ProxyStatusLatency latency={latencies?.[proxy.id]?.latencyMS} />
303+
<ProxyStatusLatency
304+
latency={latencies?.[proxy.id]?.latencyMS}
305+
isLoading={proxyLatencyLoading(proxy)}
306+
/>
281307
</Box>
282308
</MenuItem>
283309
))}
@@ -298,7 +324,8 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
298324
// Stop the menu from closing
299325
e.stopPropagation()
300326
// Refresh the latencies.
301-
refreshLatencies()
327+
const refetchDate = refreshLatencies()
328+
setRefetchDate(refetchDate)
302329
}}
303330
>
304331
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)