Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(sidebar): after a department-order save with no answer, show the order the server holds

Bugfixer 2ff18c38ebe33
diff --git a/src/components/bot-sections.test.ts b/src/components/bot-sections.test.ts
index 48cf002b78..dc055bf0c5 100644
--- a/src/components/bot-sections.test.ts
+++ b/src/components/bot-sections.test.ts
@@ -1,12 +1,16 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-const sent: { body: string; timeoutMs?: number; finish: () => void }[] = [];
+const sent: { body: string; timeoutMs?: number; finish: () => void; fail: (cause: unknown) => void }[] = [];
+let held: string[] = [];
vi.mock("@/state/store", async (importOriginal) => ({
...(await importOriginal<typeof import("@/state/store")>()),
- api: vi.fn((_path: string, init: { body: string; timeoutMs?: number }) =>
- new Promise<void>((resolve) => sent.push({ body: init.body, timeoutMs: init.timeoutMs, finish: resolve }))),
+ api: vi.fn((path: string, init: { method?: string; body: string; timeoutMs?: number }) =>
+ init.method === "PUT"
+ ? new Promise<void>((resolve, reject) => sent.push({ body: init.body, timeoutMs: init.timeoutMs, finish: resolve, fail: reject }))
+ : Promise.resolve(path === "/api/sidebar-sections" ? { sections: held } : {})),
}));
const { saveDepartmentOrder } = await import("./bot-sections");
+const { ApiError } = await import("@/state/store");
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
afterEach(() => { sent.length = 0; });
@@ -33,4 +37,34 @@ describe("saving the department order", () => {
sent[0].finish();
expect(await saving).toBe(true);
});
+
+ it("shows the order the server holds after a save that got no answer, since the server may have kept it", async () => {
+ held = ["Hidden", "Ops", "Sales", "Legal"];
+ const dispatch = vi.fn();
+ const saving = saveDepartmentOrder(dispatch, ["Sales", "Ops", "Legal"], ["Ops", "Sales"]);
+ await settle();
+ sent[0].fail(new DOMException("The operation timed out.", "TimeoutError"));
+ expect(await saving).toBe(false);
+ expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ type: "error" }));
+ expect(dispatch).toHaveBeenNthCalledWith(2, { type: "sectionsOrdered", sections: ["Ops", "Sales"] });
+ });
+
+ it("reads the order the server holds after a server error too, since the save may have been kept", async () => {
+ held = ["Ops", "Sales"];
+ const dispatch = vi.fn();
+ const saving = saveDepartmentOrder(dispatch, ["Sales", "Ops"], ["Ops", "Sales"]);
+ await settle();
+ sent[0].fail(new ApiError("Bad Gateway", 502));
+ expect(await saving).toBe(false);
+ expect(dispatch).toHaveBeenNthCalledWith(2, { type: "sectionsOrdered", sections: ["Ops", "Sales"] });
+ });
+
+ it("puts the order back after a save the server refused", async () => {
+ const dispatch = vi.fn();
+ const saving = saveDepartmentOrder(dispatch, ["Sales", "Ops"], ["Ops", "Sales"]);
+ await settle();
+ sent[0].fail(new ApiError("unknown department: Ops", 400));
+ expect(await saving).toBe(false);
+ expect(dispatch).toHaveBeenNthCalledWith(2, { type: "sectionsOrdered", sections: ["Sales", "Ops"] });
+ });
});
diff --git a/src/components/bot-sections.ts b/src/components/bot-sections.ts
index ac1468e1d8..c57fa1f8e5 100644
--- a/src/components/bot-sections.ts
+++ b/src/components/bot-sections.ts
@@ -1,7 +1,7 @@
import type { Dispatch } from "react";
import { useOwnerOrAdmin } from "@/lib/use-owner-or-admin";
import { reorderNamesIfMoved } from "../../shared/bot-order";
-import { api, type Action, type AppState, type Bot } from "@/state/store";
+import { api, ApiError, type Action, type AppState, type Bot } from "@/state/store";
const failure = (cause: unknown) => (cause instanceof Error ? cause.message : String(cause));
@@ -69,7 +69,7 @@ let departmentOrderRequest = 0;
let departmentOrderSaves: Promise<unknown> = Promise.resolve();
/** Shows the departments `names` in this order at once and saves it for every device; every other department keeps
- * its place. True once the workspace has it. A refused save is undone only while no newer move has been made. */
+ * its place. True once the workspace has it. */
export async function saveDepartmentOrder(dispatch: Dispatch<Action>, sections: readonly string[], names: string[]): Promise<boolean> {
if (!reorderNamesIfMoved(sections, names)) return true;
const before = sections.filter((name) => names.includes(name));
@@ -81,12 +81,23 @@ export async function saveDepartmentOrder(dispatch: Dispatch<Action>, sections:
await saved;
return true;
} catch (cause) {
- if (request === departmentOrderRequest) dispatch({ type: "sectionsOrdered", sections: before });
+ await undoMove(dispatch, request, before, cause instanceof ApiError && cause.status < 500);
dispatch({ type: "error", message: failure(cause) });
return false;
}
}
+/** After a failed save, unless a newer move was made: one the server refused (4xx) changed nothing, so the order from
+ * before the move comes back; one with no answer or a server error may have been kept, so the order the server holds is
+ * shown (or the one from before, if that can't be read either). */
+async function undoMove(dispatch: Dispatch<Action>, request: number, before: string[], refused: boolean): Promise<void> {
+ // Only the departments moved here, so a department this person isn't shown stays out of the list.
+ const held = refused ? null : await api("/api/sidebar-sections", { timeoutMs: 10_000 })
+ .then((answer: { sections?: unknown }) => Array.isArray(answer?.sections) ? answer.sections.filter((name) => before.includes(name)) : null)
+ .catch(() => null);
+ if (request === departmentOrderRequest) dispatch({ type: "sectionsOrdered", sections: held ?? before });
+}
+
export async function renameDepartment(dispatch: Dispatch<Action>, from: string, to: string): Promise<void> {
const { sections } = await api(`/api/sidebar-sections?section=${encodeURIComponent(from)}`, { method: "PATCH", body: JSON.stringify({ name: to }) });
dispatch({ type: "sections", sections });