@@ -5,11 +5,12 @@ import { Chip, ComposerActionButton, cn } from '@sim/emcn'
55import { ArrowUp , Plus , Search , Sparkles , StopFilled } from '@sim/emcn/icons'
66import { generateId } from '@sim/utils/id'
77import { useIsFetching , useQueryClient } from '@tanstack/react-query'
8- import { usePathname , useRouter } from 'next/navigation'
8+ import { usePathname , useRouter , useSearchParams } from 'next/navigation'
99import { useQueryStates } from 'nuqs'
1010import { HEADER_ACTION_CLUSTER } from '@/components/page-header-bar'
1111import type { WorkspaceSearchFilters } from '@/lib/api/contracts/knowledge'
1212import { resourceScopeKey } from '@/lib/core/resource-scope'
13+ import { BrowserStorage } from '@/lib/core/utils/browser-storage'
1314import { organizationRoutes } from '@/lib/navigation/paths'
1415import type { OrganizationSummaryRequest } from '@/app/o/[organizationId]/components/organization-conversation'
1516import { SearchInputBar } from '@/app/o/[organizationId]/components/search-input-bar'
@@ -166,17 +167,17 @@ function OrganizationSearchContent({ userId, chatId }: OrganizationSearchProps)
166167 const { recordQuery } = useSearchHistoryActions ( )
167168 const router = useRouter ( )
168169 const pathname = usePathname ( )
170+ const hasSearchParams = useSearchParams ( ) . size > 0
169171 const routes = organizationRoutes ( organization . id )
170172 const hasConversation = Boolean ( chatId ) || pathname . startsWith ( `${ routes . search } /chat/` )
171173 const queryClient = useQueryClient ( )
172174 const [ { q, mode } , setParams ] = useQueryStates (
173175 organizationSearchParsers ,
174176 organizationSearchUrlKeys
175177 )
176- const activeMode =
177- mothershipAvailable && ( mode ?? ( hasConversation ? 'assistant' : 'search' ) ) === 'assistant'
178- ? 'assistant'
179- : 'search'
178+ const routeMode = mode ?? ( hasConversation ? 'assistant' : hasSearchParams ? 'search' : null )
179+ const activeMode = mothershipAvailable ? routeMode : 'search'
180+ const preferenceKey = `organization-search-mode:${ userId } :${ organization . id } `
180181 const [ assistantOpened , setAssistantOpened ] = useState ( activeMode === 'assistant' )
181182 const [ conversationInstance , setConversationInstance ] = useState ( 0 )
182183 const [ isStartingNew , startNewTransition ] = useTransition ( )
@@ -185,8 +186,20 @@ function OrganizationSearchContent({ userId, chatId }: OrganizationSearchProps)
185186 ( id : string ) => setSummaries ( ( current ) => current . filter ( ( request ) => request . id !== id ) ) ,
186187 [ ]
187188 )
189+ useEffect ( ( ) => {
190+ if ( ! mothershipAvailable ) return
191+ if ( routeMode ) {
192+ BrowserStorage . setItem ( preferenceKey , routeMode )
193+ return
194+ }
195+ const savedMode = BrowserStorage . getItem < unknown > ( preferenceKey , null )
196+ // Pin the mode to this history entry so Back survives a later preference change.
197+ void setParams ( { mode : savedMode === 'assistant' ? 'assistant' : 'search' } )
198+ } , [ mothershipAvailable , preferenceKey , routeMode , setParams ] )
199+
200+ if ( activeMode === 'assistant' && ! assistantOpened ) setAssistantOpened ( true )
201+
188202 const changeMode = ( next : 'search' | 'assistant' ) => {
189- if ( next === 'assistant' ) setAssistantOpened ( true )
190203 void setParams ( { mode : next } , { history : 'push' } )
191204 }
192205 const startNew = ( ) => {
@@ -201,7 +214,7 @@ function OrganizationSearchContent({ userId, chatId }: OrganizationSearchProps)
201214 }
202215 startNewTransition ( ( ) => {
203216 setConversationInstance ( ( current ) => current + 1 )
204- router . push ( activeMode === 'assistant' ? `${ routes . search } ?mode=assistant ` : routes . search , {
217+ router . push ( mothershipAvailable ? `${ routes . search } ?mode=${ activeMode } ` : routes . search , {
205218 scroll : false ,
206219 } )
207220 } )
@@ -263,28 +276,28 @@ function OrganizationSearchContent({ userId, chatId }: OrganizationSearchProps)
263276 return (
264277 < div className = 'h-full min-h-0 min-w-0 bg-[var(--bg)] pt-[var(--workspace-content-title-bar-inset)]' >
265278 < div className = 'flex h-full min-h-0 min-w-0 flex-col [--workspace-content-title-bar-inset:0px]' >
266- { ( assistantOpened || hasConversation || activeMode === 'assistant' ) &&
267- mothershipAvailable && (
268- < div
269- inert = { activeMode !== 'assistant' }
270- className = { cn (
271- 'min-h-0 min-w-0 flex-1 flex-col' ,
272- activeMode === 'assistant' ? 'flex' : 'hidden'
273- ) }
274- >
275- < Suspense fallback = { < HomeFallback header = { header } /> } >
276- < OrganizationConversation
277- key = { conversationInstance }
278- requestMode = 'assistant'
279- chatId = { chatId }
280- header = { header }
281- active = { activeMode === 'assistant' }
282- summaryRequest = { summaries [ 0 ] }
283- onSummaryConsumed = { consumeSummary }
284- / >
285- </ Suspense >
286- </ div >
287- ) }
279+ { ( assistantOpened || hasConversation ) && mothershipAvailable && (
280+ < div
281+ inert = { activeMode !== 'assistant' }
282+ className = { cn (
283+ 'min-h-0 min-w-0 flex-1 flex-col' ,
284+ activeMode === 'assistant' ? ' flex' : 'hidden'
285+ ) }
286+ >
287+ < Suspense fallback = { < HomeFallback header = { header } /> } >
288+ < OrganizationConversation
289+ key = { conversationInstance }
290+ requestMode = 'assistant'
291+ chatId = { chatId }
292+ header = { header }
293+ active = { activeMode === 'assistant' }
294+ summaryRequest = { summaries [ 0 ] }
295+ onSummaryConsumed = { consumeSummary }
296+ />
297+ </ Suspense >
298+ </ div >
299+ ) }
300+ { ! activeMode && < HomeFallback /> }
288301 { activeMode === 'search' && (
289302 < >
290303 < div className = { cn ( RESOURCE_HEADER_CLASSES . layout , RESOURCE_HEADER_CLASSES . content ) } >
0 commit comments