1- import { createContext , useContext , useState , useEffect , ReactNode } from 'react' ;
1+ import { createContext , useContext , useState , useEffect , useRef , ReactNode } from 'react' ;
22import { useQuery , useMutation , useApolloClient } from '@apollo/client' ;
33import { useAuth } from './AuthContext' ;
44import { Graph , GraphHierarchy , CreateGraphInput , GraphContextType , GraphPermissions , ShareSettings } from '../types/graph' ;
@@ -25,7 +25,7 @@ export function GraphProvider({ children }: GraphProviderProps) {
2525 const [ currentGraph , setCurrentGraph ] = useState < Graph | null > ( null ) ;
2626 const [ availableGraphs , setAvailableGraphs ] = useState < Graph [ ] > ( [ ] ) ;
2727 const [ isCreating , setIsCreating ] = useState ( false ) ;
28- const [ isCreatingWelcomeGraph , setIsCreatingWelcomeGraph ] = useState ( false ) ;
28+ const isCreatingWelcomeGraphRef = useRef ( false ) ;
2929
3030
3131 // GraphQL operations - Load graphs for current user only
@@ -77,6 +77,18 @@ export function GraphProvider({ children }: GraphProviderProps) {
7777
7878 // Load graphs from GraphQL response only
7979 useEffect ( ( ) => {
80+ // GUARD: Don't run if we don't have a logged-in user
81+ if ( ! currentUser ) {
82+ console . log ( '⏸️ No current user - skipping graph loading' ) ;
83+ return ;
84+ }
85+
86+ // GUARD: Don't run while GraphQL query is loading
87+ if ( isLoading ) {
88+ console . log ( '⏸️ Still loading graphs - waiting...' ) ;
89+ return ;
90+ }
91+
8092 // Combine user graphs, shared graphs, and system graphs
8193 // Remove duplicates by graph ID (some graphs may appear in multiple categories)
8294 const graphsMap = new Map ( ) ;
@@ -91,18 +103,21 @@ export function GraphProvider({ children }: GraphProviderProps) {
91103 } ) ;
92104
93105 const allGraphs = Array . from ( graphsMap . values ( ) ) ;
106+ const userGraphs = graphsData ?. userGraphs || [ ] ;
94107
95108 console . log ( '🔍 GraphContext useEffect triggered:' , {
96109 isLoading,
97110 hasGraphsError : ! ! graphsError ,
98111 hasCurrentUser : ! ! currentUser ,
99112 currentUserId : currentUser ?. id ,
100113 allGraphsCount : allGraphs . length ,
114+ userGraphsCount : userGraphs . length ,
101115 systemGraphsCount : graphsData ?. systemGraphs ?. length || 0 ,
102- userGraphsCount : graphsData ?. userGraphs ?. length || 0 ,
103- sharedGraphsCount : graphsData ?. sharedGraphs ?. length || 0
116+ sharedGraphsCount : graphsData ?. sharedGraphs ?. length || 0 ,
117+ isCreatingWelcomeGraph : isCreatingWelcomeGraphRef . current
104118 } ) ;
105119
120+ // CASE 1: User has graphs - display them
106121 if ( allGraphs . length > 0 ) {
107122 // Parse JSON strings in settings, permissions, shareSettings
108123 const parsedGraphs = allGraphs . map ( ( g : any ) => ( {
@@ -148,24 +163,41 @@ export function GraphProvider({ children }: GraphProviderProps) {
148163 localStorage . setItem ( 'currentGraphId' , selectedGraph . id ) ;
149164 }
150165 }
151- } else if ( ! isLoading && ! graphsError && currentUser && ! isCreatingWelcomeGraph ) {
152- // Check if shared Welcome graph exists (created by server onboarding service)
153- const hasWelcomeGraph = [ ...( graphsData ?. systemGraphs || [ ] ) , ...( graphsData ?. sharedGraphs || [ ] ) ]
154- . some ( g => g . name === 'Welcome' ) ;
155-
156- if ( ! hasWelcomeGraph ) {
157- console . log ( '🎉 No graphs found for user:' , currentUser . id , '- Triggering Welcome graph creation' ) ;
158- // No graphs available - automatically create Welcome graph for new users
159- setAvailableGraphs ( [ ] ) ;
160- setCurrentGraph ( null ) ;
161- setIsCreatingWelcomeGraph ( true ) ;
162- } else {
163- console . log ( '✅ Shared Welcome graph already exists, skipping creation' ) ;
166+ } else {
167+ // CASE 2: User has NO graphs - check if we should create a Welcome graph
168+
169+ // GUARD: Don't create multiple Welcome graphs (prevent race condition)
170+ if ( isCreatingWelcomeGraphRef . current ) {
171+ console . log ( '⏸️ Already creating Welcome graph - skipping duplicate creation' ) ;
164172 return ;
165173 }
166174
167- // Create Welcome graph automatically with tutorial content
168- const createWelcomeGraphWithTutorial = async ( ) => {
175+ // Check if user already has a Welcome graph
176+ const hasWelcomeGraph = userGraphs . some ( ( g : any ) => g . name === 'Welcome' ) ;
177+
178+ console . log ( '🔍 DEBUG: No graphs found - checking Welcome graph creation' , {
179+ userGraphsCount : userGraphs . length ,
180+ allGraphsCount : allGraphs . length ,
181+ hasWelcomeGraph,
182+ isCreatingWelcomeGraph : isCreatingWelcomeGraphRef . current ,
183+ currentUserId : currentUser . id
184+ } ) ;
185+
186+ // CONDITION: Create Welcome graph ONLY if:
187+ // 1. User has NO graphs at all (allGraphs.length === 0)
188+ // 2. User doesn't already have a Welcome graph (!hasWelcomeGraph)
189+ // 3. We're not already creating one (!isCreatingWelcomeGraphRef.current)
190+ if ( ! hasWelcomeGraph && allGraphs . length === 0 ) {
191+ console . log ( '🎉 Creating Welcome graph for new user:' , currentUser . id ) ;
192+
193+ // Set flag IMMEDIATELY to prevent duplicate creation
194+ isCreatingWelcomeGraphRef . current = true ;
195+
196+ setAvailableGraphs ( [ ] ) ;
197+ setCurrentGraph ( null ) ;
198+
199+ // Create Welcome graph automatically with tutorial content
200+ const createWelcomeGraphWithTutorial = async ( ) => {
169201 console . log ( '📝 Starting Welcome graph creation process...' ) ;
170202 try {
171203 // Step 1: Create the Welcome graph
@@ -347,13 +379,25 @@ export function GraphProvider({ children }: GraphProviderProps) {
347379 await refetchGraphs ( ) ;
348380 } catch ( error ) {
349381 console . error ( '❌ Failed to create Welcome graph:' , error ) ;
350- setIsCreatingWelcomeGraph ( false ) ; // Allow retry on next attempt
382+ isCreatingWelcomeGraphRef . current = false ; // Allow retry on next attempt
351383 }
352384 } ;
353385
354- createWelcomeGraphWithTutorial ( ) ;
386+ createWelcomeGraphWithTutorial ( ) ;
387+ }
355388 }
356- } , [ graphsData , currentTeam , currentUser , isLoading , graphsError , createGraphMutation , createWorkItemMutation , createEdgeMutation , refetchGraphs ] ) ;
389+ } , [
390+ graphsData ,
391+ currentTeam ,
392+ currentUser ,
393+ isLoading ,
394+ graphsError ,
395+ createGraphMutation ,
396+ createWorkItemMutation ,
397+ createEdgeMutation ,
398+ updateGraphMutation ,
399+ refetchGraphs
400+ ] ) ;
357401
358402 // Build graph hierarchy
359403 const graphHierarchy : GraphHierarchy [ ] = availableGraphs
0 commit comments