Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commandPaletteBus.ts

46 lines1.7 KB

1import type { EnvironmentId, PullRequestLinkedThreadsResult } from "@t3tools/contracts";
2​
3import type { DraftId } from "./composerDraftStore";
4import { dispatchCommandPalette, isCommandPaletteMounted } from "./commandPaletteStore";
5​
6export interface CommandPaletteLinkedThreads {
7 readonly environmentId: EnvironmentId;
8 readonly threads: PullRequestLinkedThreadsResult["threads"];
9}
10​
11export interface CommandPaletteOpenDetail {
12 readonly open?: "add-project" | "new-thread-in";
13 /** With add-project: the draft the new project is picked for. */
14 readonly draftId?: DraftId | null;
15 readonly query?: string;
16 readonly linkedThreads?: CommandPaletteLinkedThreads;
18​
19export function openCommandPalette(detail: CommandPaletteOpenDetail = {}): void {
20 // With no palette on screen (e.g. /pair) the request is dropped; kept in the
21 // store, it would pop a stale palette once the app shell mounts.
22 if (!isCommandPaletteMounted()) return;
23 if (detail.open === "new-thread-in") {
24 dispatchCommandPalette({ _tag: "OpenNewThreadIn" });
25 } else if (detail.open === "add-project") {
26 dispatchCommandPalette({
27 _tag: "OpenAddProject",
28 ...(detail.draftId ? { draftId: detail.draftId } : {}),
29 });
30 } else if (detail.query !== undefined) {
31 dispatchCommandPalette({
32 _tag: "OpenSearch",
33 query: detail.query,
34 ...(detail.linkedThreads ? { linkedThreads: detail.linkedThreads } : {}),
35 });
36 } else {
37 dispatchCommandPalette({ _tag: "SetOpen", open: true });
38 }
40​
41/** True while the palette popup is on screen, including its closing animation. */
42export function isCommandPaletteOpen(): boolean {
43 return (
44 typeof document !== "undefined" && document.querySelector("[data-command-palette]") !== null
45 );