From 7819f1a28ac366ac25950cba9488245943c7056c Mon Sep 17 00:00:00 2001 From: Tyler Slaton Date: Mon, 30 Nov 2026 11:26:24 -0800 Subject: [PATCH] OpenBot Mobile: every screen follows a change of organization at once, with no navigation error After a pick in Account, Chats went on showing the organization before for up to ten seconds: each screen keeps its own read, and read again only on its ten-second turn. Coming from an empty organization, one with six conversations read "No chats yet" for eight seconds. Coming the other way, the other organization's conversations stayed on screen. - `chooseOrganization` says the pick (src/organization-chosen.ts), and `useLoad`, which every screen reads with, starts again on it. What was read is dropped first, and an answer still on its way from the organization before is not kept. The person's own list of organizations stays on screen while it is read again (`keep`). - Chats forgets the older conversations it had loaded when the organization changes. - Account called `router.dismissAll()` with nothing to dismiss, now that it is a tab, which reported "The action 'POP_TO_TOP' was not handled by any navigator". It asks `router.canDismiss()` first. Measured on the iOS simulator, with three organizations: after a pick, Chats showed the new organization's list within one second, both ways. Before, it took eight. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 5 ++ mobile/app/(tabs)/(account)/account.tsx | 9 +++- mobile/app/(tabs)/(conversations)/index.tsx | 10 +++- mobile/src/api/request.ts | 9 +++- mobile/src/organization-chosen.ts | 34 ++++++++++++ mobile/src/use-load.ts | 50 ++++++++++++++++-- mobile/tests/organization-chosen.test.ts | 58 +++++++++++++++++++++ 7 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 mobile/src/organization-chosen.ts create mode 100644 mobile/tests/organization-chosen.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fed5d3581..0f497efa06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged. ## Unreleased +- **OpenBot Mobile follows a change of organization at once.** After you picked another organization + in Account, Chats and the other screens went on showing the one before for up to ten seconds, so an + organization with conversations could read "No chats yet". Every screen now reads again the moment + you pick, and nothing of the other organization stays on screen. Picking one from the Account tab + also no longer reports a navigation error. - **OpenBot Mobile draws the cards a Bot answers with.** Charts, tables, records, metrics, checklists, notices, quotations and activity reports appear in the conversation on the phone as they do in the web app, as the Bot writes them. The questions a Bot asks with a card (approval, diff --git a/mobile/app/(tabs)/(account)/account.tsx b/mobile/app/(tabs)/(account)/account.tsx index 04c3d22fe8..46e154782d 100644 --- a/mobile/app/(tabs)/(account)/account.tsx +++ b/mobile/app/(tabs)/(account)/account.tsx @@ -43,7 +43,10 @@ export default function Account() { .catch(() => {}); }, []); const loadOrganizations = useCallback(() => organizationChoices(), []); - const organizations = useLoad(loadOrganizations, Boolean(session)); + // The person's own list, whichever organization they work in: it stays on screen while it is read again. + const organizations = useLoad(loadOrganizations, Boolean(session), { + keep: true, + }); async function act(name: string, work: () => Promise) { setBusy(name); setError(""); @@ -101,7 +104,9 @@ export default function Account() { void act(organization.id, async () => { await chooseOrganization(organization.id); await organizations.refresh(); - router.dismissAll(); + // Anything open over the tabs is the other organization's. Nothing is + // open when Account is a tab, and there is then nothing to dismiss. + if (router.canDismiss()) router.dismissAll(); router.replace("/"); }) } diff --git a/mobile/app/(tabs)/(conversations)/index.tsx b/mobile/app/(tabs)/(conversations)/index.tsx index 0d98c90c32..3768c55170 100644 --- a/mobile/app/(tabs)/(conversations)/index.tsx +++ b/mobile/app/(tabs)/(conversations)/index.tsx @@ -58,8 +58,16 @@ export default function Conversations() { }, [session?.user.id]); // The person's organizations and the one they work in; picking another reads everything below again. const loadOrganizations = useCallback(() => organizationChoices(), []); - const organizations = useLoad(loadOrganizations, Boolean(session)); + const organizations = useLoad(loadOrganizations, Boolean(session), { + keep: true, + }); const workingIn = organizations.data?.currentId; + // The older conversations already loaded are the other organization's once the person works in another. + // biome-ignore lint/correctness/useExhaustiveDependencies: start the list again for the organization worked in now + useEffect(() => { + setOlder([]); + setOlderCursor(undefined); + }, [workingIn]); const inNone = homeView(Boolean(session), organizations.error) === "in-no-organization"; /* diff --git a/mobile/src/api/request.ts b/mobile/src/api/request.ts index 54d4d8ed13..fc49d29b16 100644 --- a/mobile/src/api/request.ts +++ b/mobile/src/api/request.ts @@ -23,6 +23,7 @@ import { ORGANIZATION_HEADER, type OrganizationChoices, } from "../organization"; +import { reportOrganizationChosen } from "../organization-chosen"; import { reportOrganizationGone } from "../organization-gone"; import { clientHeaders } from "../release"; import { reportRenewalNeedsClick } from "../renewal"; @@ -228,7 +229,13 @@ export async function organizationChoices(): Promise { export async function chooseOrganization( id: string, ): Promise { - return organizations.choose(id, (await authClient.getAccessToken()) ?? ""); + const chosen = await organizations.choose( + id, + (await authClient.getAccessToken()) ?? "", + ); + // What every screen holds is the other organization's: each reads again, now (../organization-chosen.ts). + reportOrganizationChosen(); + return chosen; } /** The person asking, as the API addresses them in its `people/{personId}` operations. */ diff --git a/mobile/src/organization-chosen.ts b/mobile/src/organization-chosen.ts new file mode 100644 index 0000000000..3ae5efbd2e --- /dev/null +++ b/mobile/src/organization-chosen.ts @@ -0,0 +1,34 @@ +/** + * The person picked another of their organizations to work in (src/api/request.ts, `chooseOrganization`). Every + * request after that goes to the new one, and what the screens already hold is the old one's. So the pick is said + * here, and every screen's reads start again at once (./use-load.ts). Without it a screen went on showing the old + * organization's conversations until its next read, up to ten seconds later. + * + * It imports nothing, so it can be tested without a phone. + */ + +type Listener = () => void; +const listeners = new Set(); +let times = 0; + +/** The person works in another organization from now on. */ +export function reportOrganizationChosen(): void { + times += 1; + for (const listener of [...listeners]) listener(); +} + +/** + * How often an organization was picked since the app started. A read that was asked at one count and answered at + * another is the old organization's, and is dropped. + */ +export function organizationChosenTimes(): number { + return times; +} + +/** Hear each pick. Returns how to stop hearing. */ +export function onOrganizationChosen(listener: Listener): () => void { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; +} diff --git a/mobile/src/use-load.ts b/mobile/src/use-load.ts index 10d2c1d474..259104e150 100644 --- a/mobile/src/use-load.ts +++ b/mobile/src/use-load.ts @@ -1,15 +1,49 @@ -import { useCallback, useEffect, useState } from "react"; +import { + useCallback, + useEffect, + useRef, + useState, + useSyncExternalStore, +} from "react"; import { AppState } from "react-native"; -export function useLoad(load: () => Promise, enabled = true) { +import { + onOrganizationChosen, + organizationChosenTimes, +} from "./organization-chosen"; + +/** + * Read something a screen shows, again every ten seconds while the app is in front, and at once when the person + * picks another organization to work in (./organization-chosen.ts). + * + * What was read belongs to the organization it was read in. After a pick it is dropped before the new one's is + * asked for, so no screen shows one organization's conversations under another's name, and an answer still on its + * way from the old one is not kept. `keep` is for what is the person's own whichever organization they work in + * (the list of their organizations): it is read again, and what is on screen stays until the answer is in. + */ +export function useLoad( + load: () => Promise, + enabled = true, + { keep = false }: { keep?: boolean } = {}, +) { const [data, setData] = useState(); const [error, setError] = useState(); const [pending, setPending] = useState(false); + const chosen = useSyncExternalStore( + onOrganizationChosen, + organizationChosenTimes, + organizationChosenTimes, + ); const refresh = useCallback(async () => { + const asked = organizationChosenTimes(); setPending(true); try { - setData(await load()); + const value = await load(); + // The person works in another organization since this was asked: the answer is the other one's. + if (asked !== organizationChosenTimes()) return; + setData(value); setError(undefined); } catch (failure) { + if (asked !== organizationChosenTimes()) return; setError( failure instanceof Error ? failure.message @@ -19,17 +53,25 @@ export function useLoad(load: () => Promise, enabled = true) { setPending(false); } }, [load]); + const shown = useRef(chosen); useEffect(() => { if (!enabled) { setData(undefined); setError(undefined); return; } + if (shown.current !== chosen) { + shown.current = chosen; + if (!keep) { + setData(undefined); + setError(undefined); + } + } void refresh(); const timer = setInterval(() => { if (AppState.currentState === "active") void refresh(); }, 10_000); return () => clearInterval(timer); - }, [enabled, refresh]); + }, [enabled, refresh, chosen, keep]); return { data, error, pending, refresh }; } diff --git a/mobile/tests/organization-chosen.test.ts b/mobile/tests/organization-chosen.test.ts new file mode 100644 index 0000000000..5ce2f0025b --- /dev/null +++ b/mobile/tests/organization-chosen.test.ts @@ -0,0 +1,58 @@ +/** + * The pick of another organization, said to every screen (src/organization-chosen.ts), and the two places that + * depend on it: the request that makes the pick, and the hook every screen reads with. + */ + +import { expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { + onOrganizationChosen, + organizationChosenTimes, + reportOrganizationChosen, +} from "../src/organization-chosen"; + +const source = (file: string) => + readFileSync(join(import.meta.dir, "..", file), "utf8"); + +test("each pick is heard, and counted", () => { + const before = organizationChosenTimes(); + let heard = 0; + const stop = onOrganizationChosen(() => { + heard += 1; + }); + reportOrganizationChosen(); + reportOrganizationChosen(); + expect(heard).toBe(2); + expect(organizationChosenTimes()).toBe(before + 2); + stop(); + reportOrganizationChosen(); + expect(heard).toBe(2); + expect(organizationChosenTimes()).toBe(before + 3); +}); + +test("a listener that stops hearing while a pick is said doesn't stop the others", () => { + let second = 0; + const stopFirst = onOrganizationChosen(() => stopFirst()); + const stopSecond = onOrganizationChosen(() => { + second += 1; + }); + reportOrganizationChosen(); + expect(second).toBe(1); + stopSecond(); +}); + +test("picking an organization says so, once the pick is kept", () => { + // The request needs a phone, so its source is read: the pick is reported after it is made, never before. + const request = source("src/api/request.ts"); + const picked = request.indexOf("await organizations.choose("); + const said = request.indexOf("reportOrganizationChosen();"); + expect(picked).toBeGreaterThan(0); + expect(said).toBeGreaterThan(picked); +}); + +test("every screen's read starts again on a pick, and an answer from before it is dropped", () => { + const hook = source("src/use-load.ts"); + expect(hook).toContain("onOrganizationChosen"); + expect(hook).toContain("if (asked !== organizationChosenTimes()) return;"); +});