11import { ChevronDown , Loader2 , PlusIcon } from "lucide-react" ;
22import Image from "next/image" ;
3+ import { usePathname } from "next/navigation" ;
34import type React from "react" ;
45import { useEffect , useRef , useState } from "react" ;
56import {
@@ -26,12 +27,36 @@ const PersonaSelector: React.FC<PersonaSelectorProps> = ({
2627 onPersonaChange,
2728 disabled = false ,
2829} ) => {
29- const { selectedPersona : selectedPersonaId , setSelectedPersona } = useApp ( ) ;
30+ const {
31+ selectedPersona : selectedPersonaId ,
32+ setSelectedPersona,
33+ chatHistory,
34+ } = useApp ( ) ;
3035 const { createChat, isCreatingChat } = useCreateChat ( ) ;
3136 const [ isOpen , setIsOpen ] = useState ( false ) ;
37+ const pathname = usePathname ( ) ;
3238
39+ // Get current chat ID from pathname
40+ const currentChatId = pathname ?. match ( / \/ a p p \/ c h a t \/ ( .+ ) / ) ?. [ 1 ] ;
41+
42+ // Determine which persona to show
43+ const getCurrentPersona = ( ) => {
44+ // If we're in an existing chat, find the current chat's persona
45+ if ( currentChatId ) {
46+ const currentChat = chatHistory . find (
47+ ( chat ) => chat . _id === currentChatId ,
48+ ) ;
49+ if ( currentChat ?. persona ) {
50+ return currentChat . persona ;
51+ }
52+ }
53+ // Fall back to the selected persona from AppContext (for new chats)
54+ return selectedPersonaId ;
55+ } ;
56+
57+ const currentPersonaId = getCurrentPersona ( ) ;
3358 const selectedPersona =
34- personas . find ( ( persona ) => persona . id === selectedPersonaId ) || personas [ 0 ] ;
59+ personas . find ( ( persona ) => persona . id === currentPersonaId ) || personas [ 0 ] ;
3560 const dropdownRef = useRef < HTMLDivElement > ( null ) ;
3661
3762 useEffect ( ( ) => {
@@ -112,7 +137,7 @@ const PersonaSelector: React.FC<PersonaSelectorProps> = ({
112137 < button
113138 key = { persona . id }
114139 className = { `block w-full text-left px-4 py-3 text-sm hover:bg-neutral-50 ${
115- selectedPersona . id === persona . id
140+ currentPersonaId === persona . id
116141 ? "text-neutral-900 bg-neutral-50"
117142 : "text-neutral-700"
118143 } `}
@@ -125,7 +150,7 @@ const PersonaSelector: React.FC<PersonaSelectorProps> = ({
125150 { persona . description }
126151 </ div >
127152 </ div >
128- { selectedPersona . id === persona . id && (
153+ { currentPersonaId === persona . id && (
129154 < Image
130155 src = "/img/tick_icon.svg"
131156 alt = "Selected"
0 commit comments