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" ;
28import { type Session } from "@supabase/supabase-js" ;
39import { type Codespace } from "../App/Dashboard/codespace.types" ;
410import { 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
2027const 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
3038const 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<{
241257export const useCodespaceContext = ( ) => {
242258 const context = useContext ( CodespaceContext ) ;
243259 return context ;
244- } ;
260+ } ;
0 commit comments