diff --git a/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx b/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx index 1d6d194a59902..2bf02bcc28e57 100644 --- a/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx +++ b/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx @@ -237,8 +237,9 @@ function OwnerAgentLibrarySettingsScreen() { const setAgentEnabled = useAtomCommand(agentPersonaEnvironment.setAgentPersonaEnabled, { reportFailure: false, }); - async function toggleAgent(personaId: string, enabled: boolean) { - if (effectiveEnvironmentId === null || busy) return; + /** Resolves whether the server took the change, so the row can drop its pending value. */ + async function toggleAgent(personaId: string, enabled: boolean): Promise { + if (effectiveEnvironmentId === null || busy) return false; setBusy(true); const result = await setAgentEnabled({ environmentId: effectiveEnvironmentId, @@ -247,9 +248,10 @@ function OwnerAgentLibrarySettingsScreen() { setBusy(false); if (result._tag === "Failure") { setNotification(failureNotice("Persona action failed", result)); - return; + return false; } catalog.refresh(); + return true; } async function removePersona(personaId: string) { if (effectiveEnvironmentId === null || busy) return; @@ -506,7 +508,7 @@ function OwnerAgentLibrarySettingsScreen() { > {effectiveEnvironmentId === null ? ( - ) : catalog.isPending ? ( + ) : catalog.isPending && catalog.data == null ? ( ) : catalog.error ? ( @@ -515,12 +517,13 @@ function OwnerAgentLibrarySettingsScreen() { ) : ( personas.map((persona, index) => ( 0} locked={locked} busy={busy} - onToggle={(enabled) => void toggleAgent(persona.personaId, enabled)} + onToggle={(enabled) => toggleAgent(persona.personaId, enabled)} onOpen={() => setViewing(persona.personaId)} /> )) @@ -554,7 +557,7 @@ function OwnerAgentLibrarySettingsScreen() { : "Folders this environment reads YAML definitions from, on its machine; relative paths resolve from its state directory. Files are read on every catalog request, so edits and git pulls apply without a restart." } > - {librarySources.isPending ? ( + {librarySources.isPending && librarySources.data == null ? ( ) : librarySources.error ? ( @@ -679,10 +682,15 @@ function PersonaRow(props: { readonly borderTop: boolean; readonly locked: boolean; readonly busy: boolean; - readonly onToggle: (enabled: boolean) => void; + readonly onToggle: (enabled: boolean) => Promise; readonly onOpen: () => void; }) { const { persona } = props; + // The tapped value holds until the reread catalog shows it; a controlled Switch otherwise snaps + // back to the old value while the toggle saves. A failed toggle drops it, so the row reverts. + const [pending, setPending] = useState(null); + const enabled = pending ?? persona.enabled; + if (pending !== null && pending === persona.enabled) setPending(null); const marker = personaRowMarker({ removed: persona.removed, blocked: @@ -708,9 +716,7 @@ function PersonaRow(props: { > {persona.displayName} @@ -731,10 +737,16 @@ function PersonaRow(props: { { + setPending(next); + void props.onToggle(next).then((saved) => { + if (!saved) setPending(null); + }); + }} /> )}