Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions web/src/store/modules/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface SessionState {
isLoading: boolean
isCreatingSession: boolean
isSwitchingSession: boolean
isNavigating: boolean
}

export const useSessionStore = defineStore('session-store', {
Expand All @@ -25,6 +26,7 @@ export const useSessionStore = defineStore('session-store', {
isLoading: false,
isCreatingSession: false,
isSwitchingSession: false,
isNavigating: false,
}),

getters: {
Expand Down Expand Up @@ -372,33 +374,45 @@ export const useSessionStore = defineStore('session-store', {
},

async navigateToSession(sessionUuid: string) {
const session = this.getChatSessionByUuid(sessionUuid)
if (session && session.workspaceUuid) {
// Check if we're already on the correct route to avoid unnecessary navigation
const currentRoute = router.currentRoute.value
const currentWorkspaceUuid = currentRoute.params.workspaceUuid as string
const currentSessionUuid = currentRoute.params.uuid as string

if (currentWorkspaceUuid === session.workspaceUuid && currentSessionUuid === sessionUuid) {
console.log('Already on correct route, skipping navigation')
return
}
// Prevent overlapping navigation calls
if (this.isNavigating) {
console.log('Navigation already in progress, skipping')
return
}

const workspaceStore = useWorkspaceStore()
workspaceStore.navigateToWorkspace(session.workspaceUuid, sessionUuid)
} else {
// If session doesn't have a workspace, try to assign it to the default workspace
const workspaceStore = useWorkspaceStore()
const defaultWorkspace = workspaceStore.workspaces.find(w => w.isDefault) || workspaceStore.workspaces[0]
this.isNavigating = true

try {
const session = this.getChatSessionByUuid(sessionUuid)
if (session && session.workspaceUuid) {
// Check if we're already on the correct route to avoid unnecessary navigation
const currentRoute = router.currentRoute.value
const currentWorkspaceUuid = currentRoute.params.workspaceUuid as string
const currentSessionUuid = currentRoute.params.uuid as string

if (currentWorkspaceUuid === session.workspaceUuid && currentSessionUuid === sessionUuid) {
console.log('Already on correct route, skipping navigation')
return
}

if (defaultWorkspace) {
console.log('Session without workspace, navigating to default workspace:', defaultWorkspace.uuid)
workspaceStore.navigateToWorkspace(defaultWorkspace.uuid, sessionUuid)
const workspaceStore = useWorkspaceStore()
await workspaceStore.navigateToWorkspace(session.workspaceUuid, sessionUuid)
} else {
// Last resort: navigate to default route
console.log('No workspace available, navigating to default route')
await router.push({ name: 'DefaultWorkspace' })
// If session doesn't have a workspace, try to assign it to the default workspace
const workspaceStore = useWorkspaceStore()
const defaultWorkspace = workspaceStore.workspaces.find(w => w.isDefault) || workspaceStore.workspaces[0]

if (defaultWorkspace) {
console.log('Session without workspace, navigating to default workspace:', defaultWorkspace.uuid)
await workspaceStore.navigateToWorkspace(defaultWorkspace.uuid, sessionUuid)
} else {
// Last resort: navigate to default route
console.log('No workspace available, navigating to default route')
await router.push({ name: 'DefaultWorkspace' })
}
}
} finally {
this.isNavigating = false
}
},

Expand Down
14 changes: 8 additions & 6 deletions web/src/store/modules/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ export const useWorkspaceStore = defineStore('workspace-store', {
delete this.workspaceActiveSessions[workspaceUuid]
},

navigateToWorkspace(workspaceUuid: string, sessionUuid?: string) {
async navigateToWorkspace(workspaceUuid: string, sessionUuid?: string) {
// Check if we're already on the target route to avoid unnecessary navigation
const currentRoute = router.currentRoute.value
const currentWorkspaceUuid = currentRoute.params.workspaceUuid as string
Expand All @@ -590,17 +590,19 @@ export const useWorkspaceStore = defineStore('workspace-store', {
targetSessionUuid = sessionStore.activeSessionUuid || undefined
}

// Check if we're already on the correct route
if (currentWorkspaceUuid === workspaceUuid &&
(!targetSessionUuid || currentSessionUuid === targetSessionUuid)) {
console.log('Already on target workspace route, skipping navigation')
return Promise.resolve()
// More thorough route checking - only skip if route params match exactly
if (currentRoute.name === 'WorkspaceChat' &&
currentWorkspaceUuid === workspaceUuid &&
currentSessionUuid === targetSessionUuid) {
console.log('Already on exact target route, skipping navigation')
return
}

const route = targetSessionUuid
? { name: 'WorkspaceChat', params: { workspaceUuid, uuid: targetSessionUuid } }
: { name: 'WorkspaceChat', params: { workspaceUuid } }

console.log('Navigating to:', route)
return router.push(route)
},
},
Expand Down
24 changes: 14 additions & 10 deletions web/src/views/chat/layout/sider/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,28 @@ async function handleSelect(uuid: string) {
if (sessionStore.isSwitchingSession)
return

const session = sessionStore.getChatSessionByUuid(uuid)
if (session && session.workspaceUuid) {
await sessionStore.setActiveSession(session.workspaceUuid, uuid)
}
try {
const session = sessionStore.getChatSessionByUuid(uuid)
if (session && session.workspaceUuid) {
await sessionStore.setActiveSession(session.workspaceUuid, uuid)
}

if (isMobile.value)
appStore.setSiderCollapsed(true)
if (isMobile.value)
appStore.setSiderCollapsed(true)
} catch (error) {
console.error('Error handling session select:', error)
}
}

// throttle handleSelect
// async function handleSelectThrottle() {
// throttle(async ({ uuid }: Chat.Session) => await handleSelect(uuid), 500)
// }

// Create a wrapper to debounce the handleSelect function
const throttledHandleSelect = throttle((uuid) => {
handleSelect(uuid);
}, 500); // 300ms debounce time
// Create a wrapper to throttle the handleSelect function
const throttledHandleSelect = throttle(async (uuid) => {
await handleSelect(uuid);
}, 300); // 300ms throttle time

function handleEdit({ uuid }: Chat.Session, isEdit: boolean, event?: MouseEvent) {
event?.stopPropagation()
Expand Down