Skip to content

Commit e6fefc8

Browse files
authored
fix(table-block): resolve canonical tableId in filter/sort builders (#4294)
The visual filter/sort builders read the selected tableId from subBlock id 'tableId', but the Table block stores it under 'tableSelector' (basic) or 'manualTableId' (advanced) via canonicalParamId. The lookup always returned null, so useTable was disabled and the column picker always showed "no options available". Adds useCanonicalSubBlockValue that resolves by canonicalParamId through the canonical index, mirroring the pattern used by dropdown dependsOn.
1 parent 5fba724 commit e6fefc8

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/filter-builder/filter-builder.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ComboboxOption } from '@/components/emcn'
66
import { useTableColumns } from '@/lib/table/hooks'
77
import type { FilterRule } from '@/lib/table/query-builder/constants'
88
import { useFilterBuilder } from '@/lib/table/query-builder/use-query-builder'
9+
import { useCanonicalSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-canonical-sub-block-value'
910
import { useSubBlockInput } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-input'
1011
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
1112
import { FilterRuleRow } from './components/filter-rule-row'
@@ -40,7 +41,7 @@ export function FilterBuilder({
4041
tableIdSubBlockId = 'tableId',
4142
}: FilterBuilderProps) {
4243
const [storeValue, setStoreValue] = useSubBlockValue<FilterRule[]>(blockId, subBlockId)
43-
const [tableIdValue] = useSubBlockValue<string>(blockId, tableIdSubBlockId)
44+
const tableIdValue = useCanonicalSubBlockValue<string>(blockId, tableIdSubBlockId)
4445

4546
const dynamicColumns = useTableColumns({ tableId: tableIdValue })
4647
const columns = useMemo(() => {

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/sort-builder/sort-builder.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateId } from '@sim/utils/id'
55
import type { ComboboxOption } from '@/components/emcn'
66
import { useTableColumns } from '@/lib/table/hooks'
77
import { SORT_DIRECTIONS, type SortRule } from '@/lib/table/query-builder/constants'
8+
import { useCanonicalSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-canonical-sub-block-value'
89
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
910
import { SortRuleRow } from './components/sort-rule-row'
1011

@@ -36,7 +37,7 @@ export function SortBuilder({
3637
tableIdSubBlockId = 'tableId',
3738
}: SortBuilderProps) {
3839
const [storeValue, setStoreValue] = useSubBlockValue<SortRule[]>(blockId, subBlockId)
39-
const [tableIdValue] = useSubBlockValue<string>(blockId, tableIdSubBlockId)
40+
const tableIdValue = useCanonicalSubBlockValue<string>(blockId, tableIdSubBlockId)
4041

4142
const dynamicColumns = useTableColumns({ tableId: tableIdValue, includeBuiltIn: true })
4243
const columns = useMemo(() => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useCallback, useMemo } from 'react'
2+
import { isEqual } from 'es-toolkit'
3+
import { useStoreWithEqualityFn } from 'zustand/traditional'
4+
import { buildCanonicalIndex, resolveDependencyValue } from '@/lib/workflows/subblocks/visibility'
5+
import { getBlock } from '@/blocks/registry'
6+
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
7+
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
8+
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
9+
10+
/**
11+
* Read a sub-block value by either its raw subBlockId or its canonicalParamId.
12+
*
13+
* `useSubBlockValue` only looks up the raw subBlockId. For fields that use
14+
* `canonicalParamId` to unify basic/advanced inputs (e.g. `tableSelector` vs
15+
* `manualTableId` both mapping to `tableId`), this hook resolves to whichever
16+
* member of the canonical group currently holds the value.
17+
*/
18+
export function useCanonicalSubBlockValue<T = unknown>(
19+
blockId: string,
20+
canonicalOrSubBlockId: string
21+
): T | null {
22+
const activeWorkflowId = useWorkflowRegistry((s) => s.activeWorkflowId)
23+
const blockState = useWorkflowStore((state) => state.blocks[blockId])
24+
const blockConfig = blockState?.type ? getBlock(blockState.type) : null
25+
const canonicalIndex = useMemo(
26+
() => buildCanonicalIndex(blockConfig?.subBlocks || []),
27+
[blockConfig?.subBlocks]
28+
)
29+
const canonicalModeOverrides = blockState?.data?.canonicalModes
30+
31+
return useStoreWithEqualityFn(
32+
useSubBlockStore,
33+
useCallback(
34+
(state) => {
35+
if (!activeWorkflowId) return null
36+
const blockValues = state.workflowValues[activeWorkflowId]?.[blockId] || {}
37+
const resolved = resolveDependencyValue(
38+
canonicalOrSubBlockId,
39+
blockValues,
40+
canonicalIndex,
41+
canonicalModeOverrides
42+
)
43+
return (resolved ?? null) as T | null
44+
},
45+
[activeWorkflowId, blockId, canonicalOrSubBlockId, canonicalIndex, canonicalModeOverrides]
46+
),
47+
(a, b) => isEqual(a, b)
48+
)
49+
}

0 commit comments

Comments
 (0)