Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions mobile/app/(tabs)/(account)/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>) {
setBusy(name);
setError("");
Expand Down Expand Up @@ -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("/");
})
}
Expand Down
10 changes: 9 additions & 1 deletion mobile/app/(tabs)/(conversations)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
/*
Expand Down
9 changes: 8 additions & 1 deletion mobile/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -228,7 +229,13 @@ export async function organizationChoices(): Promise<OrganizationChoices> {
export async function chooseOrganization(
id: string,
): Promise<OrganizationChoices> {
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. */
Expand Down
34 changes: 34 additions & 0 deletions mobile/src/organization-chosen.ts
Original file line number Diff line number Diff line change
@@ -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<Listener>();
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);
};
}
50 changes: 46 additions & 4 deletions mobile/src/use-load.ts
Original file line number Diff line number Diff line change
@@ -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<T>(load: () => Promise<T>, 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<T>(
load: () => Promise<T>,
enabled = true,
{ keep = false }: { keep?: boolean } = {},
) {
const [data, setData] = useState<T>();
const [error, setError] = useState<string>();
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
Expand All @@ -19,17 +53,25 @@ export function useLoad<T>(load: () => Promise<T>, 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 };
}
58 changes: 58 additions & 0 deletions mobile/tests/organization-chosen.test.ts
Original file line number Diff line number Diff line change
@@ -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;");
});
Loading