@@ -54,6 +54,14 @@ export interface ProjectContextType {
5454 setRecentlyCompletedProjectId : ( id : string | null ) => void ;
5555 chatId : string | null ;
5656 setChatId : ( chatId : string | null ) => void ;
57+ pendingProjects : Project [ ] ;
58+ setPendingProjects : React . Dispatch < React . SetStateAction < Project [ ] > > ;
59+ refetchPublicProjects : ( ) => Promise < any > ;
60+ setRefetchPublicProjects : React . Dispatch <
61+ React . SetStateAction < ( ) => Promise < any > >
62+ > ;
63+ tempLoadingProjectId : string | null ;
64+ setTempLoadingProjectId : React . Dispatch < React . SetStateAction < string | null > > ;
5765}
5866
5967export const ProjectContext = createContext < ProjectContextType | undefined > (
@@ -102,13 +110,14 @@ const checkUrlStatus = async (
102110
103111export function ProjectProvider ( { children } : { children : ReactNode } ) {
104112 const router = useRouter ( ) ;
105- const { isAuthorized } = useAuthContext ( ) ;
113+ const { isAuthorized, user } = useAuthContext ( ) ;
106114 const [ projects , setProjects ] = useState < Project [ ] > ( [ ] ) ;
107115 const [ curProject , setCurProject ] = useState < Project | undefined > ( undefined ) ;
108116 const [ projectLoading , setProjectLoading ] = useState < boolean > ( true ) ;
109117 const [ filePath , setFilePath ] = useState < string | null > ( null ) ;
110118 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
111119 const editorRef = useRef < any > ( null ) ;
120+ const [ pendingProjects , setPendingProjects ] = useState < Project [ ] > ( [ ] ) ;
112121 const [ recentlyCompletedProjectIdRaw , setRecentlyCompletedProjectIdRaw ] =
113122 useState < string | null > ( ( ) =>
114123 typeof window !== 'undefined'
@@ -128,6 +137,9 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
128137 const [ chatId , setChatId ] = useState < string | null > ( null ) ;
129138 const [ pollTime , setPollTime ] = useState ( Date . now ( ) ) ;
130139 const [ isCreateButtonClicked , setIsCreateButtonClicked ] = useState ( false ) ;
140+ const [ tempLoadingProjectId , setTempLoadingProjectId ] = useState <
141+ string | null
142+ > ( null ) ;
131143 interface ChatProjectCacheEntry {
132144 project : Project | null ;
133145 timestamp : number ;
@@ -153,7 +165,9 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
153165 const MAX_RETRIES = 30 ;
154166 const CACHE_TTL = 5 * 60 * 1000 ; // 5 minutes TTL for cache
155167 const SYNC_DEBOUNCE_TIME = 1000 ; // 1 second debounce for sync operations
156-
168+ const [ refetchPublicProjects , setRefetchPublicProjects ] = useState <
169+ ( ) => Promise < any >
170+ > ( ( ) => async ( ) => { } ) ;
157171 // Mounted ref to prevent state updates after unmount
158172 const isMounted = useRef ( true ) ;
159173
@@ -710,18 +724,13 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
710724 model = 'gpt-4o-mini'
711725 ) : Promise < string > => {
712726 if ( ! prompt . trim ( ) ) {
713- if ( isMounted . current ) {
714- toast . error ( 'Please enter a project description' ) ;
715- }
727+ toast . error ( 'Please enter a project description' ) ;
716728 throw new Error ( 'Invalid prompt' ) ;
717729 }
718730
719731 try {
720- if ( isMounted . current ) {
721- setIsLoading ( true ) ;
722- }
732+ setIsLoading ( true ) ;
723733
724- // Default packages based on typical web project needs
725734 const defaultPackages = [
726735 { name : 'react' , version : '^18.2.0' } ,
727736 { name : 'next' , version : '^13.4.0' } ,
@@ -734,32 +743,29 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
734743 description : prompt ,
735744 packages : defaultPackages ,
736745 public : isPublic ,
737- model : model ,
746+ model,
738747 } ,
739748 } ,
740749 } ) ;
750+
741751 const createdChat = result . data ?. createProject ;
742752 if ( createdChat ?. id ) {
743753 setChatId ( createdChat . id ) ;
744754 setIsCreateButtonClicked ( true ) ;
745755 localStorage . setItem ( 'pendingChatId' , createdChat . id ) ;
756+ setTempLoadingProjectId ( createdChat . id ) ;
746757 return createdChat . id ;
747758 } else {
748759 throw new Error ( 'Project creation failed: no chatId' ) ;
749760 }
750761 } catch ( error ) {
751- logger . error ( 'Error creating project:' , error ) ;
752- if ( isMounted . current ) {
753- toast . error ( 'Failed to create project from prompt' ) ;
754- }
762+ toast . error ( 'Failed to create project from prompt' ) ;
755763 throw error ;
756764 } finally {
757- if ( isMounted . current ) {
758- setIsLoading ( false ) ;
759- }
765+ setIsLoading ( false ) ;
760766 }
761767 } ,
762- [ createProject , setChatId ]
768+ [ createProject , setChatId , user ]
763769 ) ;
764770
765771 // New function to fork a project
@@ -870,6 +876,30 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
870876 retryCount : retries ,
871877 } ) ;
872878
879+ // First update the project list to ensure it exists in allProjects
880+ setProjects ( ( prev ) => {
881+ const exists = prev . find ( ( p ) => p . id === project . id ) ;
882+ return exists ? prev : [ ...prev , project ] ;
883+ } ) ;
884+
885+ // Then more aggressively clean up pending projects
886+ setPendingProjects ( ( prev ) => {
887+ const filtered = prev . filter ( ( p ) => p . id !== project . id ) ;
888+ if ( filtered . length !== prev . length ) {
889+ logger . info (
890+ `Removed project ${ project . id } from pending projects`
891+ ) ;
892+ }
893+ return filtered ;
894+ } ) ;
895+
896+ // Then trigger the public projects refetch
897+ await refetchPublicProjects ( ) ;
898+ console . log (
899+ '[pollChatProject] refetchPublicProjects triggered after project is ready:' ,
900+ project . id
901+ ) ;
902+
873903 // Trigger state sync if needed
874904 if (
875905 now - projectSyncState . current . lastSyncTime >=
@@ -887,6 +917,9 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
887917 } ) ;
888918 }
889919
920+ if ( isMounted . current ) {
921+ setTempLoadingProjectId ( null ) ;
922+ }
890923 return project ;
891924 }
892925 } catch ( error ) {
@@ -921,6 +954,7 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
921954 MAX_RETRIES ,
922955 CACHE_TTL ,
923956 SYNC_DEBOUNCE_TIME ,
957+ refetchPublicProjects ,
924958 ]
925959 ) ;
926960
@@ -969,6 +1003,12 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
9691003 setChatId,
9701004 recentlyCompletedProjectId : recentlyCompletedProjectIdRaw ,
9711005 setRecentlyCompletedProjectId,
1006+ pendingProjects,
1007+ setPendingProjects,
1008+ refetchPublicProjects,
1009+ setRefetchPublicProjects,
1010+ tempLoadingProjectId,
1011+ setTempLoadingProjectId,
9721012 } ) ,
9731013 [
9741014 projects ,
@@ -988,6 +1028,12 @@ export function ProjectProvider({ children }: { children: ReactNode }) {
9881028 chatId ,
9891029 setChatId ,
9901030 recentlyCompletedProjectIdRaw ,
1031+ pendingProjects ,
1032+ setPendingProjects ,
1033+ refetchPublicProjects ,
1034+ setRefetchPublicProjects ,
1035+ tempLoadingProjectId ,
1036+ setTempLoadingProjectId ,
9911037 ]
9921038 ) ;
9931039
0 commit comments