Skip to content

Commit e2cb0e5

Browse files
authored
fix: o(n) requests on project switch + perisstance (#801)
### TL;DR Improved project selection logic and optimized query cache clearing when switching projects. closes #798 ### What changed? - Enhanced project selection logic to maintain the current project when switching accounts if it's available in the new account's scoped teams - Added `setStep` to the dependency arrays in `useActionSelectorState` to ensure proper reactivity - Modified query cache clearing behavior when switching projects to only remove project-specific queries while preserving project list and user data - Added proper handling of available project IDs when authenticating ### How to test? 1. Log in with an account that has access to multiple projects 2. Switch between projects and verify that the current project is maintained if available in both accounts 3. Verify that the project switcher still shows the correct list of available projects after switching 4. Check that navigation to task input works correctly after project selection ### Why make this change? This change improves the user experience when switching between projects by: 1. Maintaining context when possible (keeping the same project if available) 2. Preserving necessary data in the query cache to avoid unnecessary refetching 3. Ensuring the project switcher has the correct data available immediately
1 parent 8ed3253 commit e2cb0e5

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

apps/twig/src/renderer/components/action-selector/useActionSelectorState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ export function useActionSelectorState({
245245
customInput,
246246
onSelect,
247247
saveCurrentStepAnswer,
248+
setStep,
248249
]);
249250

250251
const selectByIndex = useCallback(
@@ -291,6 +292,7 @@ export function useActionSelectorState({
291292
toggleCheck,
292293
onSelect,
293294
saveCurrentStepAnswer,
295+
setStep,
294296
],
295297
);
296298

apps/twig/src/renderer/features/auth/stores/authStore.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)