Make chat timeline height estimates width-aware with attachment-aware tests #167
Mergedjuliusmarmingemerged 11 commits into
main fromrefs/pull/167/headmerged by juliusmarminge View on GitHub- @@ -3202,0 +3198,21 @@3198+ setTimelineWidthPx((previousWidth) => {3199+ if (previousWidth !== null && Math.abs(previousWidth - nextWidth) < 0.5) {3200+ return previousWidth;3201+ }3202+ return nextWidth;3203+ });3204+ };3205+3206+ updateWidth(timelineRoot.getBoundingClientRect().width);3207+3208+ if (typeof ResizeObserver === "undefined") return;3209+ const observer = new ResizeObserver((entries) => {3210+ const [entry] = entries;3211+ if (!entry) return;3212+ updateWidth(entry.contentRect.width);3213+ });3214+ observer.observe(timelineRoot);3215+ return () => {3216+ observer.disconnect();3217+ };3218+ }, []);
- apps/web/src/components/timelineHeight.ts lines 14 to 14OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+const ASSISTANT_CHARS_PER_LINE_FALLBACK = 72;2+const USER_CHARS_PER_LINE_FALLBACK = 56;3+const LINE_HEIGHT_PX = 22;4+const ASSISTANT_BASE_HEIGHT_PX = 78;5+const USER_BASE_HEIGHT_PX = 96;6+const ATTACHMENTS_PER_ROW = 2;7+// Attachment thumbnails render with `max-h-[220px]` plus ~8px row gap.8+const USER_ATTACHMENT_ROW_HEIGHT_PX = 228;9+const USER_BUBBLE_WIDTH_RATIO = 0.8;10+const USER_BUBBLE_HORIZONTAL_PADDING_PX = 32;11+const ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX = 8;12+const USER_MONO_AVG_CHAR_WIDTH_PX = 8.4;13+const ASSISTANT_AVG_CHAR_WIDTH_PX = 7.2;14+const MIN_USER_CHARS_PER_LINE = 16;
- @@ -3202,0 +3192,21 @@3192+3193+ useLayoutEffect(() => {3194+ const timelineRoot = timelineRootRef.current;3195+ if (!timelineRoot) return;3196+3197+ const updateWidth = (nextWidth: number) => {3198+ setTimelineWidthPx((previousWidth) => {3199+ if (previousWidth !== null && Math.abs(previousWidth - nextWidth) < 0.5) {3200+ return previousWidth;3201+ }3202+ return nextWidth;3203+ });3204+ };3205+3206+ updateWidth(timelineRoot.getBoundingClientRect().width);3207+3208+ if (typeof ResizeObserver === "undefined") return;3209+ const observer = new ResizeObserver((entries) => {3210+ const [entry] = entries;3211+ if (!entry) return;3212+ updateWidth(entry.contentRect.width);
- @@ -26,3 +26,6 @@2626 }>()({2727 component: RootRouteView,2828 errorComponent: RootRouteErrorView,29+ head: () => ({30+ meta: [{ name: "title", content: APP_DISPLAY_NAME }],31+ }),
- apps/web/src/components/ChatView.tsx lines 3193 to 3218OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3202,0 +3198,21 @@3198+ setTimelineWidthPx((previousWidth) => {3199+ if (previousWidth !== null && Math.abs(previousWidth - nextWidth) < 0.5) {3200+ return previousWidth;3201+ }3202+ return nextWidth;3203+ });3204+ };3205+3206+ updateWidth(timelineRoot.getBoundingClientRect().width);3207+3208+ if (typeof ResizeObserver === "undefined") return;3209+ const observer = new ResizeObserver((entries) => {3210+ const [entry] = entries;3211+ if (!entry) return;3212+ updateWidth(entry.contentRect.width);3213+ });3214+ observer.observe(timelineRoot);3215+ return () => {3216+ observer.disconnect();3217+ };3218+ }, []);
- apps/web/src/components/timelineHeight.ts line 73Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +53,21 @@53+ const textWidthPx = Math.max(bubbleWidthPx - USER_BUBBLE_HORIZONTAL_PADDING_PX, 0);54+ return Math.max(MIN_USER_CHARS_PER_LINE, Math.floor(textWidthPx / USER_MONO_AVG_CHAR_WIDTH_PX));55+}56+57+function estimateCharsPerLineForAssistant(timelineWidthPx: number | null): number {58+ if (!isFinitePositiveNumber(timelineWidthPx)) return ASSISTANT_CHARS_PER_LINE_FALLBACK;59+ const textWidthPx = Math.max(timelineWidthPx - ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX, 0);60+ return Math.max(61+ MIN_ASSISTANT_CHARS_PER_LINE,62+ Math.floor(textWidthPx / ASSISTANT_AVG_CHAR_WIDTH_PX),63+ );64+}65+66+export function estimateTimelineMessageHeight(67+ message: TimelineMessageHeightInput,68+ layout: TimelineHeightEstimateLayout = { timelineWidthPx: null },69+): number {70+ if (message.role !== "user") {71+ const charsPerLine = estimateCharsPerLineForAssistant(layout.timelineWidthPx);72+ const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);73+ return ASSISTANT_BASE_HEIGHT_PX + estimatedLines * LINE_HEIGHT_PX;
juliusmarminge merged this7c7fcf4
juliusmarminge closed this
Sign in to join this conversation.



Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
0d941110-f4e5-4a28-a68b-50fcf6e47356You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands and usage tips.