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 | 12x 12x 12x 12x 12x 240x 240x 240x 240x 240x 240x 240x 240x 240x 75x 8x 6x 6x 3x 8x 8x 240x 240x 240x 240x 49x 191x 10x | import { escapeTakenBelow } from "@/components/chat/ChatLayout/escape";
import { useBodyScrollLock } from "@/components/detail";
import { useDocumentTop } from "@/hooks/useDocumentTop";
import { useElementWidth } from "@/hooks/useElementWidth";
import { Maximize2, Minimize2 } from "lucide-react";
import React, { useEffect, useState } from "react";
/** Below this much room the conversation list goes above the chat instead
* of beside it: side by side, the chat keeps at least 512px (768 less the
* 240px list and the gap). */
export const STACK_BELOW_PX = 768;
/** What the app keeps below every page (`MyApp`'s `pb-15`). The chat runs
* down to it rather than past it, so the page does not scroll by that much. */
const PAGE_BOTTOM_PADDING = "3.75rem";
/** The shortest the chat gets on the page: below it, the page scrolls
* rather than squeezing the thread. About what the chat was before it
* ran to the bottom of the window (a 360px thread and the message box). */
const PAGE_MIN_HEIGHT = "30rem";
/** Where the chat sits: on a page of its own, or in a panel that gives it
* a box of definite size — the chat dock (`ChatDock`), which owns the
* panel's position, its header and its full screen. */
export type ChatFrame = "page" | "panel";
const ROW_CLASSES = {
/** Beside each other, filling a frame of definite height. */
beside: "flex flex-1 min-h-0 gap-4",
/** The list as a bar above the chat, each its own height. */
stacked: "flex flex-col gap-3",
/** The bar above the chat, in full screen. */
stackedFill: "flex flex-1 min-h-0 flex-col gap-3",
} as const;
// ─── Props ────────────────────────────────────────────────────────────────────
export interface ChatLayoutProps {
/** Heads the full-screen view, where the page's own title is covered. */
title: React.ReactNode;
/** Drawn beside the title in full screen — the page's title icon. */
icon?: React.ReactNode;
/** The conversation list. `fill` is true when the frame has a height of
* its own — full screen, or beside the chat on the page — so the list
* should run that height rather than cap itself; `stacked` when there is
* too little room for it beside the chat, so it sits above as a bar
* (`ConversationSidebar`'s `stacked`). */
sidebar: (fill: boolean, stacked: boolean) => React.ReactNode;
/** The chat column's contents. `fill` as for `sidebar`: they should grow
* to the column's height rather than cap the thread. */
children: (fill: boolean) => React.ReactNode;
/** `"panel"`: fill the parent and nothing more — no heading, no full
* screen toggle, no Escape or scroll lock, which are the panel's. The
* list still goes above the chat where the panel is narrow. */
frame?: ChatFrame;
}
// ─── Component ────────────────────────────────────────────────────────────────
/**
* The frame every chat page shares: the conversation list beside the chat,
* and a way to give both the whole window.
*
* Where there is not room for both side by side — a phone, a tablet, a
* laptop with the app's navigation open — the list goes above the chat
* (`STACK_BELOW_PX`, measured on the frame itself, not the window, since
* the room left depends on what else is showing). Below that the list used
* to keep its 240px and leave the chat a sliver: a 26px message box at
* 768px, and a page scrolling sideways at 390px.
*
* Beside each other on the page, the frame runs from where it starts down
* to the bottom of the window (`useDocumentTop`, less the app's bottom
* padding), and the list and the chat both fill it, so they end level.
* Each used to cap itself at 60vh, the chat from a 360px floor, so a short
* conversation stopped well above a long list. Stacked, each keeps its own
* height: the list is a bar, and on a phone the page should scroll.
*
* Full screen is for reading what a chat produces — a chart, a long report,
* a wide table — which the app's navigation and the page header otherwise
* squeeze. The sidebar comes along and stays collapsible.
*
* Toggling swaps the container's classes and nothing else, so the subtree is
* never remounted: ChatPanel keeps the draft, attachments and settings in
* its own state, and the sidebar its collapsed state and search. Moving the
* chat into a portal would drop all of them on every toggle.
*
* Escape leaves full screen, except while a dialog, menu, popover or the
* command palette is open over it (`escapeTakenBelow`) — each closes on
* Escape itself, and one key should take one step back.
* The key is read in the capture phase, before any dialog's own listener:
* a real key press runs microtasks between listeners, so by the bubble
* phase React may already have removed the dialog it closed, and the check
* would find nothing open. The page behind stops scrolling for as long as
* the chat covers it.
*
* In a panel (`frame="panel"`) the frame fills the box it is given and the
* panel owns the rest: its own header, full screen, Escape and scroll lock.
* The box always has a definite height, so the list and the chat always
* fill it, side by side or the list above as a bar.
*/
const ChatLayout: React.FC<ChatLayoutProps> = ({
title,
icon,
sidebar,
children,
frame = "page",
}) => {
const panel = frame === "panel";
const [fullscreen, setFullscreen] = useState(false);
// The row's own width decides the arrangement; 0 is unmeasured (jsdom),
// which keeps the list beside the chat.
const [rowRef, rowWidth] = useElementWidth<HTMLDivElement>();
const stacked = rowWidth > 0 && rowWidth < STACK_BELOW_PX;
// Where the frame starts, for running it to the bottom of the window.
const [frameRef, frameTop] = useDocumentTop<HTMLDivElement>();
// The frame has a height of its own: the window, or the panel's box.
const definite = fullscreen || panel;
const fill = definite || !stacked;
useBodyScrollLock(fullscreen);
useEffect(() => {
if (!fullscreen) return;
const onKey = (e: KeyboardEvent) => {
Iif (e.key !== "Escape" || e.isComposing) return;
if (escapeTakenBelow(e)) return;
setFullscreen(false);
};
window.addEventListener("keydown", onKey, true);
return () => window.removeEventListener("keydown", onKey, true);
}, [fullscreen]);
const label = fullscreen ? "Exit full screen" : "Full screen";
const ToggleIcon = fullscreen ? Minimize2 : Maximize2;
const row = (
<div
ref={rowRef}
data-stacked={stacked || undefined}
className={
!stacked
? ROW_CLASSES.beside
: definite
? ROW_CLASSES.stackedFill
: ROW_CLASSES.stacked
}
>
{sidebar(fill, stacked)}
<div
className={
fill
? // Scrolls only when the frame is shorter than the chat's
// own minimum, so nothing is ever cut off.
"flex flex-1 min-w-0 min-h-0 flex-col overflow-y-auto"
: "flex-1 min-w-0"
}
>
{children(fill)}
</div>
</div>
);
if (panel)
return (
<div
data-testid="chat-layout"
data-frame="panel"
className="flex flex-1 min-h-0 flex-col"
>
{row}
</div>
);
return (
<div
ref={frameRef}
data-testid="chat-layout"
data-fullscreen={fullscreen || undefined}
className={
fullscreen
? // Above the app's fixed navigation (z-40) and account controls
// (z-50). Dialogs the chat opens render inside this container,
// so they still stack over it.
"fixed inset-0 z-[60] flex flex-col gap-3 p-4 bg-gray-50 dark:bg-gray-950"
: "flex flex-col gap-2"
}
style={
fill && !fullscreen
? {
height: `max(${PAGE_MIN_HEIGHT}, calc(100dvh - ${frameTop}px - ${PAGE_BOTTOM_PADDING}))`,
}
: undefined
}
>
<div className="flex items-center gap-3 min-w-0">
{fullscreen && (
<>
{icon}
{/* h3, like the detail kit's titles: index.css sizes every h2
at 40px, unlayered, so no utility class can shrink one. */}
<h3 className="min-w-0 truncate text-base font-semibold text-gray-900 dark:text-gray-100">
{title}
</h3>
</>
)}
<button
type="button"
onClick={() => setFullscreen((on) => !on)}
title={fullscreen ? `${label} (Esc)` : label}
className="ml-auto shrink-0 inline-flex items-center gap-1.5 px-2 py-1 rounded-md border-0 text-xs font-medium 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"
>
<ToggleIcon className="size-3.5" aria-hidden="true" />
{label}
</button>
</div>
{row}
</div>
);
};
export default ChatLayout;
|