refactor(webui): separate gateway mutations and UI state (#6727)
This commit is contained in:
1 parent
ce12bfdd0d
commit
5537bdabd3
70 files changed
+4058
-3650
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import type { CSSProperties, PointerEvent as ReactPointerEvent, ReactNode } from "react";
|
||||
import type { CSSProperties, ReactNode } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { composerUsageSnapshots, type ComposerUsageSnapshot } from "@/lib/model-request";
|
||||
@@ -87,6 +87,8 @@ import { projectWebuiThreadMessages } from "@/lib/thread-display-projection";
|
||||
import { ThreadMessageCache } from "@/lib/thread-message-cache";
|
||||
import { providerDisplayLabel } from "@/lib/provider-brand";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useFilePreviewResize } from "@/hooks/useFilePreviewResize";
|
||||
import { hasAnyOverlayMounted } from "@/lib/dom-overlays";
|
||||
import { composerContextSnapshots } from "@/lib/context-graph";
|
||||
import { useClient } from "@/providers/ClientProvider";
|
||||
|
||||
@@ -187,10 +189,6 @@ function hasInlineDeliveryError(
|
||||
));
|
||||
}
|
||||
|
||||
const FILE_PREVIEW_DEFAULT_WIDTH = 544;
|
||||
const FILE_PREVIEW_MIN_WIDTH = 360;
|
||||
const FILE_PREVIEW_MAX_WIDTH = 860;
|
||||
const FILE_PREVIEW_MIN_MAIN_WIDTH = 420;
|
||||
const FILE_PREVIEW_CLOSE_ANIMATION_MS = 320;
|
||||
|
||||
type FilePreviewAvailabilityCacheEntry = {
|
||||
@@ -199,20 +197,6 @@ type FilePreviewAvailabilityCacheEntry = {
|
||||
revision: number;
|
||||
};
|
||||
|
||||
function clampFilePreviewWidth(width: number, maxWidth: number): number {
|
||||
return Math.min(Math.max(width, FILE_PREVIEW_MIN_WIDTH), maxWidth);
|
||||
}
|
||||
|
||||
function maxFilePreviewWidth(containerWidth: number): number {
|
||||
return Math.min(
|
||||
containerWidth > 0 ? containerWidth : FILE_PREVIEW_MIN_WIDTH,
|
||||
Math.max(
|
||||
FILE_PREVIEW_MIN_WIDTH,
|
||||
Math.min(FILE_PREVIEW_MAX_WIDTH, containerWidth - FILE_PREVIEW_MIN_MAIN_WIDTH),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const EMPTY_TITLE_OVERRIDES: Record<string, string> = {};
|
||||
|
||||
interface ThreadShellProps {
|
||||
@@ -498,6 +482,7 @@ export function ThreadShell({
|
||||
}, [multiplePanes, previewSessionKey]);
|
||||
const [sidebarEnterWidth, setSidebarEnterWidth] = useState(0);
|
||||
const [contextSidebarHost, setContextSidebarHost] = useState<HTMLElement | null>(null);
|
||||
const contextSidebarRef = useRef<HTMLElement>(null);
|
||||
const setSidebarHost = useCallback((node: HTMLElement | null) => { shellRef.current = node; setContextSidebarHost(node); }, []);
|
||||
const [closingPreview, setClosingPreview] = useState<{ key: string; state: FilePreviewState } | null>(null);
|
||||
const filePreviewClosing = closingPreview?.key === previewSessionKey;
|
||||
@@ -525,8 +510,6 @@ export function ThreadShell({
|
||||
if (range && selection) { selection.removeAllRanges(); selection.addRange(range); }
|
||||
}, [composerDestination, composerMount, inlineComposer]);
|
||||
useLayoutEffect(() => () => composerMount.remove(), [composerMount]);
|
||||
const [filePreviewMaxWidth, setFilePreviewMaxWidth] = useState(FILE_PREVIEW_MAX_WIDTH);
|
||||
const filePreviewWidth = clampFilePreviewWidth(previewState.width, filePreviewMaxWidth);
|
||||
const draftKey = session?.key ?? (temporaryChatEnabled ? "new:temporary" : "new:chat");
|
||||
const persistDraft = session ? !temporary : !temporaryChatEnabled;
|
||||
const [quote, setQuote] = useState<{ key: string; text: string | null } | null>(null);
|
||||
@@ -538,6 +521,9 @@ export function ThreadShell({
|
||||
}, [draftKey]);
|
||||
const [composerFocusSignal, setComposerFocusSignal] = useState(0);
|
||||
const shellRef = useRef<HTMLElement | null>(null);
|
||||
const { width: filePreviewWidth, panelRef: previewPanelRef, startResize: handleFilePreviewResizeStart, cancelResize: cancelPreviewResize } = useFilePreviewResize(
|
||||
shellRef, previewOpen, previewState.width, setFilePreviewWidth,
|
||||
);
|
||||
const [conversationElement, setConversationElement] = useState<HTMLDivElement | null>(null);
|
||||
const [composerSurface, setComposerSurface] = useState<HTMLDivElement | null>(null);
|
||||
const alignSidebar = useCallback(() => {
|
||||
@@ -556,9 +542,7 @@ export function ThreadShell({
|
||||
observer.observe(composerSurface);
|
||||
return () => observer.disconnect();
|
||||
}, [alignSidebar, contextSidebarHost, sidebarOpen, inlineComposer, composerDestination, composerMount, composerSurface]);
|
||||
const filePreviewWidthRef = useRef(FILE_PREVIEW_DEFAULT_WIDTH);
|
||||
const filePreviewCloseTimerRef = useRef<number | null>(null);
|
||||
const filePreviewResizeCleanupRef = useRef<(() => void) | null>(null);
|
||||
const pendingFirstRef = useRef<(PendingFirstMessage & { draft?: ComposerDraft; draftKey: string }) | null>(null);
|
||||
const [restoredFirstDraft, setRestoredFirstDraft] = useState<{ chatId: string; revision: number; error: string } | null>(null);
|
||||
const [pendingFirstTargetChatId, setPendingFirstTargetChatId] = useState<string | null>(null);
|
||||
@@ -647,18 +631,14 @@ export function ThreadShell({
|
||||
}, [chatId, historyKey]);
|
||||
|
||||
useEffect(() => {
|
||||
filePreviewWidthRef.current = filePreviewWidth;
|
||||
}, [filePreviewWidth]);
|
||||
|
||||
useEffect(() => {
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
cancelPreviewResize();
|
||||
if (filePreviewCloseTimerRef.current !== null) {
|
||||
window.clearTimeout(filePreviewCloseTimerRef.current);
|
||||
filePreviewCloseTimerRef.current = null;
|
||||
}
|
||||
setClosingPreview(null);
|
||||
setSubmittedViewportTurn(null);
|
||||
}, [previewSessionKey]);
|
||||
}, [cancelPreviewResize, previewSessionKey]);
|
||||
|
||||
useEffect(() => {
|
||||
const retained = new Set(temporaryChatIds);
|
||||
@@ -680,7 +660,6 @@ export function ThreadShell({
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
if (filePreviewCloseTimerRef.current !== null) {
|
||||
window.clearTimeout(filePreviewCloseTimerRef.current);
|
||||
}
|
||||
@@ -1133,7 +1112,7 @@ export function ThreadShell({
|
||||
|
||||
const handleCloseFilePreview = useCallback(() => {
|
||||
if (!previewSessionKey || !previewState.activeId || filePreviewClosing) return;
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
cancelPreviewResize();
|
||||
setClosingPreview({ key: previewSessionKey, state: previewState });
|
||||
// Record the closed state immediately, even if navigation interrupts the animation.
|
||||
closePreview();
|
||||
@@ -1141,23 +1120,24 @@ export function ThreadShell({
|
||||
filePreviewCloseTimerRef.current = null;
|
||||
setClosingPreview(null);
|
||||
}, FILE_PREVIEW_CLOSE_ANIMATION_MS);
|
||||
}, [previewSessionKey, filePreviewClosing, previewState, closePreview]);
|
||||
}, [cancelPreviewResize, previewSessionKey, filePreviewClosing, previewState, closePreview]);
|
||||
|
||||
const cancelPreviewClose = useCallback(() => {
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
cancelPreviewResize();
|
||||
if (filePreviewCloseTimerRef.current !== null) {
|
||||
window.clearTimeout(filePreviewCloseTimerRef.current);
|
||||
filePreviewCloseTimerRef.current = null;
|
||||
}
|
||||
setClosingPreview(null);
|
||||
}, []);
|
||||
}, [cancelPreviewResize]);
|
||||
|
||||
const handleOpenFilePreview = useCallback((path: string) => {
|
||||
setSidebarEnterWidth(shellRef.current?.querySelector(".file-preview-pane")?.getBoundingClientRect().width ?? 0);
|
||||
const outgoingSidebar = previewPanelRef.current ?? contextSidebarRef.current;
|
||||
setSidebarEnterWidth(outgoingSidebar?.getBoundingClientRect().width ?? 0);
|
||||
setContextSidebarOpen(false);
|
||||
cancelPreviewClose();
|
||||
openFile(path);
|
||||
}, [cancelPreviewClose, openFile]);
|
||||
}, [cancelPreviewClose, openFile, previewPanelRef]);
|
||||
|
||||
const handleContextSidebarOpenChange = useCallback((open: boolean) => {
|
||||
if (open) {
|
||||
@@ -1170,10 +1150,10 @@ export function ThreadShell({
|
||||
const handleClosePreviewTab = useCallback((id: string) => {
|
||||
if (previewState.tabs.length === 1) handleCloseFilePreview();
|
||||
else {
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
cancelPreviewResize();
|
||||
closeTab(id);
|
||||
}
|
||||
}, [previewState.tabs.length, handleCloseFilePreview, closeTab]);
|
||||
}, [cancelPreviewResize, previewState.tabs.length, handleCloseFilePreview, closeTab]);
|
||||
|
||||
// Markdown blocks can retain rendered links while their text is unchanged.
|
||||
// Keep their callback stable, but always act on the current pane/session state.
|
||||
@@ -1203,7 +1183,7 @@ export function ThreadShell({
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key !== "Escape" || event.defaultPrevented || event.isComposing) return;
|
||||
// Let a dialog/menu above the pane consume Escape without closing this preview.
|
||||
if (document.querySelector('[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"], [data-radix-popper-content-wrapper]')) return;
|
||||
if (hasAnyOverlayMounted()) return;
|
||||
event.preventDefault();
|
||||
handleCloseFilePreview();
|
||||
};
|
||||
@@ -1211,84 +1191,6 @@ export function ThreadShell({
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [previewOpen, headerActive, handleCloseFilePreview]);
|
||||
|
||||
const handleFilePreviewResizeStart = useCallback((event: ReactPointerEvent<HTMLButtonElement>) => {
|
||||
filePreviewResizeCleanupRef.current?.();
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const panel = event.currentTarget.closest<HTMLElement>("[data-file-preview-panel]");
|
||||
const shellRect = shellRef.current?.getBoundingClientRect();
|
||||
const rightEdge = shellRect?.right ?? window.innerWidth;
|
||||
const maxWidth = maxFilePreviewWidth(shellRect?.width ?? window.innerWidth);
|
||||
const originalBodyCursor = document.body.style.cursor;
|
||||
const originalBodyUserSelect = document.body.style.userSelect;
|
||||
const originalPanelTransition = panel?.style.transition ?? "";
|
||||
const frameElement = panel?.querySelector("iframe");
|
||||
const originalFramePointerEvents = frameElement?.style.pointerEvents ?? "";
|
||||
let nextWidth = filePreviewWidthRef.current;
|
||||
let frame: number | null = null;
|
||||
|
||||
document.body.style.cursor = "col-resize";
|
||||
document.body.style.userSelect = "none";
|
||||
if (panel) panel.style.transition = "none";
|
||||
if (frameElement) frameElement.style.pointerEvents = "none";
|
||||
|
||||
const applyWidth = (clientX: number) => {
|
||||
nextWidth = clampFilePreviewWidth(rightEdge - clientX, maxWidth);
|
||||
filePreviewWidthRef.current = nextWidth;
|
||||
if (frame !== null) return;
|
||||
frame = window.requestAnimationFrame(() => {
|
||||
frame = null;
|
||||
panel?.style.setProperty("--file-preview-width", `${nextWidth}px`);
|
||||
panel?.style.setProperty("--file-preview-slot-width", `${nextWidth}px`);
|
||||
});
|
||||
};
|
||||
const handlePointerMove = (moveEvent: PointerEvent) => {
|
||||
moveEvent.preventDefault();
|
||||
applyWidth(moveEvent.clientX);
|
||||
};
|
||||
const stopResize = (commit: boolean) => {
|
||||
if (frame !== null) {
|
||||
window.cancelAnimationFrame(frame);
|
||||
frame = null;
|
||||
}
|
||||
panel?.style.setProperty("--file-preview-width", `${nextWidth}px`);
|
||||
panel?.style.setProperty("--file-preview-slot-width", `${nextWidth}px`);
|
||||
if (panel) panel.style.transition = originalPanelTransition;
|
||||
if (frameElement) frameElement.style.pointerEvents = originalFramePointerEvents;
|
||||
if (commit) setFilePreviewWidth(nextWidth);
|
||||
document.body.style.cursor = originalBodyCursor;
|
||||
document.body.style.userSelect = originalBodyUserSelect;
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
window.removeEventListener("pointercancel", handlePointerUp);
|
||||
filePreviewResizeCleanupRef.current = null;
|
||||
};
|
||||
const handlePointerUp = () => stopResize(true);
|
||||
filePreviewResizeCleanupRef.current = () => stopResize(false);
|
||||
|
||||
applyWidth(event.clientX);
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
window.addEventListener("pointercancel", handlePointerUp);
|
||||
}, [setFilePreviewWidth]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!previewOpen) return;
|
||||
const clampToShell = () => {
|
||||
const shellWidth = shellRef.current?.getBoundingClientRect().width ?? window.innerWidth;
|
||||
const maxWidth = maxFilePreviewWidth(shellWidth);
|
||||
setFilePreviewMaxWidth(maxWidth);
|
||||
};
|
||||
clampToShell();
|
||||
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(clampToShell);
|
||||
if (shellRef.current) observer?.observe(shellRef.current);
|
||||
window.addEventListener("resize", clampToShell);
|
||||
return () => {
|
||||
observer?.disconnect();
|
||||
window.removeEventListener("resize", clampToShell);
|
||||
};
|
||||
}, [previewOpen]);
|
||||
|
||||
const handleForkFromMessage = useCallback(
|
||||
async (beforeUserIndex: number) => {
|
||||
if (!chatId || !onForkChat) return;
|
||||
@@ -1588,6 +1490,7 @@ export function ThreadShell({
|
||||
rounds={composerRoundUsage}
|
||||
snapshots={contextGraphSnapshots}
|
||||
sidebarHost={contextSidebarHost}
|
||||
sidebarRef={contextSidebarRef}
|
||||
open={contextSidebarOpen}
|
||||
onOpenChange={handleContextSidebarOpenChange}
|
||||
onOpenFile={handleOpenFilePreview}
|
||||
@@ -1615,6 +1518,7 @@ export function ThreadShell({
|
||||
onSelect={selectTab}
|
||||
onCloseTab={handleClosePreviewTab}
|
||||
onClose={handleCloseFilePreview}
|
||||
panelRef={previewPanelRef}
|
||||
onResizeStart={handleFilePreviewResizeStart}
|
||||
>
|
||||
<FilePreviewPanel
|
||||
|
||||
Reference in new issue
Block a user