Skip to content

Commit 0431fdf

Browse files
fixed share load codespace and branch creatings
1 parent 9e56ec0 commit 0431fdf

File tree

5 files changed

+105
-109
lines changed

5 files changed

+105
-109
lines changed

Codespace_Service/src/controllers/codespaceController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ export class CodespaceController {
188188
static async createSession(req, res, next) {
189189
try {
190190
const { codespaceId, branchName } = req.body;
191-
const codespace = await CodespaceService.createBranchWithSession(
191+
const session = await CodespaceService.createBranchWithSession(
192192
codespaceId,
193193
branchName
194194
);
195195

196196
res.status(201).json({
197-
codespace,
197+
session,
198198
message: "Session created successfully",
199199
});
200200
} catch (error) {

Frontend/src/App/CodeEditor/GitPanel/GitPanel.tsx

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,10 @@ const GitPanel = () => {
2929
initGitState();
3030
}, []);
3131

32-
// Handle branch selection
33-
// const handleBranchSelect = async (branchName: string) => {
34-
// try {
35-
// setIsLoading(true);
36-
// setError(null);
37-
38-
// // Switch branch and get updated state
39-
// const updatedState = await switchBranch(branchName);
40-
// setGitState(updatedState);
41-
// } catch (err) {
42-
// setError(`Failed to switch to branch: ${branchName}`);
43-
// console.error(err);
44-
// } finally {
45-
// setIsLoading(false);
46-
// }
47-
// };
48-
4932
return (
5033
<div className={`h-full flex flex-col ${theme.surface} ${theme.text}`}>
5134
<div className={`p-4 ${theme.border} border-b`}>
52-
<BranchSelector
53-
// branches={gitState.branches}
54-
// onBranchSelect={handleBranchSelect}
55-
/>
35+
<BranchSelector />
5636
</div>
5737

5838
<div className="flex-grow overflow-auto p-4 Simple-Scrollbar">
@@ -95,10 +75,7 @@ const GitPanel = () => {
9575

9676
{!isLoading && (
9777
<>
98-
<CommitHistory
99-
// commits={gitState.commits}
100-
// onRollback={handleRollback}
101-
/>
78+
<CommitHistory />
10279
</>
10380
)}
10481
</div>

Frontend/src/App/Dashboard/AcceptInvite.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useNavigate, useParams } from "react-router";
33
import { Users, AlertCircle, ArrowRight, Info } from "lucide-react";
44
import ThemeToggleButton from "../../components/ThemeToggleBtn";
55
import { useTheme } from "../../Contexts/ThemeProvider";
6+
import { useCodespaceContext } from "../../Contexts/CodespaceContext";
67

78
const getToken = () => {
89
const storageKey = `sb-${
@@ -20,6 +21,7 @@ const CollaboratePage: React.FC = () => {
2021
const navigate = useNavigate();
2122
const { invitationId } = useParams<{ invitationId: string }>();
2223
const { theme } = useTheme();
24+
const { refreshCodespaces } = useCodespaceContext();
2325
const [error, setError] = useState<string | null>(null);
2426
const [isLoading, setIsLoading] = useState(false);
2527
const [showContent, setShowContent] = useState(false);
@@ -54,7 +56,6 @@ const CollaboratePage: React.FC = () => {
5456
localStorage.setItem("invitationId", invitationId);
5557
console.warn("Unauthorized! Redirecting to login.");
5658
navigate("/login", { state: { invitationId } });
57-
5859
return;
5960
}
6061

@@ -66,6 +67,8 @@ const CollaboratePage: React.FC = () => {
6667
const data = await response.json();
6768
console.log("API response22:", data.invitation.workspace_id);
6869

70+
await refreshCodespaces();
71+
6972
navigate(
7073
data.invitation.workspace_id
7174
? `/codeeditor/${data.invitation.workspace_id}`

Frontend/src/Contexts/CodespaceContext.tsx

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { createContext, useContext, useState, useEffect } from "react";
1+
import React, {
2+
createContext,
3+
useContext,
4+
useState,
5+
useEffect,
6+
useCallback,
7+
} from "react";
28
import { type Session } from "@supabase/supabase-js";
39
import { type Codespace } from "../App/Dashboard/codespace.types";
410
import { formatDateTime, getTokenFromStorage } from "../utility/utility";
@@ -15,6 +21,7 @@ interface CodespaceContextType {
1521
role: string
1622
) => Promise<boolean>;
1723
editCodespace: (id: string, newName: string) => Promise<boolean>;
24+
refreshCodespaces: () => Promise<void>;
1825
}
1926

2027
const initialCodespaceContext: CodespaceContextType = {
@@ -25,6 +32,7 @@ const initialCodespaceContext: CodespaceContextType = {
2532
deleteCodespace: async () => false,
2633
shareCodespaceByEmail: async () => false,
2734
editCodespace: async () => false,
35+
refreshCodespaces: async () => {},
2836
};
2937

3038
const CodespaceContext = createContext<CodespaceContextType>(
@@ -45,91 +53,98 @@ export const CodespaceProvider: React.FC<{
4553
session?.user?.email ||
4654
"Anonymous";
4755

48-
const getToken = () => {
49-
if (session?.access_token) return session.access_token;
50-
return getTokenFromStorage();
51-
};
52-
53-
useEffect(() => {
54-
if (!session) {
55-
setError("No active session. Please log in.");
56-
return;
57-
}
58-
59-
const fetchCodespaces = async () => {
60-
const result = await handleApiRequest(
61-
CODESPACE_API_URL,
62-
"GET",
63-
undefined,
64-
"Failed to fetch codespaces"
65-
);
66-
67-
if (result.success && result.data?.codespaces) {
68-
const codespaceList = result.data.codespaces.map((item: Codespace) => ({
69-
id: item.id,
70-
name: item.name,
71-
role: item.role,
72-
lastModified: formatDateTime(item.lastModified),
73-
owner: userName,
74-
}));
75-
setCodespaces(codespaceList);
56+
const handleApiRequest = useCallback(
57+
async (
58+
endpoint: string,
59+
method: string,
60+
body?: object,
61+
errorMessage = "API request failed"
62+
) => {
63+
if (!session) {
64+
setError("You must be logged in");
65+
return { success: false };
7666
}
77-
};
7867

79-
fetchCodespaces();
80-
}, [session]);
81-
82-
const handleApiRequest = async (
83-
endpoint: string,
84-
method: string,
85-
body?: object,
86-
errorMessage = "API request failed"
87-
) => {
88-
if (!session) {
89-
setError("You must be logged in");
90-
return { success: false };
91-
}
68+
setLoading(true);
9269

93-
setLoading(true);
70+
const getToken = () => {
71+
if (session?.access_token) return session.access_token;
72+
return getTokenFromStorage();
73+
};
9474

95-
try {
96-
const token = getToken();
97-
if (!token) {
98-
setError("No authentication token available");
75+
try {
76+
const token = getToken();
77+
if (!token) {
78+
setError("No authentication token available");
79+
setLoading(false);
80+
return { success: false };
81+
}
82+
83+
const headers: HeadersInit = {
84+
Authorization: token,
85+
"Content-Type": "application/json",
86+
};
87+
88+
const requestOptions: RequestInit = {
89+
method,
90+
headers,
91+
...(body && { body: JSON.stringify(body) }),
92+
};
93+
94+
const response = await fetch(endpoint, requestOptions);
95+
96+
if (!response.ok) {
97+
const errorData = await response.json().catch(() => ({}));
98+
setError(`Server error: ${errorData.message || response.status}`);
99+
setLoading(false);
100+
return { success: false };
101+
}
102+
103+
const data = await response.json().catch(() => ({}));
104+
setLoading(false);
105+
return { success: true, data };
106+
} catch {
107+
//console.error(errorMessage, error);
108+
setError(errorMessage);
99109
setLoading(false);
100110
return { success: false };
101111
}
112+
},
113+
[session]
114+
);
102115

103-
const headers: HeadersInit = {
104-
Authorization: token,
105-
"Content-Type": "application/json",
106-
};
116+
const fetchCodespaces = useCallback(async () => {
117+
if (!session) {
118+
setError("No active session. Please log in.");
119+
return;
120+
}
107121

108-
const requestOptions: RequestInit = {
109-
method,
110-
headers,
111-
...(body && { body: JSON.stringify(body) }),
112-
};
122+
const result = await handleApiRequest(
123+
CODESPACE_API_URL,
124+
"GET",
125+
undefined,
126+
"Failed to fetch codespaces"
127+
);
113128

114-
const response = await fetch(endpoint, requestOptions);
129+
if (result.success && result.data?.codespaces) {
130+
const codespaceList = result.data.codespaces.map((item: Codespace) => ({
131+
id: item.id,
132+
name: item.name,
133+
role: item.role,
134+
lastModified: formatDateTime(item.lastModified),
135+
owner: userName,
136+
}));
137+
setCodespaces(codespaceList);
138+
}
139+
}, [CODESPACE_API_URL, handleApiRequest, session, userName]);
115140

116-
if (!response.ok) {
117-
const errorData = await response.json().catch(() => ({}));
118-
setError(`Server error: ${errorData.message || response.status}`);
119-
setLoading(false);
120-
return { success: false };
121-
}
141+
useEffect(() => {
142+
fetchCodespaces();
143+
}, [fetchCodespaces]);
122144

123-
const data = await response.json().catch(() => ({}));
124-
setLoading(false);
125-
return { success: true, data };
126-
} catch (error) {
127-
//console.error(errorMessage, error);
128-
setError(errorMessage);
129-
setLoading(false);
130-
return { success: false };
131-
}
132-
};
145+
const refreshCodespaces = useCallback(async () => {
146+
await fetchCodespaces();
147+
}, [fetchCodespaces]);
133148

134149
const createCodespace = async (workspaceName: string): Promise<boolean> => {
135150
if (!workspaceName.trim()) {
@@ -230,6 +245,7 @@ export const CodespaceProvider: React.FC<{
230245
deleteCodespace,
231246
shareCodespaceByEmail,
232247
editCodespace,
248+
refreshCodespaces,
233249
}}
234250
>
235251
{children}
@@ -241,4 +257,4 @@ export const CodespaceProvider: React.FC<{
241257
export const useCodespaceContext = () => {
242258
const context = useContext(CodespaceContext);
243259
return context;
244-
};
260+
};

Frontend/src/Contexts/EditorContext.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export const EditorCollaborationProvider: React.FC<{
233233
try {
234234
setLoading(true);
235235
setGitOperationLoading(true);
236-
236+
237237
const response = await fetch(`${CODESPACE_API_URL}/${codespaceId}`, {
238238
headers: getAuthHeader(),
239239
});
@@ -889,11 +889,11 @@ export const EditorCollaborationProvider: React.FC<{
889889
const result = await response.json();
890890

891891
const newSession = {
892-
sessionId: result.sessionId,
893-
branchId: result.branchId,
894-
name: result.name,
895-
commits: result.commits || [],
896-
startedAt: result.startedAt,
892+
sessionId: result.session.sessionId,
893+
branchId: result.session.branchId,
894+
name: result.session.name,
895+
commits: result.session.commits || [],
896+
startedAt: result.session.startedAt,
897897
};
898898

899899
const updatedCodespace = {

0 commit comments

Comments
 (0)