Skip to content

Commit e8ccf56

Browse files
authored
Merge pull request #934 from helixml/fix/nav-loading
tighten up nav loading and showing the right items (chats/apps) at all times
2 parents 73ec695 + 17d501f commit e8ccf56

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

frontend/src/components/apps/AppsMenu.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC } from 'react'
1+
import React, { FC, useEffect } from 'react'
22
import List from '@mui/material/List'
33
import ListItem from '@mui/material/ListItem'
44
import ListItemButton from '@mui/material/ListItemButton'
@@ -25,14 +25,19 @@ export const AppsMenu: FC<{
2525
}> = ({
2626
onOpenApp,
2727
}) => {
28-
const { apps } = useApps()
28+
const { apps, loadApps } = useApps()
2929
const account = useAccount()
3030
const lightTheme = useLightTheme()
3131
const {
3232
navigate,
3333
params,
3434
} = useRouter()
3535

36+
// Load apps when component mounts
37+
useEffect(() => {
38+
loadApps()
39+
}, [])
40+
3641
// Helper function to get the icon for an app
3742
const getAppIcon = (app: IApp) => {
3843
// Use the app's avatar if available

frontend/src/components/system/Sidebar.tsx

+53-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useContext, useMemo } from 'react'
1+
import React, { useState, useContext, useMemo, useEffect } from 'react'
22
import Button from '@mui/material/Button'
33
import Box from '@mui/material/Box'
44
import Typography from '@mui/material/Typography'
@@ -35,6 +35,7 @@ import useLightTheme from '../../hooks/useLightTheme'
3535
import useRouter from '../../hooks/useRouter'
3636
import useAccount from '../../hooks/useAccount'
3737
import useApps from '../../hooks/useApps'
38+
import useSessions from '../../hooks/useSessions'
3839
import { AccountContext } from '../../contexts/account'
3940
import SlideMenuContainer, { triggerMenuChange } from './SlideMenuContainer'
4041

@@ -61,6 +62,7 @@ const SidebarContent: React.FC<{
6162
const router = useRouter()
6263
const account = useAccount()
6364
const apps = useApps()
65+
const sessions = useSessions()
6466
const { models } = useContext(AccountContext)
6567
const activeTab = useMemo(() => {
6668
// Always respect resource_type if it's present
@@ -76,6 +78,40 @@ const SidebarContent: React.FC<{
7678
router.params,
7779
])
7880

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+
79115
const filteredModels = useMemo(() => {
80116
return models.filter(m => m.type === "text" || m.type === "chat")
81117
}, [models])
@@ -158,19 +194,27 @@ const SidebarContent: React.FC<{
158194
const fromResourceType = RESOURCE_TYPES[activeTab]
159195
const toResourceType = RESOURCE_TYPES[newValue]
160196

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
162201
const newParams: Record<string, any> = {
163-
resource_type: RESOURCE_TYPES[newValue],
202+
...router.params
164203
};
165204

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;
169211
}
170212

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);
174218
}
175219

176220
// Handle creating new chat or app based on active tab

frontend/src/pages/Layout.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import useAccount from '../hooks/useAccount'
2222
import useLightTheme from '../hooks/useLightTheme'
2323
import useThemeConfig from '../hooks/useThemeConfig'
2424
import useIsBigScreen from '../hooks/useIsBigScreen'
25+
import useApps from '../hooks/useApps'
2526

2627
const Layout: FC = ({
2728
children
@@ -32,6 +33,7 @@ const Layout: FC = ({
3233
const isBigScreen = useIsBigScreen()
3334
const router = useRouter()
3435
const account = useAccount()
36+
const apps = useApps()
3537
const [showVersionBanner, setShowVersionBanner] = useState(true)
3638

3739
const hasNewVersion = useMemo(() => {
@@ -51,13 +53,27 @@ const Layout: FC = ({
5153

5254
let sidebarMenu = null
5355
const isOrgMenu = router.meta.menu == 'orgs'
54-
const resourceType = router.params.resource_type || 'chat'
56+
57+
// Determine which resource type to use
58+
// 1. Use resource_type from URL params if available
59+
// 2. If app_id is present in the URL, default to 'apps'
60+
// 3. Otherwise default to 'chat'
61+
const resourceType = router.params.resource_type || (router.params.app_id ? 'apps' : 'chat')
62+
63+
console.log('[LAYOUT] Router params:', router.params)
64+
console.log('[LAYOUT] Determined resource type:', resourceType)
5565

5666
// This useEffect handles registering/updating the menu
5767
React.useEffect(() => {
5868
// Store the current resource type for later use
5969
if (resourceType) {
6070
localStorage.setItem('last_resource_type', resourceType)
71+
console.log('[LAYOUT] Stored resource type:', resourceType)
72+
73+
// Ensure the appropriate content is loaded
74+
if (resourceType === 'apps') {
75+
apps.loadApps()
76+
}
6177
}
6278
}, [resourceType])
6379

0 commit comments

Comments
 (0)