Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | 39x 1x 56x 108x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 13x 13x 13x 49x 3x 3x 2x 2x 3x 3x 49x 30x 13x 13x 13x 13x 13x 49x 28x 2x 2x 49x 34x 18x 7x 4x 3x 1x 1x 2x 7x 1x 18x 18x 49x 28x 49x 1x 11x | import { escapeTakenBelow } from "@/components/chat/ChatLayout/escape";
import { useBodyScrollLock } from "@/components/detail/Modal";
import { SectionErrorBoundary } from "@/components/ErrorBoundary";
import { useWorkspace } from "@/context/WorkspaceContext";
import { APP_ROUTES } from "@/routes/const";
import { asPersonalPath, type WorkspaceRef } from "@/routes/workspace";
import { ChevronDown, MessageCircle } from "lucide-react";
import React, {
lazy,
Suspense,
useEffect,
useId,
useMemo,
useRef,
useState,
} from "react";
import { matchPath, useLocation } from "react-router-dom";
/** The panel — the chat itself, the heaviest code in the app — is fetched
* on first open, never with the app: this file is in the main chunk. */
const loadPanel = () => import("./ChatDockPanel");
/** The pages that are a chat already: the dock stands aside on them. */
const CHAT_ROUTES = [
APP_ROUTES.USER.INFERENCE.AGENTS.chat,
APP_ROUTES.USER.INFERENCE.MODELS.chat,
];
/** True on an agent's or a model's chat page, in any workspace. */
export function onChatPage(workspace: WorkspaceRef, pathname: string): boolean {
const path = asPersonalPath(workspace, pathname);
return CHAT_ROUTES.some((pattern) => matchPath(pattern, path) !== null);
}
// Layers (frontend-components.md, "The chat dock"): docked at z-40 — under
// the page's dialogs (z-50), the header's controls and every Radix menu
// portalled to <body> — and lifted to z-[60] while a dialog the chat opened
// is up, so its backdrop covers the header too; full screen at z-[60], like
// a chat page's own. Never a transform on the panel (only opacity): the
// chat's dialogs are `fixed inset-0` inside it, and a transformed ancestor
// would shrink them to the panel's box.
//
// Bounds: the panel never runs under a control pinned to a corner. It keeps
// 0.75rem above the launcher (`bottom-5` + `size-12` = 4.25rem; the panel's
// bottom is 5rem) and the same below the header's row (`NavHeader`: `top-5`,
// 42px tall, so 3.875rem; the panel's top is at least 4.625rem). Its height
// is capped at the window less both, 9.625rem. With 7.5rem its top reached
// 2.5rem on a laptop, and the header covered its Full screen and Minimise.
const DOCKED =
"fixed z-40 has-[[aria-modal=true]]:z-[60] flex flex-col overflow-hidden rounded-2xl border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-950 shadow-2xl left-2 right-2 top-[4.5rem] bottom-2 sm:left-auto sm:top-auto sm:right-5 sm:bottom-20 sm:w-[32rem] sm:max-w-[calc(100vw-2.5rem)] sm:h-[44rem] sm:max-h-[calc(100dvh-9.625rem)]";
const FULLSCREEN = "fixed inset-0 z-[60] flex flex-col bg-gray-50 dark:bg-gray-950";
// Hidden, not unmounted: the draft, the thread's scroll and a streaming
// answer survive minimising. Not `display: none` either — the chat measures
// its own width to lay itself out (`ChatLayout`), and would read 0.
const SHOWN = "visible opacity-100 motion-safe:transition-[opacity,visibility] motion-safe:duration-150";
const HIDDEN = "invisible opacity-0 pointer-events-none motion-safe:transition-[opacity,visibility] motion-safe:duration-150";
const LAUNCHER =
"fixed bottom-4 right-4 sm:bottom-5 sm:right-5 z-40 size-12 p-0 border-0 rounded-full inline-flex items-center justify-center bg-primary text-primary-foreground hover:bg-primary/90 shadow-lg transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 dark:focus-visible:ring-offset-gray-900";
// On a phone the open panel covers the corner; its own Minimise closes it.
const LAUNCHER_OPEN = `${LAUNCHER} max-sm:hidden`;
/**
* The chat dock: a button in the bottom-right corner of every page of the
* signed-in app (`MyApp`'s `dock`), opening a chat panel above it — CHAINR
* Bot by default, any agent or model on request (`ChatDockPanel`), full
* screen when asked.
*
* The panel does not block the page: the page scrolls and takes clicks
* beside it, and Escape minimises it only when focus is in it. Minimised it
* stays mounted, so a turn runs on and its answer is there on reopening.
* One per workspace: a chat is governed and ledgered in the workspace it is
* held in, so moving to another starts the dock afresh.
*/
const ChatDock: React.FC = () => {
const workspace = useWorkspace();
return <Dock key={workspace.key} workspace={workspace} />;
};
const Dock: React.FC<{ workspace: WorkspaceRef }> = ({ workspace }) => {
const { pathname } = useLocation();
const [open, setOpen] = useState(false);
// Mounted on first open and kept: nothing is fetched before then.
const [mounted, setMounted] = useState(false);
const [fullscreen, setFullscreen] = useState(false);
// A fresh `lazy` per attempt, so Retry fetches a chunk that failed.
const [attempt, setAttempt] = useState(0);
const Panel = useMemo(() => lazy(loadPanel), [attempt]);
const panelRef = useRef<HTMLElement>(null);
const launcherRef = useRef<HTMLButtonElement>(null);
const focusOnShow = useRef(false);
const focusLauncher = useRef(false);
const panelId = useId();
const suppressed = onChatPage(workspace, pathname);
const shown = open && !suppressed;
useBodyScrollLock(shown && fullscreen);
// A link followed from the chat changes the page behind it: show it.
useEffect(() => setFullscreen(false), [pathname]);
const openDock = () => {
focusOnShow.current = true;
setMounted(true);
setOpen(true);
};
const closeDock = () => {
// Out of the panel before it hides: a hidden subtree must not hold focus.
const panel = panelRef.current;
if (panel?.contains(document.activeElement)) {
(document.activeElement as HTMLElement).blur();
focusLauncher.current = true;
}
setFullscreen(false);
setOpen(false);
};
// Opened by the person: into the chat, at its message box once it has
// one. A first open lands on the panel, and the chat takes it from there
// (`ChatPanel`'s `autoFocus`) when it has loaded.
useEffect(() => {
if (!shown || !focusOnShow.current) return;
focusOnShow.current = false;
const panel = panelRef.current;
Iif (!panel) return;
const composer = panel.querySelector<HTMLElement>(
'textarea[aria-label="Message"]',
);
(composer ?? panel).focus({ preventScroll: true });
}, [shown]);
// Minimised from inside: back to the button that opened it.
useEffect(() => {
if (open || !focusLauncher.current) return;
focusLauncher.current = false;
launcherRef.current?.focus({ preventScroll: true });
}, [open]);
// Escape, one step at a time: whatever is open over the chat first (it
// closes itself), then full screen, then the panel — the last only when
// the key was pressed in it, since the page beside it is the person's.
useEffect(() => {
if (!shown) return;
const onKey = (e: KeyboardEvent) => {
if (e.key !== "Escape" || e.isComposing) return;
if (escapeTakenBelow(e)) return;
if (fullscreen) {
setFullscreen(false);
return;
}
const from = e.target instanceof Node ? e.target : null;
if (panelRef.current?.contains(from) || launcherRef.current?.contains(from))
closeDock();
};
window.addEventListener("keydown", onKey, true);
return () => window.removeEventListener("keydown", onKey, true);
}, [shown, fullscreen]);
const preload = () => {
// Warmed while the pointer is on its way; a failure surfaces on open.
loadPanel().catch(() => undefined);
};
return (
<>
{mounted && (
<section
ref={panelRef}
id={panelId}
role="dialog"
aria-label="Chat"
aria-hidden={!shown || undefined}
tabIndex={-1}
data-testid="chat-dock"
data-state={shown ? "open" : "closed"}
data-fullscreen={fullscreen || undefined}
className={`${fullscreen ? FULLSCREEN : DOCKED} ${shown ? SHOWN : HIDDEN} outline-none`}
>
<SectionErrorBoundary
resetKey={attempt}
fallback={() => (
<PanelMessage onClose={closeDock}>
<p className="text-sm text-gray-700 dark:text-gray-300">
The chat could not be opened.
</p>
<button
type="button"
onClick={() => setAttempt((n) => n + 1)}
className="px-3 py-1.5 rounded-lg border-0 text-sm font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-90 transition-opacity"
>
Try again
</button>
</PanelMessage>
)}
>
<Suspense
fallback={
<PanelMessage onClose={closeDock}>
<div
aria-busy="true"
className="animate-pulse h-8 w-48 rounded bg-gray-200 dark:bg-gray-700"
/>
</PanelMessage>
}
>
<Panel
fullscreen={fullscreen}
onFullscreenChange={setFullscreen}
onClose={closeDock}
/>
</Suspense>
</SectionErrorBoundary>
</section>
)}
{!suppressed && (
<button
ref={launcherRef}
type="button"
aria-label="Chat"
aria-expanded={shown}
aria-controls={mounted ? panelId : undefined}
title={shown ? "Minimise chat" : "Chat"}
onClick={shown ? closeDock : openDock}
onPointerEnter={preload}
onFocus={preload}
className={shown ? LAUNCHER_OPEN : LAUNCHER}
>
{shown ? (
<ChevronDown className="size-6" aria-hidden="true" />
) : (
<MessageCircle className="size-6" aria-hidden="true" />
)}
</button>
)}
</>
);
};
/** The panel before it has loaded, or when it could not: still a panel,
* with a way to put it away. */
const PanelMessage: React.FC<{
onClose: () => void;
children: React.ReactNode;
}> = ({ onClose, children }) => (
<>
<div className="flex shrink-0 justify-end border-b border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 px-2 py-1.5">
<button
type="button"
onClick={onClose}
aria-label="Minimise chat"
className="inline-flex items-center justify-center size-8 p-0 rounded-md border-0 bg-transparent text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
>
<ChevronDown className="size-4" aria-hidden="true" />
</button>
</div>
<div className="flex flex-1 flex-col items-center justify-center gap-3 p-6">
{children}
</div>
</>
);
export default ChatDock;
|