Files
learnharness/components/settings-signal.ts
T
amosblomqvist 1f777f770c Zero-keys wave: provider-key gate renders honestly everywhere
Over Platform's 66dbea9 key-presence seam, the five owner-locked tasks:

1. Zero-keys pill — components/setup-cta.tsx: pure hasProviderKey
   (classifyKeys reuse — tool keys like Deepgram never count), useZeroKeys
   (GET /api/keys, re-read on the new lh:settings-saved 'keys' signal),
   ZeroKeysPill (testid setup-cta-keys) taking PRECEDENCE over the models
   CTA on chat/home/new-session. SettingsSection gains 'keys'; keys-section
   dispatches after save AND delete.

2. Setup stage 1 is self-contained: provider picker (MenuShell +
   ProviderMenuList) + key paste + Save via PUT /api/keys/:id — the dialog
   STAYS MOUNTED; its own save dispatches the keys signal, the fetch effect
   re-runs on it, and the stage flips to the pickers on its own.
   onOpenKeys + setup-open-keys die. SetupKeysBody exported for SSR pins.

3. parseProviderKeyRefusal (chat.tsx, envelope-verbatim twin of
   parseModelsRefusal) joins the revert-draft-and-open-setup interception;
   activity-dock reads 'needs a provider key'; dispatch-card shows
   'Add a key' — both deep-link setup, which lands on stage 1 by itself.

4. Dictation trio: the token route's only 503 (missing Deepgram key)
   becomes the client-authored DICTATION_KEYLESS short line; the errored
   idle click clears the error and deep-links Settings -> Keys ->
   DEEPGRAM_API_KEY via the new open(section, row) seam (dialog landOnRow
   -> SettingsDialogBody initialRow -> KeysSection openRow follow-effect).

Live-verified on :3001 under LH_IGNORE_ENV_KEYS=1 (the fresh-user switch):
pill on all three surfaces + precedence over the CTA, stage-1 save ->
auto-advance with dialog mounted, real 503 send refusal reverting the
draft and opening setup, dictation 503 -> short line -> Settings lands on
the unfolded Deepgram row with the error cleared, keys-section save and
delete both flipping the pill live, and a fabricated refused research job
rendering the 'Add a key' dispatch card. 926/926, tsc/lint/build green.
2026-09-24 23:20:59 +03:00

50 lines
2.3 KiB
TypeScript

"use client";
/**
* THE cross-surface settings signal (fix wave, owner ruling 2026-09-22):
* every WRITER dispatches after a successful PUT, every READER re-reads —
* the composer reflects the strong slot the instant setup saves, the
* settings section reflects the composer's pick on its next mount... and
* so on, by construction. CustomEvent on the window, the lh:chat-renamed
* pattern: writers and readers live in different component subtrees
* (portaled dialogs included), and the app already speaks this dialect.
*
* The event carries the section name so ONE event serves all sections —
* readers filter on what they actually read.
*/
import { useEffect, useRef } from "react";
export const SETTINGS_SAVED_EVENT = "lh:settings-saved";
/** The settings sections a writer can land (the write seam's union). */
// "keys" joined the roster (zero-keys wave, 2026-09-25): keys-section
// dispatches it after save AND delete — deletions used to be invisible to
// the zero-keys pill and the setup dialog's auto-advance.
export type SettingsSection = "general" | "keys" | "agent" | "models" | "appearance" | "graph";
/** Writers call this after the PUT resolves ok — never before (a failed
* write notifies nothing; the record is unchanged). */
export function notifySettingsSaved(section: SettingsSection): void {
window.dispatchEvent(
new CustomEvent(SETTINGS_SAVED_EVENT, { detail: { section } }),
);
}
/** Non-hook listener (imperative seams, tests). Returns an unsubscribe. */
export function onSettingsSaved(handler: (section: SettingsSection) => void): () => void {
const listener = (e: Event) => {
const section = (e as CustomEvent<{ section?: SettingsSection }>).detail?.section;
if (section !== undefined) handler(section);
};
window.addEventListener(SETTINGS_SAVED_EVENT, listener);
return () => window.removeEventListener(SETTINGS_SAVED_EVENT, listener);
}
/** Reader subscription: re-invokes whenever a writer lands, with the
* handler captured through a ref so callers never churn the
* subscription (and never stale-close over it either). */
export function useSettingsSaved(handler: (section: SettingsSection) => void): void {
const saved = useRef(handler);
saved.current = handler;
useEffect(() => onSettingsSaved((section) => saved.current(section)), []);
}