11'use client'
22
3- import { useCallback , useEffect , useMemo , useState } from 'react'
3+ import { useCallback , useEffect , useState } from 'react'
44import { Check , ChevronDown , FileText } from 'lucide-react'
55import { Button } from '@/components/ui/button'
66import {
@@ -13,7 +13,6 @@ import {
1313} from '@/components/ui/command'
1414import { Popover , PopoverContent , PopoverTrigger } from '@/components/ui/popover'
1515import type { SubBlockConfig } from '@/blocks/types'
16- import { useSubBlockStore } from '@/stores/workflows/subblock/store'
1716import { useSubBlockValue } from '../../hooks/use-sub-block-value'
1817
1918interface DocumentData {
@@ -51,19 +50,16 @@ export function DocumentSelector({
5150 isPreview = false ,
5251 previewValue,
5352} : DocumentSelectorProps ) {
54- const { getValue } = useSubBlockStore ( )
55-
5653 const [ documents , setDocuments ] = useState < DocumentData [ ] > ( [ ] )
5754 const [ error , setError ] = useState < string | null > ( null )
5855 const [ open , setOpen ] = useState ( false )
5956 const [ selectedDocument , setSelectedDocument ] = useState < DocumentData | null > ( null )
60- const [ initialFetchDone , setInitialFetchDone ] = useState ( false )
6157
6258 // Use the proper hook to get the current value and setter
6359 const [ storeValue , setStoreValue ] = useSubBlockValue ( blockId , subBlock . id )
6460
65- // Get the knowledge base ID from the same block's knowledgeBaseId subblock - memoize to prevent re-renders
66- const knowledgeBaseId = useMemo ( ( ) => getValue ( blockId , 'knowledgeBaseId' ) , [ getValue , blockId ] )
61+ // Get the knowledge base ID from the same block's knowledgeBaseId subblock
62+ const [ knowledgeBaseId ] = useSubBlockValue ( blockId , 'knowledgeBaseId' )
6763
6864 // Use preview value when in preview mode, otherwise use store value
6965 const value = isPreview ? previewValue : storeValue
@@ -73,7 +69,6 @@ export function DocumentSelector({
7369 if ( ! knowledgeBaseId ) {
7470 setDocuments ( [ ] )
7571 setError ( 'No knowledge base selected' )
76- setInitialFetchDone ( true )
7772 return
7873 }
7974
@@ -94,7 +89,6 @@ export function DocumentSelector({
9489
9590 const fetchedDocuments = result . data || [ ]
9691 setDocuments ( fetchedDocuments )
97- setInitialFetchDone ( true )
9892 } catch ( err ) {
9993 if ( ( err as Error ) . name === 'AbortError' ) return
10094 setError ( ( err as Error ) . message )
@@ -138,16 +132,15 @@ export function DocumentSelector({
138132 useEffect ( ( ) => {
139133 setDocuments ( [ ] )
140134 setSelectedDocument ( null )
141- setInitialFetchDone ( false )
142135 setError ( null )
143136 } , [ knowledgeBaseId ] )
144137
145- // Fetch documents when knowledge base is available and we haven't fetched yet
138+ // Fetch documents when knowledge base is available
146139 useEffect ( ( ) => {
147- if ( knowledgeBaseId && ! initialFetchDone && ! isPreview ) {
140+ if ( knowledgeBaseId && ! isPreview ) {
148141 fetchDocuments ( )
149142 }
150- } , [ knowledgeBaseId , initialFetchDone , isPreview , fetchDocuments ] )
143+ } , [ knowledgeBaseId , isPreview , fetchDocuments ] )
151144
152145 const formatDocumentName = ( document : DocumentData ) => {
153146 return document . filename
0 commit comments