@@ -2,7 +2,7 @@ import { getErrorDetail, getErrorMessage } from "api/errors";
2
2
import { workspacePermissionsByOrganization } from "api/queries/organizations" ;
3
3
import { templates } from "api/queries/templates" ;
4
4
import { workspaces } from "api/queries/workspaces" ;
5
- import type { Workspace } from "api/typesGenerated" ;
5
+ import type { Workspace , WorkspaceStatus } from "api/typesGenerated" ;
6
6
import { useFilter } from "components/Filter/Filter" ;
7
7
import { useUserFilterMenu } from "components/Filter/UserFilter" ;
8
8
import { displayError } from "components/GlobalSnackbar/utils" ;
@@ -22,6 +22,18 @@ import { WorkspacesPageView } from "./WorkspacesPageView";
22
22
import { useBatchActions } from "./batchActions" ;
23
23
import { useStatusFilterMenu , useTemplateFilterMenu } from "./filter/menus" ;
24
24
25
+ // To reduce the number of fetches, we reduce the fetch interval if there are no
26
+ // active workspace builds.
27
+ const ACTIVE_BUILD_STATUSES : WorkspaceStatus [ ] = [
28
+ "canceling" ,
29
+ "deleting" ,
30
+ "pending" ,
31
+ "starting" ,
32
+ "stopping" ,
33
+ ] ;
34
+ const ACTIVE_BUILDS_REFRESH_INTERVAL = 5_000 ;
35
+ const NO_ACTIVE_BUILDS_REFRESH_INTERVAL = 30_000 ;
36
+
25
37
function useSafeSearchParams ( ) {
26
38
// Have to wrap setSearchParams because React Router doesn't make sure that
27
39
// the function's memory reference stays stable on each render, even though
@@ -78,8 +90,23 @@ const WorkspacesPage: FC = () => {
78
90
const { data, error, refetch } = useQuery ( {
79
91
...workspacesQueryOptions ,
80
92
refetchInterval : ( { state } ) => {
81
- return state . error ? false : 5_000 ;
93
+ if ( state . error ) return false ;
94
+
95
+ // Default to 5s interval until first fetch completes
96
+ if ( ! state . data ) return ACTIVE_BUILDS_REFRESH_INTERVAL ;
97
+
98
+ // Check if any workspace has an active build
99
+ const hasActiveBuilds = state . data . workspaces ?. some ( ( workspace ) => {
100
+ const status = workspace . latest_build . status ;
101
+ return ACTIVE_BUILD_STATUSES . includes ( status ) ;
102
+ } ) ;
103
+
104
+ // Poll every 5s if there are active builds, otherwise every 30s
105
+ return hasActiveBuilds
106
+ ? ACTIVE_BUILDS_REFRESH_INTERVAL
107
+ : NO_ACTIVE_BUILDS_REFRESH_INTERVAL ;
82
108
} ,
109
+ refetchOnWindowFocus : "always" ,
83
110
} ) ;
84
111
85
112
const [ checkedWorkspaces , setCheckedWorkspaces ] = useState <
0 commit comments