Skip to content

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

2.0 KB
Raw Blame
import { useSyncExternalStore } from "react";
import type { Action } from "@/state/store";
import type { KeyId } from "./key-catalog";

/** A named open of the keys page: `row` is where it lands, the key's own row or a row in another section that uses it.
 * Every request is a new object, so following the same link again lands there again. */
export interface KeyFocusRequest { id?: KeyId; row?: KeyId; nonce: number }

const OPEN_KEYS = { type: "toggleAppSettings", open: true, section: "connections" } as const satisfies Action;

let latest: KeyFocusRequest | null = null;
let nonce = 0;
const watchers = new Set<() => void>();

function publish(next: KeyFocusRequest | null) {
  latest = next;
  for (const watch of Array.from(watchers)) watch();
}

/** The action that opens Settings on the keys page, at the row for `id` (or, with `purpose`, the row of that name that
 * uses the key): its section opens, the row scrolls into view, its field takes focus and the row lights up briefly.
 * With no key it opens the page at its first section. */
export function openKeySettings(id?: KeyId, purpose?: KeyId): typeof OPEN_KEYS {
  requestKeyFocus(id, purpose);
  return OPEN_KEYS;
}

/** Asks the keys page for a row without opening Settings: for a caller that navigates to AI itself, with {@link KEYS_ANCHOR_FOR_FOCUS}. */
export function requestKeyFocus(id?: KeyId, purpose?: KeyId): void {
  nonce += 1;
  publish(id ? { id, row: purpose ?? id, nonce } : { nonce });
}

/** The AI page anchor that opens Other services for a requested row without scrolling to its top block or listing AI providers first. */
export const KEYS_ANCHOR_FOR_FOCUS = "keys-focus";

export function finishKeyFocus(request: KeyFocusRequest) {
  if (latest?.nonce === request.nonce) publish(null);
}

function subscribe(watch: () => void) {
  watchers.add(watch);
  return () => { watchers.delete(watch); };
}

const snapshot = () => latest;

export function useKeyFocus(): KeyFocusRequest | null {
  return useSyncExternalStore(subscribe, snapshot, snapshot);
}