Skip to content

Commit 8568b80

Browse files
Fix navigation through sessions and subpages (#11)
- Fix for navigation between open sessions and new sessions. - If you change to the knowledge page and go back, the previous session is reopened.
1 parent 54ddc4e commit 8568b80

9 files changed

Lines changed: 42 additions & 27 deletions

File tree

src/hooks/useChatMessages.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ export default function useChatMessages(): UseChatMessagesResponse {
4444
useEffect(() => {
4545
const resetChatMessages = async () => {
4646
// Reset chat messages when sessionID changes
47-
if (
48-
activeSessionIdRef.current !== previousSessionId.current &&
49-
!activeSessionIsNew
50-
) {
47+
if (!activeSessionIsNew) {
5148
setLoadingResponse(false);
5249
setError(null);
5350
setCurrentChatMessages([]);

src/lib/navigateToSession.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NavigateFunction } from "react-router";
2+
3+
export function navigateToSession(
4+
sessionId: string | null,
5+
navigate: NavigateFunction,
6+
) {
7+
if (sessionId === null) void navigate("/");
8+
else void navigate(`/?session=${sessionId}`);
9+
}

src/locales/de/translation.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"deleteSessionSuccessful": "Die Session wurde erfolgreich gelöscht",
1818
"errorModelNotAvailable": "Das ausgewählte Modell ist nicht mehr verfügbar",
1919
"newSession": "Erzeuge neue Session",
20-
"newSessionCreate": "Neue Sitzung erstellt",
2120
"noSessionLabel": "Keine Session Label verfügbar",
2221
"chat": {
2322
"newChat": {

src/locales/en/translation.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"deleteSessionSuccessful": "The session was successfully deleted",
1818
"errorModelNotAvailable": "The selected model is no longer available",
1919
"newSession": "Create new session",
20-
"newSessionCreate": "New session created",
2120
"noSessionLabel": "No session label available",
2221
"chat": {
2322
"newChat": {

src/services/GetSessionsService.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ import { useToast } from "@/hooks/use-toast";
1212
import { default as useRefState } from "react-usestateref";
1313
import useCurrentPage from "@/hooks/useCurrentPage";
1414
import { useNavigate } from "react-router";
15+
import { navigateToSession } from "@/lib/navigateToSession";
1516

1617
export interface UseGetSessionsApiResponse {
1718
sessions: Session[];
1819
activeSessionId: string | null;
1920
activeSessionIdRef: React.MutableRefObject<string | null>;
2021
activeSessionIsNew: boolean;
21-
setActiveSessionId: (sessionId: string | null, isNew?: boolean) => void;
22+
setActiveSessionId: (
23+
sessionId: string | null,
24+
isNew?: boolean,
25+
redirect?: boolean,
26+
) => void;
2227
appendSessionLocal: (session: Session | null) => void;
2328
updateSessionLabelLocal: (sessionId: string, newLabel: string) => void;
2429
deleteSessionLocal: (sessionId: string) => void;
@@ -109,9 +114,17 @@ export default function GetSessionsProvider({
109114
);
110115
};
111116

112-
const setActiveSessionIdFunc = (sessionId: string | null, isNew = false) => {
117+
// Set active session id and update URL with the new selected session
118+
const setActiveSessionIdFunc = (
119+
sessionId: string | null,
120+
isNew = false,
121+
redirect = true,
122+
) => {
113123
activeSessionIsNewRef.current = isNew;
114124
setActiveSessionId(sessionId);
125+
if (redirect) {
126+
navigateToSession(sessionId, navigate);
127+
}
115128
};
116129

117130
const value = useMemo(

src/views/chatbot/Chatbot.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ export default function Chatbot() {
3838
handlePromptAndMessages,
3939
} = useChatMessages();
4040

41+
// Set active session id from URL at load time
4142
useEffect(() => {
4243
const queryParams = new URLSearchParams(location.search);
4344
const sessionId = queryParams.get("session");
4445
setPdfKnowledgeId(null);
4546
setPdfFileName(null);
4647
setActiveSessionId(sessionId);
47-
}, [location]);
48+
}, []);
4849

4950
// collapse sidebar if PDF is opened and screen is resized to a smaller size
5051
useEffect(() => {

src/views/overlays/Overlay.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
import { useTranslation } from "react-i18next";
21
import Header from "./Header";
32
import Sidebar from "./sidebar/Sidebar";
4-
import { useToast } from "@/hooks/use-toast";
5-
import { useNavigate } from "react-router";
63
import Footer from "./Footer";
74
import { useRef } from "react";
85
import useElementSize from "@/hooks/useElementSize.ts";
6+
import { useGetSessions } from "@/services/GetSessionsService";
97

108
interface OverlayProps {
119
children: React.ReactNode;
1210
}
1311

1412
export function Overlay({ children }: Readonly<OverlayProps>) {
15-
const navigate = useNavigate();
16-
const { toast } = useToast();
17-
const { t } = useTranslation();
1813
const headerRef = useRef<HTMLDivElement>(null);
1914
const footerRef = useRef<HTMLDivElement>(null);
2015
const headerSize = useElementSize(headerRef);
2116
const footerSize = useElementSize(footerRef);
17+
const { setActiveSessionId } = useGetSessions();
2218

23-
const onClickNewSession = async () => {
24-
await navigate("/", { replace: true }); // Remove session id from URL
25-
toast({
26-
variant: "default",
27-
title: t("chatbot.newSessionCreate"),
28-
});
19+
const onClickNewSession = () => {
20+
setActiveSessionId(null);
2921
};
3022

3123
return (
3224
<Sidebar>
3325
<div ref={headerRef} className="sticky top-0 z-40 ">
34-
<Header onClickNewSession={() => void onClickNewSession()} />
26+
<Header
27+
onClickNewSession={() => {
28+
onClickNewSession();
29+
}}
30+
/>
3531
</div>
3632
<div
3733
className="mx-4"

src/views/overlays/sidebar/SidebarContent.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { SidebarSessionsGroup, SidebarSessionList } from "./SidebarSessionList";
1515
import { SidebarModelSelector as ChatbotSidebarHeader } from "./SidebarModelSelector";
1616
import { useToast } from "@/hooks/use-toast";
1717
import { groupSessionsByDateForNavbar } from "@/lib/groupSessionsByDateForNavbar";
18-
import { useNavigate } from "react-router";
1918
import { useUserContext } from "@/services/UserContextService";
2019
import { useGetSessions } from "@/services/GetSessionsService";
2120
import LoadingAnimation from "@/components/loading-animation";
@@ -101,11 +100,10 @@ const GetSessions = ({
101100
}) => {
102101
const { t } = useTranslation();
103102
const { user } = useUserContext();
104-
105-
const navigate = useNavigate();
103+
const { setActiveSessionId } = useGetSessions();
106104

107105
const onSelectSession = (sessionId: string) => {
108-
void navigate(`/?session=${sessionId}`);
106+
setActiveSessionId(sessionId);
109107
};
110108

111109
return (

src/views/overlays/sidebar/SidebarMainNav.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
useSidebar,
77
} from "@/components/ui/sidebar";
88
import useCurrentPage from "@/hooks/useCurrentPage";
9+
import { navigateToSession } from "@/lib/navigateToSession";
10+
import { useGetSessions } from "@/services/GetSessionsService";
911
import { useUserContext } from "@/services/UserContextService";
1012
import { t } from "i18next";
1113
import { Bot, HardDriveUpload } from "lucide-react";
@@ -16,14 +18,15 @@ export default function SidebarMainNav() {
1618
const { open } = useSidebar();
1719
const navigate = useNavigate();
1820
const { user } = useUserContext();
21+
const { activeSessionId } = useGetSessions();
1922

2023
return (
2124
<SidebarGroup className="pt-4">
2225
<SidebarMenu>
2326
<SidebarMenuItem>
2427
<SidebarMenuButton
2528
onClick={() => {
26-
void navigate("/");
29+
navigateToSession(activeSessionId, navigate);
2730
}}
2831
className={activePage === "chatbot" ? "bg-secondary" : ""}
2932
>

0 commit comments

Comments
 (0)