@@ -4,7 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
44import { createLogger } from '@sim/logger'
55import { format } from 'date-fns'
66import { AlertCircle , Loader2 , Pencil , Plus , Tag , X } from 'lucide-react'
7- import { useParams , useRouter } from 'next/navigation'
7+ import { useParams , usePathname , useRouter , useSearchParams } from 'next/navigation'
88import { usePostHog } from 'posthog-js/react'
99import {
1010 Badge ,
@@ -25,6 +25,7 @@ import {
2525import { Database , DatabaseX } from '@/components/emcn/icons'
2626import { SearchHighlight } from '@/components/ui/search-highlight'
2727import { cn } from '@/lib/core/utils/cn'
28+ import { ADD_CONNECTOR_SEARCH_PARAM } from '@/lib/credentials/client-state'
2829import { ALL_TAG_SLOTS , type AllTagSlot , getFieldTypeForSlot } from '@/lib/knowledge/constants'
2930import type { DocumentSortField , SortOrder } from '@/lib/knowledge/documents/types'
3031import { type FilterFieldType , getOperatorsForFieldType } from '@/lib/knowledge/filters/types'
@@ -192,6 +193,10 @@ export function KnowledgeBase({
192193} : KnowledgeBaseProps ) {
193194 const params = useParams ( )
194195 const workspaceId = propWorkspaceId || ( params . workspaceId as string )
196+ const router = useRouter ( )
197+ const searchParams = useSearchParams ( )
198+ const pathname = usePathname ( )
199+ const addConnectorParam = searchParams . get ( ADD_CONNECTOR_SEARCH_PARAM )
195200 const posthog = usePostHog ( )
196201
197202 useEffect ( ( ) => {
@@ -278,7 +283,29 @@ export function KnowledgeBase({
278283 const [ contextMenuDocument , setContextMenuDocument ] = useState < DocumentData | null > ( null )
279284 const [ showRenameModal , setShowRenameModal ] = useState ( false )
280285 const [ documentToRename , setDocumentToRename ] = useState < DocumentData | null > ( null )
281- const [ showAddConnectorModal , setShowAddConnectorModal ] = useState ( false )
286+ const showAddConnectorModal = addConnectorParam != null
287+ const searchParamsRef = useRef ( searchParams )
288+ searchParamsRef . current = searchParams
289+ const updateAddConnectorParam = useCallback (
290+ ( value : string | null ) => {
291+ const current = searchParamsRef . current
292+ const currentValue = current . get ( ADD_CONNECTOR_SEARCH_PARAM )
293+ if ( value === currentValue || ( value === null && currentValue === null ) ) return
294+ const next = new URLSearchParams ( current . toString ( ) )
295+ if ( value === null ) {
296+ next . delete ( ADD_CONNECTOR_SEARCH_PARAM )
297+ } else {
298+ next . set ( ADD_CONNECTOR_SEARCH_PARAM , value )
299+ }
300+ const qs = next . toString ( )
301+ router . replace ( qs ? `${ pathname } ?${ qs } ` : pathname , { scroll : false } )
302+ } ,
303+ [ pathname , router ]
304+ )
305+ const setShowAddConnectorModal = useCallback (
306+ ( open : boolean ) => updateAddConnectorParam ( open ? '' : null ) ,
307+ [ updateAddConnectorParam ]
308+ )
282309
283310 const {
284311 isOpen : isContextMenuOpen ,
@@ -340,8 +367,6 @@ export function KnowledgeBase({
340367 prevHadSyncingRef . current = hasSyncingConnectors
341368 } , [ hasSyncingConnectors , refreshKnowledgeBase , refreshDocuments ] )
342369
343- const router = useRouter ( )
344-
345370 const knowledgeBaseName = knowledgeBase ?. name || passedKnowledgeBaseName || 'Knowledge Base'
346371 const error = knowledgeBaseError || documentsError
347372
@@ -1254,7 +1279,13 @@ export function KnowledgeBase({
12541279 />
12551280
12561281 { showAddConnectorModal && (
1257- < AddConnectorModal open onOpenChange = { setShowAddConnectorModal } knowledgeBaseId = { id } />
1282+ < AddConnectorModal
1283+ open
1284+ onOpenChange = { setShowAddConnectorModal }
1285+ onConnectorTypeChange = { updateAddConnectorParam }
1286+ knowledgeBaseId = { id }
1287+ initialConnectorType = { addConnectorParam || undefined }
1288+ />
12581289 ) }
12591290
12601291 { documentToRename && (
0 commit comments