Skip to content

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

Add a light/dark command to the command palette

luishron1232c5daf51b5cf
diff --git a/src/components/CommandPalette.theme.dom.test.ts b/src/components/CommandPalette.theme.dom.test.ts
new file mode 100644
index 0000000000..2297a3ce98
--- /dev/null
+++ b/src/components/CommandPalette.theme.dom.test.ts
@@ -0,0 +1,101 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import type { Bot } from "@/state/store";
+
+vi.mock("@/state/store", async (importOriginal) => {
+ const original = await importOriginal<typeof import("@/state/store")>();
+ return {
+ ...original,
+ api: vi.fn(async () => ({ hits: [] })),
+ useStore: () => ({ state: { ...original.initialState, bots: [bot], groups: [] }, dispatch: vi.fn() }),
+ };
+});
+
+import { CommandPalette, openCommandPalette } from "./CommandPalette";
+
+const bot: Bot = {
+ id: "theme-bot", threadId: "theme-thread", name: "Juniper", title: "", description: "",
+ color: "blue", notifications: false, unread: false, busy: false, messages: [], section: "",
+ modelSelection: { instanceId: "claude", model: "fake" },
+};
+
+// The real skins set --code-color-scheme per skin; this stands in for the two the command moves between and one more.
+const skinModes = document.createElement("style");
+skinModes.textContent = ':root[data-skin="daylight"], :root[data-skin="linen"] { --code-color-scheme: light; } :root[data-skin="midnight"] { --code-color-scheme: dark; }';
+document.head.append(skinModes);
+
+const values = new Map<string, string>();
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ values.clear();
+ vi.stubGlobal("localStorage", {
+ getItem: (key: string) => values.get(key) ?? null,
+ setItem: (key: string, value: string) => { values.set(key, value); },
+ removeItem: (key: string) => { values.delete(key); },
+ });
+ vi.stubGlobal("matchMedia", () => ({ matches: true, addEventListener: () => {}, removeEventListener: () => {} }));
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ vi.unstubAllGlobals();
+ delete document.documentElement.dataset.skin;
+});
+
+function open(skin: string) {
+ document.documentElement.dataset.skin = skin;
+ flushSync(() => root.render(createElement(CommandPalette)));
+ flushSync(() => openCommandPalette());
+ return document.querySelector<HTMLInputElement>('[role="dialog"] input')!;
+}
+
+function type(input: HTMLInputElement, value: string) {
+ const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
+ flushSync(() => {
+ setter.call(input, value);
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+}
+
+const command = () => [...document.querySelectorAll<HTMLButtonElement>('[role="dialog"] button')].find((button) => button.textContent === "Toggle light/dark");
+const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
+
+it("lists the command under Commands and wears Daylight from Midnight, saving the choice Settings reads", async () => {
+ open("midnight");
+ expect(document.querySelector('[role="dialog"]')!.textContent).toContain("Commands");
+ flushSync(() => command()!.click());
+ await settle();
+ expect(document.documentElement.dataset.skin).toBe("daylight");
+ expect(values.get("omb-skin")).toBe("daylight");
+ expect(document.querySelector('[role="dialog"]')).toBeNull();
+});
+
+it("finds it by typing and takes another light skin to Midnight with Enter", async () => {
+ const input = open("linen");
+ type(input, "dark");
+ expect(command()).toBeDefined();
+ flushSync(() => input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true })));
+ await settle();
+ expect(values.get("omb-skin")).toBe("midnight");
+});
+
+it("pins Midnight or Daylight when the theme followed the system, as Appearance does", async () => {
+ values.set("omb-skin", "system");
+ open("midnight");
+ flushSync(() => command()!.click());
+ await settle();
+ expect(values.get("omb-skin")).toBe("daylight");
+});
+
+it("leaves the command out when the search does not name it", () => {
+ const input = open("midnight");
+ type(input, "juniper");
+ expect(command()).toBeUndefined();
+});
diff --git a/src/components/CommandPalette.tsx b/src/components/CommandPalette.tsx
index 76bb4f33c7..c39a7a6b07 100644
--- a/src/components/CommandPalette.tsx
+++ b/src/components/CommandPalette.tsx
@@ -1,6 +1,6 @@
import { t } from "@/lib/i18n";
import { memo, useEffect, useRef, useState } from "react";
-import { MessageSquare, Search, Users } from "lucide-react";
+import { MessageSquare, Search, SunMoon, Users } from "lucide-react";
import { BotAvatar } from "./Avatar";
import { api, openBot } from "@/state/store";
import { useBotsApartFromTurns, useGroupsApartFromTurns, type BotApartFromTurns, type GroupApartFromTurns } from "@/state/apart-from-turns";
@@ -12,10 +12,13 @@ import { cn } from "@/lib/cn";
import type { SearchHit } from "@/lib/search-hit";
import { landOnSearchHit } from "@/lib/focus-message";
import { imeComposing } from "@/lib/send-key";
+import { readColorScheme } from "@/lib/color-scheme";
+import { applySkinChoice, THEME_SKINS } from "@/lib/skins";
type PaletteEntry =
| { kind: "bot"; bot: BotApartFromTurns }
| { kind: "room"; group: GroupApartFromTurns }
+ | { kind: "theme" }
| { kind: "message"; hit: SearchHit };
const OPEN_EVENT = "omb:open-command-palette";
@@ -91,9 +94,11 @@ export const CommandPalette = memo(function CommandPalette({ onOpenChange }: { o
const bots = rankByName(allBots.filter((b) => !b.hidden), q);
const rooms = rankByName(groups, q);
+ const themeCommand = t("palette.toggleTheme").toLowerCase().includes(q);
const entries: PaletteEntry[] = [
...bots.map((bot): PaletteEntry => ({ kind: "bot", bot })),
...rooms.map((group): PaletteEntry => ({ kind: "room", group })),
+ ...(themeCommand ? [{ kind: "theme" } as const] : []),
...(q ? messageHits.map((hit): PaletteEntry => ({ kind: "message", hit })) : []),
];
const selected = entries.length ? Math.min(cursor, entries.length - 1) : 0;
@@ -106,6 +111,8 @@ export const CommandPalette = memo(function CommandPalette({ onOpenChange }: { o
} catch (error) {
dispatch({ type: "error", message: error instanceof Error ? error.message : String(error) });
}
+ } else if (entry.kind === "theme") {
+ applySkinChoice(readColorScheme() === "dark" ? THEME_SKINS.light : THEME_SKINS.dark);
} else if (entry.kind === "bot") {
const bot = store.state().bots.find((candidate) => candidate.id === entry.bot.id);
if (bot) openBot(dispatch, bot, showThreads);
@@ -135,7 +142,8 @@ export const CommandPalette = memo(function CommandPalette({ onOpenChange }: { o
};
const roomOffset = bots.length;
- const messageOffset = bots.length + rooms.length;
+ const themeOffset = bots.length + rooms.length;
+ const messageOffset = themeOffset + (themeCommand ? 1 : 0);
const row = (key: string, index: number, onPick: () => void, children: React.ReactNode, twoLine = false) => (
<button
@@ -221,6 +229,21 @@ export const CommandPalette = memo(function CommandPalette({ onOpenChange }: { o
</>,
),
)}
+ {themeCommand && (
+ <div className="px-3 pb-1 pt-2 text-[12px] font-medium text-ink-tertiary">
+ {t("palette.commands")}
+ </div>
+ )}
+ {themeCommand &&
+ row(
+ "command:theme",
+ themeOffset,
+ () => void activate({ kind: "theme" }),
+ <>
+ <SunMoon size={16} className="mx-[3px] shrink-0 text-ink-secondary" />
+ <span className="truncate text-[14px] text-ink">{t("palette.toggleTheme")}</span>
+ </>,
+ )}
{q && messageHits.length > 0 && (
<div className="px-3 pb-1 pt-2 text-[12px] font-medium text-ink-tertiary">
{t("palette.messages")}
diff --git a/src/locales/en.json b/src/locales/en.json
index 11afc7fd0a..c6b7225c01 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -9009,6 +9009,8 @@
"palette.bots": "Bots",
"palette.groups": "Groups",
"palette.messages": "Messages",
+ "palette.commands": "Commands",
+ "palette.toggleTheme": "Toggle light/dark",
"remote.status.pairedOne": "1 device paired",
"remote.status.pairedMany": "{count} devices paired",
"companyBackup.restorePending": "An earlier restore needs recovery first.",
diff --git a/src/locales/it.json b/src/locales/it.json
index 1a79e164dd..1674148a11 100644
--- a/src/locales/it.json
+++ b/src/locales/it.json
@@ -8947,6 +8947,8 @@
"palette.bots": "Bots",
"palette.groups": "Gruppi",
"palette.messages": "Messaggi",
+ "palette.commands": "Comandi",
+ "palette.toggleTheme": "Alterna chiaro/scuro",
"remote.status.pairedOne": "1 dispositivo abbinato",
"remote.status.pairedMany": "{count} dispositivi abbinati",
"companyBackup.restorePending": "Un precedente ripristino ha bisogno di recupero prima.",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 3640b9601e..7cda927c2a 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -8941,6 +8941,8 @@
"palette.bots": "Bots",
"palette.groups": "Grupos",
"palette.messages": "Mensagens",
+ "palette.commands": "Comandos",
+ "palette.toggleTheme": "Alternar claro/escuro",
"remote.status.pairedOne": "1 dispositivo pareado",
"remote.status.pairedMany": "{count} dispositivos pareados",
"companyBackup.restorePending": "Uma restauração anterior precisa ser recuperada primeiro.",
diff --git a/src/locales/zh-tw.json b/src/locales/zh-tw.json
index 52e0c2d9b5..a062c34c36 100644
--- a/src/locales/zh-tw.json
+++ b/src/locales/zh-tw.json
@@ -8067,6 +8067,8 @@
"palette.bots": "機器人",
"palette.groups": "群組",
"palette.messages": "訊息",
+ "palette.commands": "指令",
+ "palette.toggleTheme": "切換淺色/深色",
"remote.status.pairedOne": "已配對 1 部裝置",
"remote.status.pairedMany": "已配對 {count} 部裝置",
"companyBackup.restorePending": "先前的還原需要先完成復原。",
diff --git a/src/locales/zh.json b/src/locales/zh.json
index 6d4415bd1a..d759232641 100644
--- a/src/locales/zh.json
+++ b/src/locales/zh.json
@@ -9957,6 +9957,8 @@
"palette.bots": "机器人",
"palette.groups": "群组",
"palette.messages": "消息",
+ "palette.commands": "命令",
+ "palette.toggleTheme": "切换浅色/深色",
"settings.profile.awayEmails": "我不在时给我发邮件",
"settings.section.vault": "保险库",
"settings.simple.group.typing": "语言和输入",