diff --git a/app/w/components/workflow-block/sub-block/components/table.tsx b/app/w/components/workflow-block/sub-block/components/table.tsx index d42cca7bbe..fe8c2fe6a8 100644 --- a/app/w/components/workflow-block/sub-block/components/table.tsx +++ b/app/w/components/workflow-block/sub-block/components/table.tsx @@ -1,58 +1,59 @@ import { Trash2 } from 'lucide-react' -import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { cn } from '@/lib/utils' +import { useSubBlockValue } from '../hooks/use-sub-block-value' interface TableProps { columns: string[] + blockId: string + subBlockId: string } interface TableRow { id: string - cells: string[] + cells: Record } -export function Table({ columns }: TableProps) { - const [rows, setRows] = useState([ - { id: crypto.randomUUID(), cells: Array(columns.length).fill('') }, - ]) +export function Table({ columns, blockId, subBlockId }: TableProps) { + const [value, setValue] = useSubBlockValue(blockId, subBlockId) + + // Initialize with empty row if no value exists + const rows = (value as any[]) || [ + { + id: crypto.randomUUID(), + cells: Object.fromEntries(columns.map((col) => [col, ''])), + }, + ] const handleCellChange = ( rowIndex: number, - cellIndex: number, + column: string, value: string ) => { - setRows((currentRows) => { - const updatedRows = currentRows.map((row, idx) => - idx === rowIndex - ? { - ...row, - cells: row.cells.map((cell, cidx) => - cidx === cellIndex ? value : cell - ), - } - : row - ) + const updatedRows = rows.map((row, idx) => + idx === rowIndex + ? { + ...row, + cells: { ...row.cells, [column]: value }, + } + : row + ) - // Add new row if typing in the last row - if (rowIndex === currentRows.length - 1 && value !== '') { - updatedRows.push({ - id: crypto.randomUUID(), - cells: Array(columns.length).fill(''), - }) - } + // Add new row if typing in the last row + if (rowIndex === rows.length - 1 && value !== '') { + updatedRows.push({ + id: crypto.randomUUID(), + cells: Object.fromEntries(columns.map((col) => [col, ''])), + }) + } - return updatedRows - }) + setValue(updatedRows) } const handleDeleteRow = (rowIndex: number) => { - setRows((currentRows) => { - // Don't delete if it's the last row - if (currentRows.length === 1) return currentRows - return currentRows.filter((_, index) => index !== rowIndex) - }) + if (rows.length === 1) return // Don't delete if it's the last row + setValue(rows.filter((_, index) => index !== rowIndex)) } return ( @@ -62,7 +63,7 @@ export function Table({ columns }: TableProps) { {columns.map((column, index) => ( {rows.map((row, rowIndex) => ( - {row.cells.map((cell, cellIndex) => ( + {columns.map((column, cellIndex) => ( - handleCellChange(rowIndex, cellIndex, e.target.value) + handleCellChange(rowIndex, column, e.target.value) } className="border-0 focus-visible:ring-0 focus-visible:ring-offset-0 text-muted-foreground placeholder:text-muted-foreground/50" /> diff --git a/app/w/components/workflow-block/sub-block/sub-block.tsx b/app/w/components/workflow-block/sub-block/sub-block.tsx index cc4e04dde8..2828777eb0 100644 --- a/app/w/components/workflow-block/sub-block/sub-block.tsx +++ b/app/w/components/workflow-block/sub-block/sub-block.tsx @@ -59,7 +59,13 @@ export function SubBlock({ blockId, config }: SubBlockProps) { /> ) case 'table': - return + return ( +
+ ) case 'code': return default: