Skip to content

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

fix(sidebar): send department order saves one after the other

Bugfixer 2ececd019de00
diff --git a/src/components/bot-sections.test.ts b/src/components/bot-sections.test.ts
new file mode 100644
index 0000000000..19026ec304
--- /dev/null
+++ b/src/components/bot-sections.test.ts
@@ -0,0 +1,26 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const sent: { body: string; finish: () => void }[] = [];
+vi.mock("@/state/store", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/state/store")>()),
+ api: vi.fn((_path: string, init: { body: string }) => new Promise<void>((resolve) => sent.push({ body: init.body, finish: resolve }))),
+}));
+const { saveDepartmentOrder } = await import("./bot-sections");
+
+const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
+afterEach(() => { sent.length = 0; });
+
+describe("saving the department order", () => {
+ it("sends two quick moves one after the other, so the later move is the one the server keeps", async () => {
+ const dispatch = vi.fn();
+ const first = saveDepartmentOrder(dispatch, ["Sales", "Ops", "Legal"], ["Ops", "Sales", "Legal"]);
+ const second = saveDepartmentOrder(dispatch, ["Ops", "Sales", "Legal"], ["Legal", "Ops", "Sales"]);
+ await settle();
+ expect(sent.map(({ body }) => JSON.parse(body).sections)).toEqual([["Ops", "Sales", "Legal"]]);
+ sent[0].finish();
+ await settle();
+ expect(sent.map(({ body }) => JSON.parse(body).sections)).toEqual([["Ops", "Sales", "Legal"], ["Legal", "Ops", "Sales"]]);
+ sent[1].finish();
+ expect(await Promise.all([first, second])).toEqual([true, true]);
+ });
+});
diff --git a/src/components/bot-sections.ts b/src/components/bot-sections.ts
index 03c128e286..1e33f4bbc9 100644
--- a/src/components/bot-sections.ts
+++ b/src/components/bot-sections.ts
@@ -65,6 +65,8 @@ function offerDepartmentOrder(names: string[]): void {
}
let departmentOrderRequest = 0;
+/** Moves reach the server in the order they were made: the server keeps the last order it gets. */
+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. */
@@ -74,7 +76,9 @@ export async function saveDepartmentOrder(dispatch: Dispatch<Action>, sections:
const request = ++departmentOrderRequest;
dispatch({ type: "sectionsOrdered", sections: names });
try {
- await api("/api/sidebar-sections/order", { method: "PUT", body: JSON.stringify({ sections: names }) });
+ const saved = departmentOrderSaves.then(() => api("/api/sidebar-sections/order", { method: "PUT", body: JSON.stringify({ sections: names }) }));
+ departmentOrderSaves = saved.catch(() => undefined);
+ await saved;
return true;
} catch (cause) {
if (request === departmentOrderRequest) dispatch({ type: "sectionsOrdered", sections: before });