Skip to content

Commit 50b3808

Browse files
authored
Re-upgrade codesandbox sdk (#1916)
1 parent 340af3c commit 50b3808

File tree

8 files changed

+167
-63
lines changed

8 files changed

+167
-63
lines changed

apps/web/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@codemirror/lang-javascript": "^6.2.3",
3636
"@codemirror/lang-json": "^6.0.1",
3737
"@codemirror/lang-markdown": "^6.1.7",
38-
"@codesandbox/sdk": "^0.11.1",
38+
"@codesandbox/sdk": "^1.1.2",
3939
"@onlook/constants": "*",
4040
"@onlook/db": "*",
4141
"@onlook/fonts": "*",

apps/web/client/src/app/project/[id]/_components/main.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export const Main = observer(({ projectId }: { projectId: string }) => {
4444
projectManager.project = project;
4545

4646
if (project.sandbox?.id) {
47-
editorEngine.sandbox.session.start(project.sandbox.id);
47+
if (userManager.user?.id) {
48+
editorEngine.sandbox.session.start(project.sandbox.id, userManager.user?.id);
49+
} else {
50+
console.error('No user id');
51+
}
4852
} else {
4953
console.error('No sandbox id');
5054
}
@@ -68,7 +72,7 @@ export const Main = observer(({ projectId }: { projectId: string }) => {
6872
return () => {
6973
editorEngine.sandbox.clear();
7074
};
71-
}, [result]);
75+
}, [result, userManager.user?.id]);
7276

7377
useEffect(() => {
7478
const creationData = createManager.pendingCreationData;
@@ -133,16 +137,6 @@ export const Main = observer(({ projectId }: { projectId: string }) => {
133137
};
134138
}, []);
135139

136-
useEffect(() => {
137-
if (
138-
tabState === 'reactivated' &&
139-
editorEngine.sandbox.session.session &&
140-
!editorEngine.sandbox.session.connected()
141-
) {
142-
editorEngine.sandbox.session.reconnect();
143-
}
144-
}, [tabState]);
145-
146140
if (isLoading) {
147141
return (
148142
<div className="h-screen w-screen flex items-center justify-center gap-2">

apps/web/client/src/components/store/editor/sandbox/file-watcher.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import type { SandboxSession, WatchEvent, Watcher } from '@codesandbox/sdk';
1+
import type { WatchEvent, Watcher, WebSocketSession } from '@codesandbox/sdk';
22
import { FileEventBus } from './file-event-bus';
33

44
interface FileWatcherOptions {
5-
session: SandboxSession;
5+
session: WebSocketSession;
66
onFileChange: (event: WatchEvent) => Promise<void>;
77
excludePatterns?: string[];
88
fileEventBus: FileEventBus;
99
}
1010

1111
export class FileWatcher {
1212
private watcher: Watcher | null = null;
13-
private readonly session: SandboxSession;
13+
private readonly session: WebSocketSession;
1414
private readonly onFileChange: (event: WatchEvent) => Promise<void>;
1515
private readonly excludePatterns: string[];
1616
private readonly eventBus: FileEventBus;
@@ -24,12 +24,12 @@ export class FileWatcher {
2424

2525
async start(): Promise<void> {
2626
try {
27-
this.watcher = await this.session.fs.watch('./', {
27+
const watcher = await this.session.fs.watch('./', {
2828
recursive: true,
2929
excludes: this.excludePatterns,
3030
});
31-
32-
this.watcher.onEvent(async (event) => {
31+
this.watcher = watcher
32+
watcher.onEvent(async (event) => {
3333
// Publish the event to all subscribers
3434
this.eventBus.publish({
3535
type: event.type,

apps/web/client/src/components/store/editor/sandbox/session.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,33 @@
11
import { api } from '@/trpc/client';
2-
import type { SandboxSession } from '@codesandbox/sdk';
2+
import type { WebSocketSession } from '@codesandbox/sdk';
33
import { connectToSandbox } from '@codesandbox/sdk/browser';
44
import { makeAutoObservable } from 'mobx';
55

66
export class SessionManager {
7-
session: SandboxSession | null = null;
7+
session: WebSocketSession | null = null;
88
isConnecting = false;
99

1010
constructor() {
1111
makeAutoObservable(this);
1212
}
1313

14-
async start(sandboxId: string) {
14+
async start(sandboxId: string, userId: string) {
1515
this.isConnecting = true;
16-
const startData = await api.sandbox.start.mutate({ sandboxId });
17-
this.session = await connectToSandbox(startData);
16+
this.session = await connectToSandbox({
17+
session: await api.sandbox.start.mutate({ sandboxId, userId }),
18+
getSession: async (id) => {
19+
return await api.sandbox.start.mutate({ sandboxId, userId });
20+
},
21+
});
1822
this.isConnecting = false;
1923
}
2024

2125
async hibernate(sandboxId: string) {
2226
await api.sandbox.hibernate.mutate({ sandboxId });
2327
}
2428

25-
async connected() {
26-
if (!this.session) return false;
27-
try {
28-
await this.session.shells.run('echo "ping"');
29-
return true;
30-
} catch (error) {
31-
console.error('Failed to connect to sandbox', error);
32-
return false;
33-
}
34-
}
35-
3629
async reconnect() {
37-
if (!this.session) {
38-
console.error('No session found');
39-
return;
40-
}
41-
const sandboxId = this.session.id;
42-
this.session.disconnect();
43-
this.session = null;
44-
this.isConnecting = true;
45-
const startData = await api.sandbox.reconnect.mutate({ sandboxId });
46-
this.session = await connectToSandbox(startData);
47-
this.isConnecting = false;
30+
await this.session?.reconnect();
4831
}
4932

5033
async disconnect() {
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CodeSandbox } from '@codesandbox/sdk';
2+
import { shortenUuid } from '@onlook/utility/src/id';
23
import { z } from 'zod';
34
import { createTRPCRouter, protectedProcedure } from '../trpc';
45

@@ -7,30 +8,30 @@ const sdk = new CodeSandbox(process.env.CSB_API_KEY!);
78
export const sandboxRouter = createTRPCRouter({
89
start: protectedProcedure.input(z.object({
910
sandboxId: z.string(),
11+
userId: z.string(),
1012
})).mutation(async ({ input }) => {
11-
const startData = await sdk.sandbox.start(input.sandboxId);
12-
return startData;
13-
}),
14-
// Same as start - Separate endpoint for different state management
15-
reconnect: protectedProcedure.input(z.object({
16-
sandboxId: z.string(),
17-
})).mutation(async ({ input }) => {
18-
const startData = await sdk.sandbox.start(input.sandboxId);
19-
return startData;
13+
const startData = await sdk.sandboxes.resume(input.sandboxId);
14+
const session = await startData.createBrowserSession({
15+
id: shortenUuid(input.userId, 20)
16+
});
17+
return session;
2018
}),
2119
hibernate: protectedProcedure.input(z.object({
2220
sandboxId: z.string(),
2321
})).mutation(async ({ input }) => {
24-
await sdk.sandbox.hibernate(input.sandboxId);
22+
await sdk.sandboxes.hibernate(input.sandboxId);
2523
}),
2624
list: protectedProcedure.query(async () => {
27-
const listResponse = await sdk.sandbox.list();
25+
const listResponse = await sdk.sandboxes.list();
2826
return listResponse;
2927
}),
3028
fork: protectedProcedure.input(z.object({
3129
sandboxId: z.string(),
3230
})).mutation(async ({ input }) => {
33-
const sandbox = await sdk.sandbox.create({ template: input.sandboxId });
31+
const sandbox = await sdk.sandboxes.create({
32+
source: 'template',
33+
id: input.sandboxId,
34+
});
3435
return {
3536
sandboxId: sandbox.id,
3637
previewUrl: `https://${sandbox.id}-8084.csb.app`,
@@ -39,6 +40,6 @@ export const sandboxRouter = createTRPCRouter({
3940
delete: protectedProcedure.input(z.object({
4041
sandboxId: z.string(),
4142
})).mutation(async ({ input }) => {
42-
await sdk.sandbox.shutdown(input.sandboxId);
43+
await sdk.sandboxes.shutdown(input.sandboxId);
4344
}),
4445
});

0 commit comments

Comments
 (0)