@@ -262,10 +262,12 @@ export const useAuthStore = create<AuthState>()(
262262 } ;
263263
264264 const apiHost = getCloudUrlFromRegion ( state . cloudRegion ) ;
265+ const scopedTeams = tokenResponse . scoped_teams ?? [ ] ;
266+ const storedProjectId = state . projectId ;
265267 const projectId =
266- tokenResponse . scoped_teams ?. [ 0 ] ||
267- state . projectId ||
268- undefined ;
268+ storedProjectId && scopedTeams . includes ( storedProjectId )
269+ ? storedProjectId
270+ : ( scopedTeams [ 0 ] ?? storedProjectId ?? undefined ) ;
269271
270272 const client = new PostHogAPIClient (
271273 tokenResponse . access_token ,
@@ -288,6 +290,10 @@ export const useAuthStore = create<AuthState>()(
288290 storedTokens,
289291 client,
290292 ...( projectId && { projectId } ) ,
293+ availableProjectIds :
294+ scopedTeams . length > 0
295+ ? scopedTeams
296+ : state . availableProjectIds ,
291297 } ) ;
292298
293299 trpcVanilla . agent . updateToken
@@ -665,8 +671,15 @@ export const useAuthStore = create<AuthState>()(
665671 storedTokens : updatedTokens ,
666672 } ) ;
667673
668- // Clear query cache to fetch data for the new project
669- queryClient . clear ( ) ;
674+ // Clear project-scoped queries, but keep project list/user for the switcher
675+ queryClient . removeQueries ( {
676+ predicate : ( query ) => {
677+ const key = Array . isArray ( query . queryKey )
678+ ? query . queryKey [ 0 ]
679+ : query . queryKey ;
680+ return key !== "projects" && key !== "currentUser" ;
681+ } ,
682+ } ) ;
670683
671684 // Navigate to task input after project selection
672685 useNavigationStore . getState ( ) . navigateToTaskInput ( ) ;
0 commit comments