Skip to content

Commit 41990a1

Browse files
committed
improvement(sidebar): panel resize logic
1 parent 78ea04f commit 41990a1

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-panel-resize.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@ import { useCallback, useEffect, useRef, useState } from 'react'
22
import { useSidebarStore } from '@/stores/sidebar/store'
33

44
/**
5-
* Constants for panel sizing
5+
* Default height for panel content when expanded (in pixels)
66
*/
77
const DEFAULT_HEIGHT = 200
8+
9+
/**
10+
* Minimum height for a collapsed panel (in pixels)
11+
*/
812
const MIN_HEIGHT = 28
13+
14+
/**
15+
* Height of the panel header (in pixels)
16+
*/
917
const HEADER_HEIGHT = 28
1018

1119
/**
12-
* Panel type configuration
20+
* Type representing the available panel types in the sidebar
1321
*/
1422
type PanelType = 'triggers' | 'blocks'
1523

24+
/**
25+
* Props for the usePanelResize hook
26+
*/
1627
interface UsePanelResizeProps {
28+
/** Type of panel to resize (triggers or blocks) */
1729
panelType: PanelType
30+
/** Reference to the container element for boundary calculations */
1831
containerRef: React.RefObject<HTMLDivElement | null>
1932
}
2033

@@ -23,7 +36,13 @@ interface UsePanelResizeProps {
2336
* Provides unified logic for both panels with proper constraint handling.
2437
*
2538
* @param props - Configuration object containing panel type and container ref
26-
* @returns Panel resize state and handlers
39+
* @param props.panelType - Type of panel to resize (triggers or blocks)
40+
* @param props.containerRef - Reference to the container element for boundary calculations
41+
* @returns Object containing panel state and handlers
42+
* @returns currentHeight - Current height of the panel in pixels
43+
* @returns isResizing - Boolean indicating if the panel is currently being resized
44+
* @returns handleMouseDown - Handler for mouse down events on the resize handle
45+
* @returns handleToggle - Handler to toggle panel between collapsed and expanded states
2746
*/
2847
export function usePanelResize({ panelType, containerRef }: UsePanelResizeProps) {
2948
const { triggersHeight, blocksHeight, setTriggersHeight, setBlocksHeight } = useSidebarStore()
@@ -34,12 +53,11 @@ export function usePanelResize({ panelType, containerRef }: UsePanelResizeProps)
3453

3554
// Get current panel's height and setter based on type
3655
const currentHeight = panelType === 'triggers' ? triggersHeight : blocksHeight
37-
const setCurrentHeight = panelType === 'triggers' ? setTriggersHeight : setBlocksHeight
38-
const otherHeight = panelType === 'triggers' ? blocksHeight : triggersHeight
39-
const setOtherHeight = panelType === 'triggers' ? setBlocksHeight : setTriggersHeight
4056

4157
/**
42-
* Handles mouse down on resize handle
58+
* Handles mouse down event on the resize handle to initiate panel resizing
59+
*
60+
* @param e - The React mouse event from the resize handle
4361
*/
4462
const handleMouseDown = useCallback(
4563
(e: React.MouseEvent) => {
@@ -56,7 +74,12 @@ export function usePanelResize({ panelType, containerRef }: UsePanelResizeProps)
5674
)
5775

5876
/**
59-
* Handle toggle collapse/expand
77+
* Toggles the panel between collapsed and expanded states
78+
*
79+
* @remarks
80+
* Handles complex logic for both triggers and blocks panels, ensuring proper
81+
* constraints are maintained between the two panels. When collapsing/expanding,
82+
* it respects minimum heights and parent container boundaries.
6083
*/
6184
const handleToggle = useCallback(() => {
6285
if (panelType === 'triggers') {
@@ -110,13 +133,33 @@ export function usePanelResize({ panelType, containerRef }: UsePanelResizeProps)
110133
setBlocksHeight(desiredBlocksHeight)
111134
} else {
112135
// Collapsing blocks
136+
137+
// Check if triggers is currently collapsed on top of blocks
138+
// When triggers is collapsed, its height = blocksHeight + HEADER_HEIGHT
139+
const expectedCollapsedTriggersHeight = blocksHeight + HEADER_HEIGHT
140+
const isTriggersCollapsedOnTop =
141+
Math.abs(triggersHeight - expectedCollapsedTriggersHeight) <= 2
142+
143+
// IMPORTANT: Set blocks first, then triggers
144+
// The store enforces: triggersHeight >= blocksHeight + HEADER_HEIGHT
145+
// So we must reduce blocks first to allow triggers to be reduced
113146
setBlocksHeight(MIN_HEIGHT)
147+
148+
// If triggers is collapsed on top of blocks, collapse it too
149+
if (isTriggersCollapsedOnTop) {
150+
setTriggersHeight(MIN_HEIGHT)
151+
}
114152
}
115153
}
116154
}, [panelType, triggersHeight, blocksHeight, setTriggersHeight, setBlocksHeight, containerRef])
117155

118156
/**
119-
* Setup resize event listeners and body styles when resizing
157+
* Sets up resize event listeners and body styles during resize operations
158+
*
159+
* @remarks
160+
* This effect manages mouse move and mouse up events during resize, applies
161+
* appropriate cursor styles, and handles complex panel constraint logic including
162+
* parent container boundaries and interdependent panel heights.
120163
*/
121164
useEffect(() => {
122165
if (!isResizing || !containerRef.current) return

0 commit comments

Comments
 (0)