11import { useCallback , useEffect , useRef , useState } from 'react'
22import {
33 WORKSPACE_CHAT_MIN_WIDTH ,
4+ WORKSPACE_CHAT_RIGHT_MIN_WIDTH ,
45 WORKSPACE_RIGHT_PANEL_DEFAULT_WIDTH ,
56 WORKSPACE_RIGHT_PANEL_MIN_WIDTH ,
7+ WORKSPACE_RIGHT_PANEL_RESTORE_WIDTH ,
68 WORKSPACE_SPLITTER_WIDTH ,
79} from '../desktop/constants'
810
@@ -30,6 +32,7 @@ export function useWorkspaceSplit({
3032 const [ isDragging , setIsDragging ] = useState ( false )
3133 const savedRightWidthRef = useRef ( defaultRightWidth )
3234 const rightPanelWidthRef = useRef ( defaultRightWidth )
35+ const autoCollapsedByWidthRef = useRef ( false )
3336 const dragRef = useRef < { startX : number ; startWidth : number } | null > ( null )
3437
3538 useEffect ( ( ) => {
@@ -56,12 +59,22 @@ export function useWorkspaceSplit({
5659 ? workspaceWidth
5760 : 0
5861
62+ const canFitRightPanel = workspaceWidth <= 0 || workspaceWidth >= WORKSPACE_CHAT_RIGHT_MIN_WIDTH
63+
5964 const commitWidth = useCallback ( ( nextWidth : number , wsWidth : number ) => {
6065 const clamped = clampRightWidth ( nextWidth , wsWidth )
6166 savedRightWidthRef . current = clamped
6267 setRightPanelWidth ( clamped )
6368 } , [ ] )
6469
70+ const collapseRightPanel = useCallback ( ( markAuto = true ) => {
71+ if ( ! rightPanelOpen ) return
72+ savedRightWidthRef . current = rightPanelWidthRef . current
73+ if ( markAuto ) autoCollapsedByWidthRef . current = true
74+ setRightPanelOpen ( false )
75+ setChatVisible ( true )
76+ } , [ rightPanelOpen ] )
77+
6578 const beginDrag = useCallback ( ( clientX : number ) => {
6679 if ( ! enabled || ! chatVisible || ! rightPanelOpen ) return
6780 dragRef . current = { startX : clientX , startWidth : rightPanelWidthRef . current }
@@ -110,24 +123,55 @@ export function useWorkspaceSplit({
110123
111124 useEffect ( ( ) => {
112125 if ( ! enabled || isDragging || workspaceWidth <= 0 ) return
113- if ( ! chatVisible || ! rightPanelOpen ) return
126+
127+ if ( rightPanelOpen && workspaceWidth < WORKSPACE_CHAT_RIGHT_MIN_WIDTH ) {
128+ collapseRightPanel ( true )
129+ return
130+ }
131+
132+ if (
133+ ! rightPanelOpen
134+ && autoCollapsedByWidthRef . current
135+ && workspaceWidth >= WORKSPACE_RIGHT_PANEL_RESTORE_WIDTH
136+ ) {
137+ autoCollapsedByWidthRef . current = false
138+ setRightPanelWidth ( savedRightWidthRef . current || defaultRightWidth )
139+ setRightPanelOpen ( true )
140+ setChatVisible ( true )
141+ return
142+ }
143+
144+ if ( ! rightPanelOpen || ! chatVisible ) return
114145
115146 const clamped = clampRightWidth ( rightPanelWidth , workspaceWidth )
116147 if ( clamped !== rightPanelWidth ) {
117148 commitWidth ( clamped , workspaceWidth )
118149 }
119- } , [ chatVisible , commitWidth , enabled , isDragging , rightPanelOpen , rightPanelWidth , workspaceWidth ] )
150+ } , [
151+ chatVisible ,
152+ collapseRightPanel ,
153+ commitWidth ,
154+ defaultRightWidth ,
155+ enabled ,
156+ isDragging ,
157+ rightPanelOpen ,
158+ rightPanelWidth ,
159+ workspaceWidth ,
160+ ] )
120161
121162 const toggleRightPanel = useCallback ( ( ) => {
122163 if ( rightPanelOpen ) {
123164 savedRightWidthRef . current = rightPanelWidthRef . current
165+ autoCollapsedByWidthRef . current = false
124166 setRightPanelOpen ( false )
125167 setChatVisible ( true )
126168 return
127169 }
170+ if ( enabled && workspaceWidth > 0 && workspaceWidth < WORKSPACE_CHAT_RIGHT_MIN_WIDTH ) return
171+ autoCollapsedByWidthRef . current = false
128172 setRightPanelWidth ( savedRightWidthRef . current || defaultRightWidth )
129173 setRightPanelOpen ( true )
130- } , [ defaultRightWidth , rightPanelOpen ] )
174+ } , [ defaultRightWidth , enabled , rightPanelOpen , workspaceWidth ] )
131175
132176 const toggleChatColumn = useCallback ( ( ) => {
133177 if ( ! rightPanelOpen ) return
@@ -152,7 +196,9 @@ export function useWorkspaceSplit({
152196 chatWidth,
153197 isDragging,
154198 canToggleChatColumn,
199+ canFitRightPanel,
155200 beginDrag,
201+ collapseRightPanel,
156202 toggleRightPanel,
157203 toggleChatColumn,
158204 }
0 commit comments