Skip to content

Commit 0119379

Browse files
Merge pull request #122 from codesandbox/fix/add-api-client-to-runningvms-query-fn
fix: add api client to runningvms query fn
2 parents 76af091 + 75f56da commit 0119379

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

package-lock.json

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bin/ui/Dashboard.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ function useTerminalSize() {
2121

2222
// Component to open a sandbox by ID
2323
export function Dashboard() {
24+
const { apiClient } = useSDK();
25+
2426
// Poll getRunningVms API every 2 seconds
2527
const runningVmsQuery = useQuery({
2628
queryKey: ["runningVms"],
27-
queryFn: getRunningVms,
29+
queryFn: () => getRunningVms(apiClient),
2830
});
2931

3032
const [sandboxId, setSandboxId] = useState("");
@@ -103,11 +105,11 @@ const Sandbox = memo(
103105
}) => {
104106
const sandboxQuery = useQuery({
105107
queryKey: ["sandbox", id],
106-
queryFn: () => getSandbox(id),
108+
queryFn: () => getSandbox(apiClient, id),
107109
});
108110
const runningStateRef = useRef(runningState);
109111

110-
const sdk = useSDK();
112+
const { sdk, apiClient } = useSDK();
111113

112114
// Only two states: RUNNING or IDLE
113115
const [sandboxState, setSandboxState] = useState<

src/bin/ui/sdkContext.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import * as React from "react";
22
import { createContext, useContext } from "react";
33
import { CodeSandbox } from "@codesandbox/sdk";
4+
import { createApiClient } from "../../utils/api";
5+
import { Client } from "@hey-api/client-fetch";
6+
import { getInferredApiKey } from "../../utils/constants";
7+
import { instrumentedFetch } from "../utils/sentry";
48

59
const sdk = new CodeSandbox();
610

7-
export const SDKContext = createContext<CodeSandbox>(sdk);
11+
const apiKey = getInferredApiKey();
12+
const apiClient: Client = createApiClient(apiKey, {}, instrumentedFetch);
13+
14+
export const SDKContext = createContext<{ sdk: CodeSandbox; apiClient: Client }>({
15+
sdk,
16+
apiClient,
17+
});
18+
819

920
export const SDKProvider = ({ children }: { children: React.ReactNode }) => {
10-
return <SDKContext.Provider value={sdk}>{children}</SDKContext.Provider>;
21+
return (
22+
<SDKContext.Provider value={{ sdk, apiClient }}>
23+
{children}
24+
</SDKContext.Provider>
25+
);
1126
};
1227

1328
export function useSDK() {

0 commit comments

Comments
 (0)