Skip to content

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

fix(rooms): keep an open room thread when the window gets focus back

AdityaUmale8938a95b1556
diff --git a/src/hooks/use-read-shown-chat.dom.test.ts b/src/hooks/use-read-shown-chat.dom.test.ts
index 57ea5a0194..c6329591bd 100644
--- a/src/hooks/use-read-shown-chat.dom.test.ts
+++ b/src/hooks/use-read-shown-chat.dom.test.ts
@@ -3,7 +3,7 @@ import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
-import { initialState, type Action, type AppState, type Bot } from "@/state/store";
+import { initialState, reducer, type Action, type AppState, type Bot, type Group } from "@/state/store";
const store = vi.hoisted(() => ({ state: null as unknown as AppState, dispatch: (_action: unknown) => {} }));
vi.mock("@/state/store", async (original) => ({ ...(await original<typeof import("@/state/store")>()), useStore: () => store }));
@@ -34,7 +34,7 @@ it("reads the shown chat when the window gets focus back, and only while it is u
root = createRoot(document.createElement("div"));
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
- expect(dispatched).toEqual([{ type: "select", id: "reviewer" }]);
+ expect(dispatched).toEqual([{ type: "select", id: "reviewer", keepPanel: true }]);
store.state = { ...store.state, bots: [bot(false)] };
flushSync(() => root!.render(createElement(Shell)));
@@ -58,6 +58,24 @@ it("leaves a chat the person marked unread alone when the window gets focus back
store.state = { ...store.state, bots: [bot(true, 11)] };
flushSync(() => root!.render(createElement(Shell)));
window.dispatchEvent(new Event("focus"));
- expect(dispatched).toEqual([{ type: "select", id: "reviewer" }]);
+ expect(dispatched).toEqual([{ type: "select", id: "reviewer", keepPanel: true }]);
holdMarkedUnread({ type: "select", id: "reviewer" }, store.state);
});
+
+it("keeps a room thread open beside the room when the window gets focus back and reads the room", () => {
+ const room: Group = {
+ id: "brand", threadId: "main", name: "Brand", memberIds: [], defaultResponder: { kind: "everyone" }, bulletin: "", unread: true,
+ createdAt: 1, messages: [], tasks: [{ threadId: "main", title: "Main", createdAt: 1, unread: true }, { threadId: "open", title: "Open", createdAt: 2 }],
+ };
+ const panel = { kind: "group" as const, ownerId: "brand", threadId: "open", messages: [], activeLeafId: null, loading: false, loadId: "load-1", loadFailed: false };
+ store.state = { ...initialState, activeView: "chat", selectedId: "brand", groups: [room], forkPanel: panel };
+ store.dispatch = (action) => { store.state = reducer(store.state, action as Action); };
+ root = createRoot(document.createElement("div"));
+ flushSync(() => root!.render(createElement(Shell)));
+ window.dispatchEvent(new Event("focus"));
+ expect(store.state.forkPanel?.threadId).toBe("open");
+ expect(store.state.groups[0].tasks?.find((task) => task.threadId === "main")?.unread).toBe(false);
+
+ store.dispatch({ type: "select", id: "brand" });
+ expect(store.state.forkPanel).toBeNull();
+});
diff --git a/src/hooks/use-read-shown-chat.ts b/src/hooks/use-read-shown-chat.ts
index 4702a36e69..c12edc4633 100644
--- a/src/hooks/use-read-shown-chat.ts
+++ b/src/hooks/use-read-shown-chat.ts
@@ -4,8 +4,8 @@ import { shownChatHeldUnread } from "@/state/read-visible";
import { useStore } from "@/state/store";
/** Replies that reached the shown chat while the window was in the background stay unread; once the window has focus
- * again, that chat is read through `select`, which also puts the "New" divider above them. A chat the person marked
- * unread stays so when nothing newer reached it. */
+ * again, that chat is read through `select`, which also puts the "New" divider above them and keeps a thread or fork
+ * open beside it. A chat the person marked unread stays so when nothing newer reached it. */
export function useReadShownChatOnFocus(): void {
const { state, dispatch } = useStore();
const latest = useRef(state);
@@ -13,7 +13,7 @@ export function useReadShownChatOnFocus(): void {
useEffect(() => {
const readShownChat = () => {
const { selectedId, newChat } = latest.current;
- if (selectedId && newChat?.botId !== selectedId && shownChatIsUnread(latest.current) && !shownChatHeldUnread(latest.current)) dispatch({ type: "select", id: selectedId });
+ if (selectedId && newChat?.botId !== selectedId && shownChatIsUnread(latest.current) && !shownChatHeldUnread(latest.current)) dispatch({ type: "select", id: selectedId, keepPanel: true });
};
window.addEventListener("focus", readShownChat);
return () => window.removeEventListener("focus", readShownChat);
diff --git a/src/state/store.tsx b/src/state/store.tsx
index 7d97bc8cdb..3f28b13581 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -658,7 +658,7 @@ export type Action =
/** Applied only if no live.call frame landed and no later lookup answered while it was out. */
| { type: "liveCallLookup"; call: LiveCallState | null; since: number; seq: number }
| { type: "profileSaved"; profile: Partial<NonNullable<ConfigStatus["profile"]>> }
- | { type: "select"; id: string }
+ | { type: "select"; id: string; keepPanel?: boolean }
| {
type: "send";
botId: string;
@@ -1041,7 +1041,7 @@ function reduceApp(state: AppState, action: Action): AppState {
room.messages, room.lastReadMessageId, room.hasMore),
activeView: "chat",
selectedId: action.id,
- forkPanel: null,
+ forkPanel: action.keepPanel && state.forkPanel?.ownerId === action.id ? state.forkPanel : null,
groups: state.groups.map((g) => {
if (g.id !== action.id) return g;
const tasks = g.tasks?.map((task) => task.threadId === g.threadId && !roomFeedShows(state.config, g) ? { ...task, unread: false } : task);
@@ -1059,7 +1059,7 @@ function reduceApp(state: AppState, action: Action): AppState {
: state.unreadDivider,
activeView: "chat",
selectedId: action.id,
- forkPanel: null,
+ forkPanel: action.keepPanel && state.forkPanel?.ownerId === action.id ? state.forkPanel : null,
},
action.id,
"switch",