fix(kb): auto-refresh documents after connector sync and remove redundant invalidations (#3601)

This commit is contained in:
Waleed
2026-03-15 04:24:06 -07:00
committed by GitHub
parent 8906439a7e
commit aa1f60540d
4 changed files with 20 additions and 44 deletions
@@ -2,7 +2,6 @@
import { startTransition, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { createLogger } from '@sim/logger'
import { useQueryClient } from '@tanstack/react-query'
import { ChevronDown, ChevronUp, FileText, Pencil, Tag } from 'lucide-react'
import { useParams, useRouter, useSearchParams } from 'next/navigation'
import {
@@ -41,7 +40,6 @@ import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/provide
import { useContextMenu } from '@/app/workspace/[workspaceId]/w/components/sidebar/hooks'
import { useDocument, useDocumentChunks, useKnowledgeBase } from '@/hooks/kb/use-knowledge'
import {
knowledgeKeys,
useBulkChunkOperation,
useDeleteDocument,
useDocumentChunkSearchQuery,
@@ -137,7 +135,6 @@ export function Document({
knowledgeBaseName,
documentName,
}: DocumentProps) {
const queryClient = useQueryClient()
const { workspaceId } = useParams()
const router = useRouter()
const searchParams = useSearchParams()
@@ -704,9 +701,7 @@ export function Document({
},
{
onSuccess: (result) => {
if (operation === 'delete' || result.errorCount > 0) {
refreshChunks()
} else {
if (operation !== 'delete' && result.errorCount === 0) {
chunks.forEach((chunk) => {
updateChunk(chunk.id, { enabled: operation === 'enable' })
})
@@ -789,12 +784,6 @@ export function Document({
setContextMenuChunk(null)
}, [closeContextMenu])
const handleDocumentTagsUpdate = useCallback(() => {
queryClient.invalidateQueries({
queryKey: knowledgeKeys.document(knowledgeBaseId, documentId),
})
}, [knowledgeBaseId, documentId, queryClient])
const prevDocumentIdRef = useRef<string>(documentId)
const isNavigatingToNewDoc = prevDocumentIdRef.current !== documentId
@@ -1121,7 +1110,6 @@ export function Document({
knowledgeBaseId={knowledgeBaseId}
documentId={documentId}
documentData={documentData}
onDocumentUpdate={handleDocumentTagsUpdate}
/>
<DeleteChunkModal
@@ -271,6 +271,13 @@ export function KnowledgeBase({
refresh: refreshKnowledgeBase,
} = useKnowledgeBase(id)
const { data: connectors = [], isLoading: isLoadingConnectors } = useConnectorList(id)
const hasSyncingConnectors = connectors.some((c) => c.status === 'syncing')
const hasSyncingConnectorsRef = useRef(hasSyncingConnectors)
useEffect(() => {
hasSyncingConnectorsRef.current = hasSyncingConnectors
}, [hasSyncingConnectors])
const {
documents,
pagination,
@@ -292,7 +299,9 @@ export function KnowledgeBase({
const hasPending = data?.documents?.some(
(doc) => doc.processingStatus === 'pending' || doc.processingStatus === 'processing'
)
return hasPending ? 3000 : false
if (hasPending) return 3000
if (hasSyncingConnectorsRef.current) return 5000
return false
},
enabledFilter,
tagFilters: activeTagFilters.length > 0 ? activeTagFilters : undefined,
@@ -300,16 +309,14 @@ export function KnowledgeBase({
const { tagDefinitions } = useKnowledgeBaseTagDefinitions(id)
const { data: connectors = [], isLoading: isLoadingConnectors } = useConnectorList(id)
const hasSyncingConnectors = connectors.some((c) => c.status === 'syncing')
const prevHadSyncingRef = useRef(false)
useEffect(() => {
if (prevHadSyncingRef.current && !hasSyncingConnectors) {
refreshKnowledgeBase()
refreshDocuments()
}
prevHadSyncingRef.current = hasSyncingConnectors
}, [hasSyncingConnectors, refreshKnowledgeBase])
}, [hasSyncingConnectors, refreshKnowledgeBase, refreshDocuments])
const router = useRouter()
@@ -406,7 +413,6 @@ export function KnowledgeBase({
},
{
onSuccess: () => {
refreshDocuments()
logger.info(`Document retry initiated successfully for: ${docId}`)
},
onError: (err) => {
@@ -480,7 +486,6 @@ export function KnowledgeBase({
{ knowledgeBaseId: id, documentId: documentToDelete },
{
onSuccess: () => {
refreshDocuments()
setSelectedDocuments((prev) => {
const newSet = new Set(prev)
newSet.delete(documentToDelete)
@@ -575,7 +580,6 @@ export function KnowledgeBase({
logger.info(`Successfully enabled ${result.successCount} documents`)
setSelectedDocuments(new Set())
setIsSelectAllMode(false)
refreshDocuments()
},
}
)
@@ -623,7 +627,6 @@ export function KnowledgeBase({
logger.info(`Successfully disabled ${result.successCount} documents`)
setSelectedDocuments(new Set())
setIsSelectAllMode(false)
refreshDocuments()
},
}
)
@@ -671,7 +674,6 @@ export function KnowledgeBase({
{
onSuccess: (result) => {
logger.info(`Successfully deleted ${result.successCount} documents`)
refreshDocuments()
setSelectedDocuments(new Set())
setIsSelectAllMode(false)
},
@@ -696,7 +698,6 @@ export function KnowledgeBase({
{
onSuccess: (result) => {
logger.info(`Successfully deleted ${result.successCount} documents`)
refreshDocuments()
setSelectedDocuments(new Set())
},
onSettled: () => {
@@ -91,8 +91,7 @@ interface BaseTagsModalProps {
}
export function BaseTagsModal({ open, onOpenChange, knowledgeBaseId }: BaseTagsModalProps) {
const { tagDefinitions: kbTagDefinitions, fetchTagDefinitions: refreshTagDefinitions } =
useKnowledgeBaseTagDefinitions(knowledgeBaseId)
const { tagDefinitions: kbTagDefinitions } = useKnowledgeBaseTagDefinitions(knowledgeBaseId)
const createTagMutation = useCreateTagDefinition()
const deleteTagMutation = useDeleteTagDefinition()
@@ -219,7 +218,7 @@ export function BaseTagsModal({ open, onOpenChange, knowledgeBaseId }: BaseTagsM
fieldType: createTagForm.fieldType,
})
await Promise.all([refreshTagDefinitions(), fetchTagUsage()])
await fetchTagUsage()
setCreateTagForm({
displayName: '',
@@ -240,7 +239,7 @@ export function BaseTagsModal({ open, onOpenChange, knowledgeBaseId }: BaseTagsM
tagDefinitionId: selectedTag.id,
})
await Promise.all([refreshTagDefinitions(), fetchTagUsage()])
await fetchTagUsage()
setDeleteTagDialogOpen(false)
setSelectedTag(null)
+4 -16
View File
@@ -153,7 +153,7 @@ export function useCreateConnector() {
mutationFn: createConnector,
onSuccess: (_, { knowledgeBaseId }) => {
queryClient.invalidateQueries({
queryKey: connectorKeys.list(knowledgeBaseId),
queryKey: knowledgeKeys.detail(knowledgeBaseId),
})
},
})
@@ -198,12 +198,9 @@ export function useUpdateConnector() {
return useMutation({
mutationFn: updateConnector,
onSuccess: (_, { knowledgeBaseId, connectorId }) => {
onSuccess: (_, { knowledgeBaseId }) => {
queryClient.invalidateQueries({
queryKey: connectorKeys.list(knowledgeBaseId),
})
queryClient.invalidateQueries({
queryKey: connectorKeys.detail(knowledgeBaseId, connectorId),
queryKey: connectorKeys.all(knowledgeBaseId),
})
},
})
@@ -239,9 +236,6 @@ export function useDeleteConnector() {
return useMutation({
mutationFn: deleteConnector,
onSuccess: (_, { knowledgeBaseId }) => {
queryClient.invalidateQueries({
queryKey: connectorKeys.list(knowledgeBaseId),
})
queryClient.invalidateQueries({
queryKey: knowledgeKeys.detail(knowledgeBaseId),
})
@@ -270,13 +264,7 @@ export function useTriggerSync() {
return useMutation({
mutationFn: triggerSync,
onSuccess: (_, { knowledgeBaseId, connectorId }) => {
queryClient.invalidateQueries({
queryKey: connectorKeys.list(knowledgeBaseId),
})
queryClient.invalidateQueries({
queryKey: connectorKeys.detail(knowledgeBaseId, connectorId),
})
onSuccess: (_, { knowledgeBaseId }) => {
queryClient.invalidateQueries({
queryKey: knowledgeKeys.detail(knowledgeBaseId),
})