1
- import React , { useState , useContext , useMemo } from 'react'
1
+ import React , { useState , useContext , useMemo , useEffect } from 'react'
2
2
import Button from '@mui/material/Button'
3
3
import Box from '@mui/material/Box'
4
4
import Typography from '@mui/material/Typography'
@@ -35,6 +35,7 @@ import useLightTheme from '../../hooks/useLightTheme'
35
35
import useRouter from '../../hooks/useRouter'
36
36
import useAccount from '../../hooks/useAccount'
37
37
import useApps from '../../hooks/useApps'
38
+ import useSessions from '../../hooks/useSessions'
38
39
import { AccountContext } from '../../contexts/account'
39
40
import SlideMenuContainer , { triggerMenuChange } from './SlideMenuContainer'
40
41
@@ -61,6 +62,7 @@ const SidebarContent: React.FC<{
61
62
const router = useRouter ( )
62
63
const account = useAccount ( )
63
64
const apps = useApps ( )
65
+ const sessions = useSessions ( )
64
66
const { models } = useContext ( AccountContext )
65
67
const activeTab = useMemo ( ( ) => {
66
68
// Always respect resource_type if it's present
@@ -76,6 +78,40 @@ const SidebarContent: React.FC<{
76
78
router . params ,
77
79
] )
78
80
81
+ // Ensure apps are loaded when apps tab is selected
82
+ useEffect ( ( ) => {
83
+ console . log ( `[SIDEBAR] Active tab changed to ${ activeTab } ` )
84
+ const currentResourceType = RESOURCE_TYPES [ activeTab ]
85
+
86
+ // Make sure the URL reflects the correct resource type
87
+ const urlResourceType = router . params . resource_type || 'chat'
88
+
89
+ // If there's a mismatch between activeTab and URL resource_type, update the URL
90
+ if ( currentResourceType !== urlResourceType ) {
91
+ console . log ( `[SIDEBAR] Fixing resource_type mismatch: URL has ${ urlResourceType } , should be ${ currentResourceType } ` )
92
+
93
+ // Create a copy of the params with the correct resource_type
94
+ const newParams = { ...router . params } as Record < string , string > ;
95
+ newParams . resource_type = currentResourceType ;
96
+
97
+ // If switching to chat tab, remove app_id if present
98
+ if ( currentResourceType === 'chat' && router . params . app_id ) {
99
+ delete newParams . app_id ;
100
+ }
101
+
102
+ // Update the URL without triggering a reload
103
+ router . replaceParams ( newParams )
104
+ }
105
+
106
+ // Load the appropriate content for the tab
107
+ if ( currentResourceType === 'apps' ) {
108
+ apps . loadApps ( )
109
+ } else if ( currentResourceType === 'chat' ) {
110
+ // Load sessions/chats when on the chat tab
111
+ sessions . loadSessions ( )
112
+ }
113
+ } , [ activeTab , router . params ] )
114
+
79
115
const filteredModels = useMemo ( ( ) => {
80
116
return models . filter ( m => m . type === "text" || m . type === "chat" )
81
117
} , [ models ] )
@@ -158,19 +194,27 @@ const SidebarContent: React.FC<{
158
194
const fromResourceType = RESOURCE_TYPES [ activeTab ]
159
195
const toResourceType = RESOURCE_TYPES [ newValue ]
160
196
161
- // When switching to the apps tab and we have an app_id, preserve it
197
+ console . log ( '[SIDEBAR HANDLER] Switching from' , fromResourceType , 'to' , toResourceType )
198
+ console . log ( '[SIDEBAR HANDLER] Current router params:' , router . params )
199
+
200
+ // Create a new params object with all existing params except resource_type
162
201
const newParams : Record < string , any > = {
163
- resource_type : RESOURCE_TYPES [ newValue ] ,
202
+ ... router . params
164
203
} ;
165
204
166
- // If we're switching to apps tab and there's an app_id in the URL, keep it
167
- if ( RESOURCE_TYPES [ newValue ] === 'apps' && router . params . app_id ) {
168
- newParams . app_id = router . params . app_id ;
205
+ // Update resource_type
206
+ newParams . resource_type = RESOURCE_TYPES [ newValue ] ;
207
+
208
+ // If switching to chat tab, remove app_id if present
209
+ if ( RESOURCE_TYPES [ newValue ] === 'chat' && newParams . app_id ) {
210
+ delete newParams . app_id ;
169
211
}
170
212
171
- // Direct navigation without animation for tab changes
172
- // We don't want slide animations between tabs as they're in the same component
173
- router . mergeParams ( newParams ) ;
213
+ console . log ( '[SIDEBAR HANDLER] About to navigate with params:' , newParams )
214
+
215
+ // Use a more forceful navigation method instead of just merging params
216
+ // This will trigger a full route change
217
+ router . navigate ( router . name , newParams ) ;
174
218
}
175
219
176
220
// Handle creating new chat or app based on active tab
0 commit comments