Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export const SingleWSMessage = ({
isNew,
onNewRendered,
isSelected,
onSelect
onSelect,
paneHeight
}) => {
const dispatch = useDispatch();
const { displayedTheme } = useTheme();
Expand Down Expand Up @@ -105,11 +106,16 @@ export const SingleWSMessage = ({
const fontSize = get(preferences, 'font.codeFontSize', 14);
const lineHeight = fontSize * 1.5;

const HEADER_ALLOWANCE = 44;
const maxEditorHeight = paneHeight
? Math.max(160, paneHeight - HEADER_ALLOWANCE)
: Math.round((typeof window !== 'undefined' ? window.innerHeight : 900) * 0.6);
const editorHeight = useMemo(() => {
const lineCount = (content || '').split('\n').length;
const lines = lineCount + 1;
return `${lines * lineHeight + 10}px`;
}, [content, lineHeight]);
const contentHeight = lines * lineHeight + 10;
return `${Math.min(contentHeight, maxEditorHeight)}px`;
}, [content, lineHeight, maxEditorHeight]);

const onUpdateMessageType = (newMode) => {
const currentMessages = [...(body.ws || [])];
Expand Down Expand Up @@ -173,8 +179,8 @@ export const SingleWSMessage = ({
return (
<StyledWrapper
className={!isSelected ? 'disabled' : ''}
onMouseDownCapture={() => {
if (!isSelected) setTimeout(onSelect, 0);
onMouseUpCapture={() => {
if (!isSelected) onSelect();
}}
>
<div
Expand Down Expand Up @@ -221,10 +227,7 @@ export const SingleWSMessage = ({
<span
className="message-label"
data-testid={`ws-message-label-${index}`}
onClick={(e) => {
e.preventDefault();
onToggle();
}}
onClick={(e) => e.stopPropagation()}
onDoubleClick={handleNameClick}
>
{displayName}
Expand All @@ -251,7 +254,11 @@ export const SingleWSMessage = ({
</div>
</div>
{isExpanded && (
<div className="accordion-body" data-testid={`ws-message-body-${index}`} style={{ height: editorHeight }}>
<div
className="accordion-body"
data-testid={`ws-message-body-${index}`}
style={{ height: editorHeight }}
>
<CodeEditor
collection={collection}
theme={displayedTheme}
Expand All @@ -263,6 +270,7 @@ export const SingleWSMessage = ({
onSave={onSave}
mode={codemirrorMode[displayMode] ?? 'text/plain'}
enableVariableHighlighting={true}
readOnly={isSelected ? false : 'nocursor'}
/>
</div>
)}
Expand Down
87 changes: 80 additions & 7 deletions packages/bruno-app/src/components/RequestPane/WsBody/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { updateRequestBody } from 'providers/ReduxStore/slices/collections';
import { IconPlus } from '@tabler/icons';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { usePersistedState } from 'hooks/usePersistedState';
import StyledWrapper from './StyledWrapper';
import { SingleWSMessage } from './SingleWSMessage/index';

Expand All @@ -14,6 +15,10 @@ const getSelectedIndex = (messages) => {
const WSBody = ({ item, collection, handleRun, onAddMessage }) => {
const dispatch = useDispatch();
const messagesContainerRef = useRef(null);
const [listScrollTop, setListScrollTop] = usePersistedState({
key: `ws-list-scroll-${item.uid}`,
default: 0
});
const body = item.draft ? get(item, 'draft.request.body') : get(item, 'request.body');
const messages = body?.ws || [];

Expand All @@ -27,6 +32,19 @@ const WSBody = ({ item, collection, handleRun, onAddMessage }) => {
const [newMessageUid, setNewMessageUid] = useState(null);
const prevMessagesLengthRef = useRef(messages.length);

// Track the message pane's height so an expanded editor can be capped to fit
// inside it. A taller editor would overflow the pane and produce a second
// scrollbar (the list) on top of the editor's own scroll.
const [paneHeight, setPaneHeight] = useState(0);
useEffect(() => {
const el = messagesContainerRef.current;
if (!el || typeof ResizeObserver === 'undefined') return;
const ro = new ResizeObserver(() => setPaneHeight(el.clientHeight));
ro.observe(el);
setPaneHeight(el.clientHeight);
return () => ro.disconnect();
}, []);

const setSelectedIndex = useCallback((index) => {
const currentMessages = [...(body?.ws || [])];
const updated = currentMessages.map((msg, i) => ({
Expand Down Expand Up @@ -68,6 +86,11 @@ const WSBody = ({ item, collection, handleRun, onAddMessage }) => {
setNewMessageUid(newMsg.uid);
setSelectedIndex(messages.length - 1);
}

if (messagesContainerRef.current) {
const container = messagesContainerRef.current;
container.scrollTop = container.scrollHeight;
}
}
prevMessagesLengthRef.current = messages.length;
}, [messages.length]);
Expand All @@ -76,13 +99,54 @@ const WSBody = ({ item, collection, handleRun, onAddMessage }) => {
setNewMessageUid(null);
}, []);

// Auto-scroll to bottom when new message is added
// Restore the last scroll position on mount (component remounts on tab switch,
// so listScrollTop is read synchronously for this request).
useEffect(() => {
if (messagesContainerRef.current && messages.length > 0) {
const container = messagesContainerRef.current;
container.scrollTop = container.scrollHeight;
const container = messagesContainerRef.current;
if (container) {
container.scrollTop = listScrollTop;
}
}, [messages.length]);
}, [item.uid]);

const handleScroll = useCallback(() => {
const container = messagesContainerRef.current;
if (container) {
setListScrollTop(container.scrollTop);
}
}, [setListScrollTop]);

// Clicking or typing in an editor makes the browser scroll the list to reveal
// CodeMirror's cursor, flinging the whole panel. Pin the list's scrollTop for a
// few frames so focus/keystrokes can't move it (the editor still scrolls
// internally); a real user scroll (wheel/touch) releases the pin.
const pinScrollRef = useRef(null);
const pinListScroll = useCallback(() => {
const container = messagesContainerRef.current;
if (!container) return;
const top = container.scrollTop;
let frames = 0;
const pin = () => {
const el = messagesContainerRef.current;
if (!el || pinScrollRef.current === null) return;
if (el.scrollTop !== top) el.scrollTop = top;
if (++frames < 8) {
pinScrollRef.current = requestAnimationFrame(pin);
} else {
pinScrollRef.current = null;
}
};
// Cancel any in-flight pin so a fresh gesture re-snapshots the current top.
if (pinScrollRef.current !== null) cancelAnimationFrame(pinScrollRef.current);
pinScrollRef.current = requestAnimationFrame(pin);
}, []);

// A real user scroll (wheel/touch) releases the pin immediately.
const releasePin = useCallback(() => {
if (pinScrollRef.current !== null) {
cancelAnimationFrame(pinScrollRef.current);
pinScrollRef.current = null;
}
}, []);

if (!messages.length) {
return (
Expand All @@ -100,11 +164,19 @@ const WSBody = ({ item, collection, handleRun, onAddMessage }) => {

return (
<StyledWrapper>
<div ref={messagesContainerRef} className="messages-container">
<div
ref={messagesContainerRef}
className="messages-container"
data-testid="ws-messages-container"
onScroll={handleScroll}
onMouseDownCapture={pinListScroll}
onKeyDownCapture={pinListScroll}
onWheel={releasePin}
onTouchMove={releasePin}
>
{messages.map((message, index) => (
<SingleWSMessage
key={message.uid}
id={`ws-message-${message.uid}`}
message={message}
item={item}
collection={collection}
Expand All @@ -116,6 +188,7 @@ const WSBody = ({ item, collection, handleRun, onAddMessage }) => {
onNewRendered={handleNewMessageRendered}
isSelected={selectedIndex === index}
onSelect={() => handleSelect(index)}
paneHeight={paneHeight}
/>
))}
</div>
Expand Down
Loading
Loading