import classnames from 'classnames'; import type { CSSProperties, MouseEvent as ReactMouseEvent } from 'react'; import React, { useCallback, useMemo, useRef, useState } from 'react'; import { useKeyValue } from '../hooks/useKeyValue'; import { RequestPane } from './RequestPane'; import { ResponsePane } from './ResponsePane'; import { ResizeBar } from './Workspace'; const rqst = { gridArea: 'rqst' }; const resp = { gridArea: 'resp' }; const drag = { gridArea: 'drag' }; export default function RequestResponse() { const DEFAULT = 0.5; const containerRef = useRef(null); const widthKv = useKeyValue({ key: 'body_width', defaultValue: DEFAULT }); const heightKv = useKeyValue({ key: 'body_height', defaultValue: DEFAULT }); const width = widthKv.value ?? DEFAULT; const height = heightKv.value ?? DEFAULT; const [isResizing, setIsResizing] = useState(false); const moveState = useRef<{ move: (e: MouseEvent) => void; up: (e: MouseEvent) => void } | null>( null, ); const vertical = false; const styles = useMemo( () => ({ gridTemplate: vertical ? ` ' ${rqst.gridArea}' ${1 - height}fr ' ${drag.gridArea}' auto ' ${resp.gridArea}' ${height}fr / 1fr ` : ` ' ${rqst.gridArea} ${drag.gridArea} ${resp.gridArea}' minmax(0,1fr) / ${1 - width}fr auto ${width}fr `, }), [vertical, width, height], ); const unsub = () => { if (moveState.current !== null) { document.documentElement.removeEventListener('mousemove', moveState.current.move); document.documentElement.removeEventListener('mouseup', moveState.current.up); } }; const handleReset = useCallback( () => (vertical ? heightKv.set(DEFAULT) : widthKv.set(DEFAULT)), [vertical], ); const handleResizeStart = useCallback( (e: ReactMouseEvent) => { if (containerRef.current === null) return; unsub(); const containerRect = containerRef.current.getBoundingClientRect(); const mouseStartX = e.clientX; const mouseStartY = e.clientY; const startWidth = containerRect.width * width; const startHeight = containerRect.height * height; moveState.current = { move: (e: MouseEvent) => { e.preventDefault(); // Prevent text selection and things if (vertical) { const newHeightPx = startHeight - (e.clientY - mouseStartY); const newHeight = newHeightPx / containerRect.height; heightKv.set(newHeight); } else { const newWidthPx = startWidth - (e.clientX - mouseStartX); const newWidth = newWidthPx / containerRect.width; widthKv.set(newWidth); } }, up: (e: MouseEvent) => { e.preventDefault(); unsub(); setIsResizing(false); }, }; document.documentElement.addEventListener('mousemove', moveState.current.move); document.documentElement.addEventListener('mouseup', moveState.current.up); setIsResizing(true); }, [widthKv.value, heightKv.value, vertical], ); return (
); }