11import { createLogger } from '@sim/logger'
2- import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query'
2+ import { queryOptions , useMutation , useQuery , useQueryClient } from '@tanstack/react-query'
33import { requestJson } from '@/lib/api/client/request'
44import type { ContractBodyInput } from '@/lib/api/contracts'
55import {
66 createSandboxContract ,
77 deleteSandboxContract ,
8+ listSandboxesContract ,
89 type Sandbox ,
910 type SandboxListResponse ,
1011 updateSandboxContract ,
1112} from '@/lib/api/contracts'
12- import { getSandboxListQueryOptions , sandboxKeys } from '@/hooks/queries/sandbox-list'
1313
1414const logger = createLogger ( 'SandboxQueries' )
1515
1616export type { Sandbox , SandboxListResponse }
1717
18+ export const sandboxKeys = {
19+ all : [ 'sandboxes' ] as const ,
20+ lists : ( ) => [ ...sandboxKeys . all , 'list' ] as const ,
21+ list : ( workspaceId ?: string ) => [ ...sandboxKeys . lists ( ) , workspaceId ?? '' ] as const ,
22+ }
23+
24+ const SANDBOX_LIST_STALE_TIME = 30 * 1000
25+
1826/** Poll cadence while any sandbox is still building; see {@link useSandboxes}. */
1927export const SANDBOX_BUILD_POLL_INTERVAL = 3 * 1000
2028
@@ -25,6 +33,22 @@ export const SANDBOX_BUILD_POLL_INTERVAL = 3 * 1000
2533 */
2634const MAX_BUILD_POLLS = 350
2735
36+ async function fetchSandboxes (
37+ workspaceId : string ,
38+ signal ?: AbortSignal
39+ ) : Promise < SandboxListResponse > {
40+ return requestJson ( listSandboxesContract , { params : { id : workspaceId } , signal } )
41+ }
42+
43+ function getSandboxListQueryOptions ( workspaceId : string ) {
44+ return queryOptions ( {
45+ queryKey : sandboxKeys . list ( workspaceId ) ,
46+ queryFn : ( { signal } ) => fetchSandboxes ( workspaceId , signal ) ,
47+ retryOnMount : true ,
48+ staleTime : SANDBOX_LIST_STALE_TIME ,
49+ } )
50+ }
51+
2852/** True while at least one sandbox has a build that has not reached a terminal state. */
2953export function hasPendingBuild ( sandboxes : readonly Sandbox [ ] ) : boolean {
3054 return sandboxes . some (
0 commit comments