Compare commits

..
Author SHA1 Message Date
Julius Marminge 2eabd82f0b Prebundle @pierre/diffs dependencies in Vite
- Add `optimizeDeps.include` entries for `@pierre/diffs` packages in web Vite config
- Improves dev startup/runtime stability for diff rendering modules
2026-02-19 00:40:50 -08:00
4 changed files with 80 additions and 126 deletions

No files matched your search

+16 -26
View File
@@ -1,11 +1,11 @@
import { describe, expect, it } from "vitest";
import { AUTO_SCROLL_BOTTOM_EPSILON_PX, isScrollContainerAtBottom } from "./chat-scroll";
import { AUTO_SCROLL_BOTTOM_THRESHOLD_PX, isScrollContainerNearBottom } from "./chat-scroll";
describe("isScrollContainerAtBottom", () => {
describe("isScrollContainerNearBottom", () => {
it("returns true when already at bottom", () => {
expect(
isScrollContainerAtBottom({
isScrollContainerNearBottom({
scrollTop: 600,
clientHeight: 400,
scrollHeight: 1_000,
@@ -13,31 +13,31 @@ describe("isScrollContainerAtBottom", () => {
).toBe(true);
});
it("returns true when within the bottom epsilon", () => {
it("returns true when within the auto-scroll threshold", () => {
expect(
isScrollContainerAtBottom({
scrollTop: 599,
isScrollContainerNearBottom({
scrollTop: 540,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
});
it("returns false when the user is above the epsilon", () => {
it("returns false when the user is meaningfully above the bottom", () => {
expect(
isScrollContainerAtBottom({
scrollTop: 597,
isScrollContainerNearBottom({
scrollTop: 520,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(false);
});
it("clamps negative epsilons to zero", () => {
it("clamps negative thresholds to zero", () => {
expect(
isScrollContainerAtBottom(
isScrollContainerNearBottom(
{
scrollTop: 599,
scrollTop: 539,
clientHeight: 400,
scrollHeight: 1_000,
},
@@ -46,27 +46,17 @@ describe("isScrollContainerAtBottom", () => {
).toBe(false);
});
it("falls back to the default epsilon for non-finite values", () => {
it("falls back to the default threshold for non-finite values", () => {
expect(
isScrollContainerAtBottom(
isScrollContainerNearBottom(
{
scrollTop: 599,
scrollTop: 540,
clientHeight: 400,
scrollHeight: 1_000,
},
Number.NaN,
),
).toBe(true);
expect(AUTO_SCROLL_BOTTOM_EPSILON_PX).toBe(2);
});
it("treats overscrolled values as bottom", () => {
expect(
isScrollContainerAtBottom({
scrollTop: 610,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64);
});
});
+8 -8
View File
@@ -1,4 +1,4 @@
export const AUTO_SCROLL_BOTTOM_EPSILON_PX = 2;
export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64;
interface ScrollPosition {
scrollTop: number;
@@ -6,19 +6,19 @@ interface ScrollPosition {
scrollHeight: number;
}
export function isScrollContainerAtBottom(
export function isScrollContainerNearBottom(
position: ScrollPosition,
epsilonPx = AUTO_SCROLL_BOTTOM_EPSILON_PX,
thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
): boolean {
const epsilon = Number.isFinite(epsilonPx)
? Math.max(0, epsilonPx)
: AUTO_SCROLL_BOTTOM_EPSILON_PX;
const threshold = Number.isFinite(thresholdPx)
? Math.max(0, thresholdPx)
: AUTO_SCROLL_BOTTOM_THRESHOLD_PX;
const { scrollTop, clientHeight, scrollHeight } = position;
if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) {
return true;
}
const distanceFromBottom = Math.max(0, scrollHeight - clientHeight - scrollTop);
return distanceFromBottom <= epsilon;
const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
return distanceFromBottom <= threshold;
}
+12 -92
View File
@@ -17,9 +17,6 @@ import {
type DragEvent,
type FormEvent,
type KeyboardEvent,
type PointerEvent,
type TouchEvent,
type WheelEvent,
memo,
type RefObject,
useCallback,
@@ -64,7 +61,7 @@ import {
formatElapsed,
formatTimestamp,
} from "../session-logic";
import { isScrollContainerAtBottom } from "../chat-scroll";
import { isScrollContainerNearBottom } from "../chat-scroll";
import { useStore } from "../store";
import { truncateTitle } from "../truncateTitle";
import {
@@ -405,11 +402,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
>(() => readLastInvokedScriptByProjectFromStorage());
const messagesScrollRef = useRef<HTMLDivElement>(null);
const shouldAutoScrollRef = useRef(true);
const lastKnownScrollTopRef = useRef(0);
const isPointerScrollActiveRef = useRef(false);
const lastTouchClientYRef = useRef<number | null>(null);
const pendingUserScrollUpIntentRef = useRef(false);
const pendingAutoScrollFrameRef = useRef<number | null>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const composerCommandInputRef = useRef<HTMLInputElement>(null);
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
@@ -1102,97 +1094,33 @@ export default function ChatView({ threadId }: ChatViewProps) {
}
}, [lastInvokedScriptByProjectId]);
// Auto-tail while the user stays at the bottom.
// Auto-scroll on new messages
const messageCount = activeThread?.messages.length ?? 0;
const workLogCount = workLogEntries.length;
const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
lastKnownScrollTopRef.current = scrollContainer.scrollTop;
shouldAutoScrollRef.current = true;
}, []);
const requestAutoTailToBottom = useCallback(() => {
if (pendingAutoScrollFrameRef.current !== null) return;
pendingAutoScrollFrameRef.current = window.requestAnimationFrame(() => {
pendingAutoScrollFrameRef.current = null;
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom();
});
}, [scrollMessagesToBottom]);
const onMessagesScroll = useCallback(() => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
const currentScrollTop = scrollContainer.scrollTop;
const atBottom = isScrollContainerAtBottom(scrollContainer);
if (!shouldAutoScrollRef.current && atBottom) {
shouldAutoScrollRef.current = true;
pendingUserScrollUpIntentRef.current = false;
} else if (shouldAutoScrollRef.current && pendingUserScrollUpIntentRef.current) {
const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1;
if (scrolledUp) {
shouldAutoScrollRef.current = false;
}
pendingUserScrollUpIntentRef.current = false;
} else if (shouldAutoScrollRef.current && isPointerScrollActiveRef.current) {
// Pointer-driven upward scroll means user takes control.
const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1;
if (scrolledUp) {
shouldAutoScrollRef.current = false;
}
}
lastKnownScrollTopRef.current = currentScrollTop;
}, []);
const onMessagesWheel = useCallback((event: WheelEvent<HTMLDivElement>) => {
// Wheel-up indicates user intent to move away from bottom.
if (event.deltaY < 0) {
pendingUserScrollUpIntentRef.current = true;
}
}, []);
const onMessagesPointerDown = useCallback((_event: PointerEvent<HTMLDivElement>) => {
isPointerScrollActiveRef.current = true;
}, []);
const onMessagesPointerUp = useCallback((_event: PointerEvent<HTMLDivElement>) => {
isPointerScrollActiveRef.current = false;
}, []);
const onMessagesPointerCancel = useCallback((_event: PointerEvent<HTMLDivElement>) => {
isPointerScrollActiveRef.current = false;
}, []);
const onMessagesTouchStart = useCallback((event: TouchEvent<HTMLDivElement>) => {
const touch = event.touches[0];
if (!touch) return;
lastTouchClientYRef.current = touch.clientY;
}, []);
const onMessagesTouchMove = useCallback((event: TouchEvent<HTMLDivElement>) => {
const touch = event.touches[0];
if (!touch) return;
const previousTouchY = lastTouchClientYRef.current;
if (previousTouchY !== null && touch.clientY > previousTouchY + 1) {
// Finger moved downward => user intends to scroll content upward.
pendingUserScrollUpIntentRef.current = true;
}
lastTouchClientYRef.current = touch.clientY;
}, []);
const onMessagesTouchEnd = useCallback((_event: TouchEvent<HTMLDivElement>) => {
lastTouchClientYRef.current = null;
shouldAutoScrollRef.current = isScrollContainerNearBottom(scrollContainer);
}, []);
useLayoutEffect(() => {
if (!activeThread?.id) return;
scrollMessagesToBottom();
}, [activeThread?.id, scrollMessagesToBottom]);
useLayoutEffect(() => {
useEffect(() => {
if (!shouldAutoScrollRef.current) return;
requestAutoTailToBottom();
}, [activeThread?.messages, workLogCount, requestAutoTailToBottom]);
useEffect(
() => () => {
if (pendingAutoScrollFrameRef.current === null) return;
window.cancelAnimationFrame(pendingAutoScrollFrameRef.current);
pendingAutoScrollFrameRef.current = null;
},
[],
);
scrollMessagesToBottom("smooth");
}, [messageCount, scrollMessagesToBottom]);
useEffect(() => {
if (phase !== "running") return;
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom("smooth");
}, [phase, workLogCount, scrollMessagesToBottom]);
useEffect(() => {
setExpandedWorkGroups({});
@@ -2122,14 +2050,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
ref={messagesScrollRef}
className="min-h-0 flex-1 overflow-y-auto px-5 py-4"
onScroll={onMessagesScroll}
onWheel={onMessagesWheel}
onPointerDown={onMessagesPointerDown}
onPointerUp={onMessagesPointerUp}
onPointerCancel={onMessagesPointerCancel}
onTouchStart={onMessagesTouchStart}
onTouchMove={onMessagesTouchMove}
onTouchEnd={onMessagesTouchEnd}
onTouchCancel={onMessagesTouchEnd}
>
<MessagesTimeline
hasMessages={activeThread.messages.length > 0}
+44
View File
@@ -0,0 +1,44 @@
import tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { tanstackRouter } from "@tanstack/router-plugin/vite";
import { defineConfig } from "vite";
const port = Number(process.env.PORT ?? 5173);
export default defineConfig({
plugins: [
tanstackRouter(),
react({
babel: {
plugins: [["babel-plugin-react-compiler", { target: "19" }]],
},
}),
tailwindcss(),
],
optimizeDeps: {
include: ["@pierre/diffs", "@pierre/diffs/react", "@pierre/diffs/worker/worker.js"],
},
define: {
// In dev mode, tell the web app where the WebSocket server lives
"import.meta.env.VITE_WS_URL": JSON.stringify(process.env.VITE_WS_URL ?? ""),
},
experimental: {
enableNativePlugin: true,
},
resolve: {
tsconfigPaths: true,
},
server: {
port,
strictPort: true,
hmr: {
// Explicit config so Vite's HMR WebSocket connects reliably
// inside Electron's BrowserWindow. Vite 8 uses console.debug for
// connection logs — enable "Verbose" in DevTools to see them.
protocol: "ws",
host: "localhost",
},
},
build: {
outDir: "dist",
emptyOutDir: true,
},
});