( subBlocks: SubBlockConfig[], blockId: string, activeWorkflowId: string | null, canonicalModeOverrides?: CanonicalModeOverrides )
| 12 | * Returns a Set of subblock IDs that should be hidden. |
| 13 | */ |
| 14 | export function useReactiveConditions( |
| 15 | subBlocks: SubBlockConfig[], |
| 16 | blockId: string, |
| 17 | activeWorkflowId: string | null, |
| 18 | canonicalModeOverrides?: CanonicalModeOverrides |
| 19 | ): Set<string> { |
| 20 | const reactiveSubBlock = useMemo(() => subBlocks.find((sb) => sb.reactiveCondition), [subBlocks]) |
| 21 | const reactiveCond = reactiveSubBlock?.reactiveCondition |
| 22 | |
| 23 | const canonicalIndex = useMemo(() => buildCanonicalIndex(subBlocks), [subBlocks]) |
| 24 | |
| 25 | // Resolve watchFields through canonical index to get the active credential value |
| 26 | const watchedCredentialId = useSubBlockStore( |
| 27 | useCallback( |
| 28 | (state) => { |
| 29 | if (!reactiveCond || !activeWorkflowId) return '' |
| 30 | const blockValues = |
| 31 | state.workflowValues[activeWorkflowId]?.[blockId] ?? EMPTY_BLOCK_SUBBLOCK_VALUES |
| 32 | for (const field of reactiveCond.watchFields) { |
| 33 | const val = resolveDependencyValue( |
| 34 | field, |
| 35 | blockValues, |
| 36 | canonicalIndex, |
| 37 | canonicalModeOverrides |
| 38 | ) |
| 39 | if (val && typeof val === 'string') return val |
| 40 | } |
| 41 | return '' |
| 42 | }, |
| 43 | [reactiveCond, activeWorkflowId, blockId, canonicalIndex, canonicalModeOverrides] |
| 44 | ) |
| 45 | ) |
| 46 | |
| 47 | // Always call useWorkspaceCredential (stable hook count), disable when not needed |
| 48 | const { data: credential } = useWorkspaceCredential( |
| 49 | watchedCredentialId || undefined, |
| 50 | Boolean(reactiveCond && watchedCredentialId) |
| 51 | ) |
| 52 | |
| 53 | return useMemo(() => { |
| 54 | const hidden = new Set<string>() |
| 55 | if (!reactiveSubBlock || !reactiveCond) return hidden |
| 56 | |
| 57 | const conditionMet = credential?.type === reactiveCond.requiredType |
| 58 | if (!conditionMet) { |
| 59 | hidden.add(reactiveSubBlock.id) |
| 60 | } |
| 61 | return hidden |
| 62 | }, [reactiveSubBlock, reactiveCond, credential?.type]) |
| 63 | } |
no test coverage detected