Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
974 changes: 485 additions & 489 deletions apps/web/src/components/ChatView.tsx

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions apps/web/src/components/ChatWorkspace.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import type { ScopedThreadRef } from "@t3tools/contracts";
import type { PanelAnimationDurationMs } from "@t3tools/contracts/settings";
import {
createContext,
type ReactNode,
type RefObject,
use,
useEffect,
useLayoutEffect,
useState,
useMemo,
} from "react";

import { useDiffPanelStore } from "~/diffPanelStore";
import {
type PreviewPanelInlineSize,
usePreviewPanelInlineSize,
} from "~/hooks/usePreviewPanelInlineSize";
import { cn } from "~/lib/utils";
import { usePanelPresence } from "~/panelAnimations";
import {
type RightPanelSurface,
selectActiveRightPanelSurface,
selectThreadRightPanelState,
useRightPanelStore,
} from "~/rightPanelStore";

export interface RightPanelView {
readonly open: boolean;
readonly present: boolean;
readonly maximized: boolean;
readonly ownsTitleBar: boolean;
readonly activeSurface: RightPanelSurface | null;
readonly renderedSurface: RightPanelSurface | null;
readonly renderedSurfaces: readonly RightPanelSurface[];
}

const RightPanelViewContext = createContext<RightPanelView | null>(null);
const RightPanelInlineSizeContext = createContext<PreviewPanelInlineSize | null>(null);

const EMPTY_SURFACES: readonly RightPanelSurface[] = [];

export function ChatWorkspace(props: {
readonly threadRef: ScopedThreadRef | null;
readonly threadKey: string | null;
readonly sheet: boolean;
readonly maximizeRequestThreadRef: ScopedThreadRef;
readonly animationsActive: boolean;
readonly animationDurationMs: PanelAnimationDurationMs;
readonly explicitDiffOpenRef: RefObject<ScopedThreadRef | null>;
readonly children: ReactNode;
}) {
const { threadRef, threadKey, sheet, explicitDiffOpenRef } = props;
const [row, setRow] = useState<HTMLDivElement | null>(null);
const inlineSize = usePreviewPanelInlineSize(undefined, {
container: row,
widthStorageKey: `t3code:preview-panel-width:${threadKey}`,
});
const open = useRightPanelStore(
(state) => selectThreadRightPanelState(state.byThreadKey, threadRef).isOpen,
);
const surfaces = useRightPanelStore(
(state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
);
const activeSurface = useRightPanelStore((state) =>
selectActiveRightPanelSurface(state.byThreadKey, threadRef),
);
const diffOpen = activeSurface?.kind === "diff";
useLayoutEffect(() => {
const explicitThreadRef = explicitDiffOpenRef.current;
explicitDiffOpenRef.current = null;
// Generic openings always show Changes, including tab fallbacks and thread changes.
// A timeline click instead opens the specific turn/file the user requested.
if (diffOpen && threadRef && explicitThreadRef !== threadRef) {
useDiffPanelStore.getState().selectGitScope(threadRef, "branch");
}
}, [diffOpen, explicitDiffOpenRef, threadRef]);
const presenceValue = useMemo(() => ({ activeSurface, surfaces }), [activeSurface, surfaces]);
const presence = usePanelPresence(
open && threadRef !== null,
presenceValue,
props.animationsActive,
threadKey,
props.animationDurationMs,
);
const inline = open && !sheet;
const maximized = useRightPanelStore(
(state) =>
inline && selectThreadRightPanelState(state.byThreadKey, threadRef).maximized === true,
);
useEffect(() => {
if (!inline) return;
if (useRightPanelStore.getState().consumeMaximizeRequest(props.maximizeRequestThreadRef)) {
useRightPanelStore.getState().setMaximized(props.maximizeRequestThreadRef, true);
}
}, [inline, props.maximizeRequestThreadRef]);
const renderedSurface = presence.value?.activeSurface ?? null;
const renderedSurfaces = presence.value?.surfaces ?? EMPTY_SURFACES;
const view = useMemo<RightPanelView>(
() => ({
open,
present: presence.present,
maximized,
ownsTitleBar: inline,
activeSurface,
renderedSurface,
renderedSurfaces,
}),
[activeSurface, inline, maximized, open, presence.present, renderedSurface, renderedSurfaces],
);

return (
<div
ref={setRow}
className="relative flex min-h-0 min-w-0 flex-1 overflow-hidden bg-background"
>
<RightPanelViewContext value={view}>
<RightPanelInlineSizeContext value={inlineSize}>
{props.children}
</RightPanelInlineSizeContext>
</RightPanelViewContext>
</div>
);
}

function useRightPanelView(): RightPanelView {
const view = use(RightPanelViewContext);
if (!view) throw new Error("Right panel slots must render inside ChatWorkspace.");
return view;
}

export function RightPanelViewSlot(props: {
readonly children: (view: RightPanelView) => ReactNode;
}) {
const view = useRightPanelView();
return props.children(view);
}

export function RightPanelSlot(props: {
readonly children: (view: RightPanelView, inlineSize: PreviewPanelInlineSize) => ReactNode;
}) {
const view = useRightPanelView();
const inlineSize = use(RightPanelInlineSizeContext);
if (!inlineSize) throw new Error("Right panel slots must render inside ChatWorkspace.");
return props.children(view, inlineSize);
}

export function ChatWorkspaceColumn(props: {
readonly renderHeader: (view: RightPanelView) => ReactNode;
readonly children: ReactNode;
}) {
const view = useRightPanelView();
return (
<div
className={cn(
// Clipping rather than scrolling leaves the workspace row as the scrollport the header's
// sticky card toggle measures against.
"flex min-h-0 min-w-0 flex-col overflow-x-clip",
view.maximized ? "w-0 flex-none" : "flex-1",
)}
data-chat-column-maximized-away={view.maximized ? "true" : "false"}
>
{props.renderHeader(view)}
{props.children}
</div>
);
}
8 changes: 8 additions & 0 deletions apps/web/src/components/RightPanelTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,14 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
{...(props.widthStorageKey !== undefined ? { widthStorageKey: props.widthStorageKey } : {})}
{...(props.defaultWidth !== undefined ? { defaultWidth: props.defaultWidth } : {})}
{...(props.inlineSize ? { inlineSize: props.inlineSize } : {})}
corner={
props.mode === "inline" && !props.layoutControls ? (
// The workspace's fixed titlebar controls float over this corner. Filling it keeps
// tabs sliding in with the panel from showing beneath them, and the panel edge
// reveals these controls rather than passing them over the chat header.
<div className="h-full w-[calc(var(--workspace-controls-cluster-inset)+var(--workspace-titlebar-control-size)+0.25rem)] bg-background" />
) : undefined
}
>
<div
className={cn(
Expand Down
90 changes: 51 additions & 39 deletions apps/web/src/components/chat/PanelLayoutControls.tsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,100 @@
import type { ScopedThreadRef } from "@t3tools/contracts";
import { PanelBottomIcon, PanelRightIcon, SquareMenuIcon } from "lucide-react";
import { Maximize2, Minimize2 } from "lucide";
import { MorphIcon } from "~/components/MorphIcon";
import { memo, type ReactElement } from "react";
import { memo, type ReactElement, type ReactNode } from "react";

import type { ThreadPanelPresentation } from "../../rightPanelLayout";
import { selectThreadPanelOpen, useRightPanelStore } from "../../rightPanelStore";
import { PopoverCreateHandle, PopoverTrigger } from "../ui/popover";
import { Toggle } from "../ui/toggle";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import {
type ThreadPanelPresentationStore,
useThreadPanelPresentation,
} from "./threadPanelPresentation";

export interface PanelLayoutControlsProps {
showThreadPanelControl?: boolean;
/** Rendered first, before the terminal toggle. */
threadPanelControl?: ReactNode;
showTerminalControl?: boolean;
showRightPanelControl?: boolean;
terminalAvailable: boolean;
terminalOpen: boolean;
terminalShortcutLabel: string | null;
threadPanelOpen: boolean;
threadPanelPresentation: ThreadPanelPresentation;
threadPanelPopoverHandle?: ReturnType<typeof PopoverCreateHandle>;
threadPanelShortcutLabel: string | null;
rightPanelAvailable: boolean;
rightPanelOpen: boolean;
rightPanelShortcutLabel: string | null;
rightPanelUnavailableLabel?: string;
onToggleTerminal: () => void;
onToggleThreadPanel: () => void;
onToggleRightPanel: () => void;
}

export const PanelLayoutControls = memo(function PanelLayoutControls({
showThreadPanelControl = true,
showTerminalControl = true,
showRightPanelControl = true,
terminalAvailable,
terminalOpen,
terminalShortcutLabel,
threadPanelOpen,
threadPanelPresentation,
threadPanelPopoverHandle,
threadPanelShortcutLabel,
rightPanelAvailable,
rightPanelOpen,
rightPanelShortcutLabel,
rightPanelUnavailableLabel = "Right panel is unavailable",
onToggleTerminal,
onToggleThreadPanel,
onToggleRightPanel,
}: PanelLayoutControlsProps) {
const threadPanelToggle = (
/** Toggles the workspace card, as a popover trigger whenever the card cannot dock. */
export const ThreadPanelToggle = memo(function ThreadPanelToggle({
threadRef,
presentation: presentationStore,
popoverHandle,
shortcutLabel,
onToggle,
}: {
threadRef: ScopedThreadRef | null;
presentation: ThreadPanelPresentationStore;
popoverHandle: ReturnType<typeof PopoverCreateHandle>;
shortcutLabel: string | null;
onToggle: () => void;
}) {
const presentation = useThreadPanelPresentation(presentationStore);
const open = useRightPanelStore((state) =>
selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, presentation),
);
const toggle = (
<Toggle
className="relative shrink-0 [-webkit-app-region:no-drag]"
pressed={threadPanelOpen}
pressed={open}
aria-label="Toggle thread details panel"
variant="ghost"
size="sm"
>
<SquareMenuIcon className="size-4" />
</Toggle>
);
const threadPanelTooltip = (trigger: ReactElement) => (
const tooltip = (trigger: ReactElement) => (
<Tooltip>
<TooltipTrigger
render={trigger}
{...(threadPanelPresentation === "popover" ? {} : { onClick: onToggleThreadPanel })}
{...(presentation === "popover" ? {} : { onClick: onToggle })}
/>
<TooltipPopup side="bottom">
Toggle thread details
{threadPanelShortcutLabel ? ` (${threadPanelShortcutLabel})` : ""}
{shortcutLabel ? ` (${shortcutLabel})` : ""}
</TooltipPopup>
</Tooltip>
);
return presentation === "popover"
? tooltip(<PopoverTrigger handle={popoverHandle} render={toggle} />)
: tooltip(toggle);
});

export const PanelLayoutControls = memo(function PanelLayoutControls({
threadPanelControl,
showTerminalControl = true,
showRightPanelControl = true,
terminalAvailable,
terminalOpen,
terminalShortcutLabel,
rightPanelAvailable,
rightPanelOpen,
rightPanelShortcutLabel,
rightPanelUnavailableLabel = "Right panel is unavailable",
onToggleTerminal,
onToggleRightPanel,
}: PanelLayoutControlsProps) {
return (
<div
className="flex h-full shrink-0 items-center gap-1 [-webkit-app-region:no-drag]"
data-panel-layout-controls
>
{showThreadPanelControl
? threadPanelPresentation === "popover"
? threadPanelTooltip(
<PopoverTrigger handle={threadPanelPopoverHandle} render={threadPanelToggle} />,
)
: threadPanelTooltip(threadPanelToggle)
: null}
{threadPanelControl}
{showTerminalControl ? (
<Tooltip>
<TooltipTrigger render={<span className="flex shrink-0" />}>
Expand Down
35 changes: 35 additions & 0 deletions apps/web/src/components/chat/threadPanelPresentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useSyncExternalStore } from "react";

import type { ThreadPanelPresentation } from "../../rightPanelLayout";

/**
* Whether the workspace card docks inline or opens as a popover. The card decides from the
* canvas it sits in and the header toggle reads it, so a change re-renders only those two
* rather than the whole chat view.
*/
export interface ThreadPanelPresentationStore {
readonly get: () => ThreadPanelPresentation;
readonly set: (presentation: ThreadPanelPresentation) => void;
readonly subscribe: (listener: () => void) => () => void;
}

export function createThreadPanelPresentationStore(): ThreadPanelPresentationStore {
let presentation: ThreadPanelPresentation = "inline";
const listeners = new Set<() => void>();
return {
get: () => presentation,
set: (next) => {
if (next === presentation) return;
presentation = next;
for (const listener of listeners) listener();
},
subscribe: (listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
};
}

export function useThreadPanelPresentation(store: ThreadPanelPresentationStore) {
return useSyncExternalStore(store.subscribe, store.get);
}
12 changes: 11 additions & 1 deletion apps/web/src/components/preview/PreviewPanelShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface PreviewPanelShellProps {
widthStorageKey?: string;
/** Overrides the initial width (px) before the user has resized the panel. */
defaultWidth?: number;
/** Pinned to the panel's top right, so it stays put while the content slides with the edge. */
corner?: ReactNode;
children: ReactNode;
}

Expand Down Expand Up @@ -145,7 +147,7 @@ function PreviewPanelShellFrame(
data-preview-panel-maximized={maximized ? "true" : "false"}
>
{isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
<div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
<div className={cn("relative h-full min-h-0 w-full", collapsible && "overflow-clip")}>
<div
className="flex h-full min-h-0 min-w-0 flex-col"
style={
Expand All @@ -156,6 +158,14 @@ function PreviewPanelShellFrame(
>
{props.children}
</div>
{props.corner ? (
<div
className="absolute top-[var(--workspace-controls-top)] right-0 z-10 h-[var(--workspace-topbar-height)]"
data-panel-titlebar-corner
>
{props.corner}
</div>
) : null}
</div>
</div>
);
Expand Down
Loading
Loading