Skip to content

Commit 7530498

Browse files
fix(agentex-ui): reset agent selection to the home grid on account switch
A selected agent is account-scoped, but its name persisted in the URL across a switch — so a same-named agent in the new account stayed selected (and #347's localStorage restore could reinstate it). On an account change, clear agent_name and the remembered agent so the view resets to the grid. task_id is already cleared by the switch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 10022fe commit 7530498

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

agentex-ui/components/agentex-ui-root.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useCallback, useEffect, useState } from 'react';
3+
import { useCallback, useEffect, useRef, useState } from 'react';
44

55
import { ToastContainer } from 'react-toastify';
66

@@ -17,7 +17,8 @@ import {
1717
} from '@/hooks/use-safe-search-params';
1818

1919
export function AgentexUIRoot() {
20-
const { agentName, taskID, updateParams } = useSafeSearchParams();
20+
const { agentName, taskID, sgpAccountID, updateParams } =
21+
useSafeSearchParams();
2122
const [isTracesSidebarOpen, setIsTracesSidebarOpen] = useState(false);
2223
const { agentexClient } = useAgentexClient();
2324
const { data: agents = [], isFetching: isAgentsFetching } =
@@ -32,13 +33,24 @@ export function AgentexUIRoot() {
3233
const [localAgentName, setLocalAgentName] = useLocalStorageState<
3334
string | undefined
3435
>('lastSelectedAgent', undefined);
36+
const accountRef = useRef(sgpAccountID);
3537

3638
// Wait until neither query is fetching before validating. We gate on `isFetching` (not
3739
// `isLoading`, which is false once a query has cached data) so we never validate against
3840
// stale data mid-refetch. A disabled by-name query reports `isFetching: false`, so it
3941
// doesn't block the localStorage-restore path when there's no agent_name. Deps are
4042
// intentionally narrowed so we re-validate on fetch settle / agent_name change.
4143
useEffect(() => {
44+
// An account switch resets to the home grid: the selected agent is account-scoped, so a
45+
// same-named agent in the new account must not stay selected. Clear the remembered agent
46+
// too, so the restore below doesn't reinstate it. (task_id is cleared by the switch.)
47+
if (accountRef.current !== sgpAccountID) {
48+
accountRef.current = sgpAccountID;
49+
setLocalAgentName(undefined);
50+
if (agentName) updateParams({ [SearchParamKey.AGENT_NAME]: null });
51+
return;
52+
}
53+
4254
if (isAgentsFetching || isAgentByNameFetching) return;
4355

4456
// Accept an agent found in the (paginated) list OR resolved directly by name, so a valid
@@ -61,7 +73,13 @@ export function AgentexUIRoot() {
6173
updateParams({ [SearchParamKey.AGENT_NAME]: localAgentName });
6274
}
6375
// eslint-disable-next-line react-hooks/exhaustive-deps
64-
}, [isAgentsFetching, isAgentByNameFetching, isAgentByNameError, agentName]);
76+
}, [
77+
sgpAccountID,
78+
isAgentsFetching,
79+
isAgentByNameFetching,
80+
isAgentByNameError,
81+
agentName,
82+
]);
6583

6684
const handleSelectTask = useCallback(
6785
(taskId: string | null) => {

0 commit comments

Comments
 (0)