11'use client'
22
3- import { useRef } from 'react'
3+ import { useMemo , useRef } from 'react'
44import { Plus } from 'lucide-react'
55import {
66 Badge ,
@@ -14,14 +14,19 @@ import {
1414import { cn } from '@/lib/core/utils/cn'
1515import { FIELD_TYPE_LABELS , getPlaceholderForFieldType } from '@/lib/knowledge/constants'
1616import { type FilterFieldType , getOperatorsForFieldType } from '@/lib/knowledge/filters/types'
17+ import { buildCanonicalIndex , resolveDependencyValue } from '@/lib/workflows/subblocks/visibility'
1718import { formatDisplayText } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/formatted-text'
1819import { TagDropdown } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tag-dropdown/tag-dropdown'
1920import { useSubBlockInput } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-input'
2021import { resolvePreviewContextValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/utils'
2122import { useAccessibleReferencePrefixes } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-accessible-reference-prefixes'
23+ import { getBlock } from '@/blocks/registry'
2224import type { SubBlockConfig } from '@/blocks/types'
2325import { useKnowledgeBaseTagDefinitions } from '@/hooks/kb/use-knowledge-base-tag-definitions'
2426import { useTagSelection } from '@/hooks/kb/use-tag-selection'
27+ import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
28+ import { useSubBlockStore } from '@/stores/workflows/subblock/store'
29+ import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2530import { useSubBlockValue } from '../../hooks/use-sub-block-value'
2631
2732interface TagFilter {
@@ -64,15 +69,38 @@ export function KnowledgeTagFilters({
6469 previewValue,
6570 previewContextValues,
6671} : KnowledgeTagFiltersProps ) {
72+ const { activeWorkflowId } = useWorkflowRegistry ( )
6773 const [ storeValue , setStoreValue ] = useSubBlockValue < string | null > ( blockId , subBlock . id )
6874 const emitTagSelection = useTagSelection ( blockId , subBlock . id )
6975 const valueInputRefs = useRef < Record < string , HTMLInputElement > > ( { } )
7076 const overlayRefs = useRef < Record < string , HTMLDivElement > > ( { } )
7177
72- const [ knowledgeBaseIdFromStore ] = useSubBlockValue ( blockId , 'knowledgeBaseId' )
73- const knowledgeBaseIdValue = previewContextValues
74- ? resolvePreviewContextValue ( previewContextValues . knowledgeBaseId )
75- : knowledgeBaseIdFromStore
78+ const blockState = useWorkflowStore ( ( state ) => state . blocks [ blockId ] )
79+ const blockConfig = blockState ?. type ? getBlock ( blockState . type ) : null
80+ const canonicalIndex = useMemo (
81+ ( ) => buildCanonicalIndex ( blockConfig ?. subBlocks || [ ] ) ,
82+ [ blockConfig ?. subBlocks ]
83+ )
84+ const canonicalModeOverrides = blockState ?. data ?. canonicalModes
85+
86+ const blockValues = useSubBlockStore ( ( state ) => {
87+ if ( ! activeWorkflowId ) return { }
88+ const workflowValues = state . workflowValues [ activeWorkflowId ] || { }
89+ return ( workflowValues as Record < string , Record < string , unknown > > ) [ blockId ] || { }
90+ } )
91+
92+ const knowledgeBaseIdValue = useMemo (
93+ ( ) =>
94+ previewContextValues
95+ ? resolvePreviewContextValue ( previewContextValues . knowledgeBaseId )
96+ : resolveDependencyValue (
97+ 'knowledgeBaseId' ,
98+ blockValues ,
99+ canonicalIndex ,
100+ canonicalModeOverrides
101+ ) ,
102+ [ previewContextValues , blockValues , canonicalIndex , canonicalModeOverrides ]
103+ )
76104 const knowledgeBaseId =
77105 typeof knowledgeBaseIdValue === 'string' && knowledgeBaseIdValue . trim ( ) . length > 0
78106 ? knowledgeBaseIdValue
0 commit comments