mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
State works without subblock state
This commit is contained in:
@@ -9,9 +9,16 @@ import {
|
||||
interface DropdownProps {
|
||||
options: string[]
|
||||
defaultValue?: string
|
||||
blockId: string
|
||||
subBlockId: string
|
||||
}
|
||||
|
||||
export function Dropdown({ options, defaultValue }: DropdownProps) {
|
||||
export function Dropdown({
|
||||
options,
|
||||
defaultValue,
|
||||
blockId,
|
||||
subBlockId,
|
||||
}: DropdownProps) {
|
||||
return (
|
||||
<Select defaultValue={defaultValue ?? options[0]}>
|
||||
<SelectTrigger>
|
||||
|
||||
@@ -3,9 +3,15 @@ import { useState } from 'react'
|
||||
|
||||
interface LongInputProps {
|
||||
placeholder?: string
|
||||
blockId: string
|
||||
subBlockId: string
|
||||
}
|
||||
|
||||
export function LongInput({ placeholder }: LongInputProps) {
|
||||
export function LongInput({
|
||||
placeholder,
|
||||
blockId,
|
||||
subBlockId,
|
||||
}: LongInputProps) {
|
||||
const [value, setValue] = useState('')
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,9 +4,16 @@ import { useState } from 'react'
|
||||
interface ShortInputProps {
|
||||
placeholder?: string
|
||||
password?: boolean
|
||||
blockId: string
|
||||
subBlockId: string
|
||||
}
|
||||
|
||||
export function ShortInput({ placeholder, password }: ShortInputProps) {
|
||||
export function ShortInput({
|
||||
blockId,
|
||||
subBlockId,
|
||||
placeholder,
|
||||
password,
|
||||
}: ShortInputProps) {
|
||||
const [isFocused, setIsFocused] = useState(false)
|
||||
const [value, setValue] = useState('')
|
||||
|
||||
|
||||
@@ -5,12 +5,16 @@ interface SliderInputProps {
|
||||
min?: number
|
||||
max?: number
|
||||
defaultValue: number
|
||||
blockId: string
|
||||
subBlockId: string
|
||||
}
|
||||
|
||||
export function SliderInput({
|
||||
min = 0,
|
||||
max = 100,
|
||||
defaultValue,
|
||||
blockId,
|
||||
subBlockId,
|
||||
}: SliderInputProps) {
|
||||
const [sliderValue, setSliderValue] = useState(defaultValue)
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { useWorkflowStore } from '@/stores/workflow/workflow-store'
|
||||
import { useCallback } from 'react'
|
||||
|
||||
export function useSubBlockValue(blockId: string, subBlockId: string) {
|
||||
const value = useWorkflowStore(
|
||||
useCallback(
|
||||
(state) =>
|
||||
(state.blocks[blockId]?.subBlocks[subBlockId]?.value) ?? null,
|
||||
[blockId, subBlockId]
|
||||
)
|
||||
)
|
||||
|
||||
const updateSubBlock = useWorkflowStore((state) => state.updateSubBlock)
|
||||
|
||||
const setValue = useCallback(
|
||||
(newValue: any) => {
|
||||
updateSubBlock(blockId, subBlockId, newValue)
|
||||
},
|
||||
[blockId, subBlockId, updateSubBlock]
|
||||
)
|
||||
|
||||
return [value, setValue] as const
|
||||
}
|
||||
@@ -8,10 +8,11 @@ import { Table } from './components/table'
|
||||
import { Code } from './components/code'
|
||||
|
||||
interface SubBlockProps {
|
||||
blockId: string
|
||||
config: SubBlockConfig
|
||||
}
|
||||
|
||||
export function SubBlock({ config }: SubBlockProps) {
|
||||
export function SubBlock({ blockId, config }: SubBlockProps) {
|
||||
const handleMouseDown = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
}
|
||||
@@ -21,21 +22,35 @@ export function SubBlock({ config }: SubBlockProps) {
|
||||
case 'short-input':
|
||||
return (
|
||||
<ShortInput
|
||||
blockId={blockId}
|
||||
subBlockId={config.id || ''}
|
||||
placeholder={config.placeholder}
|
||||
password={config.password}
|
||||
/>
|
||||
)
|
||||
case 'long-input':
|
||||
return <LongInput placeholder={config.placeholder} />
|
||||
return (
|
||||
<LongInput
|
||||
blockId={blockId}
|
||||
subBlockId={config.id || ''}
|
||||
placeholder={config.placeholder}
|
||||
/>
|
||||
)
|
||||
case 'dropdown':
|
||||
return (
|
||||
<div onMouseDown={handleMouseDown}>
|
||||
<Dropdown options={config.options ?? []} />
|
||||
<Dropdown
|
||||
blockId={blockId}
|
||||
subBlockId={config.id || ''}
|
||||
options={config.options ?? []}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
case 'slider':
|
||||
return (
|
||||
<SliderInput
|
||||
blockId={blockId}
|
||||
subBlockId={config.id || ''}
|
||||
min={config.min}
|
||||
max={config.max}
|
||||
defaultValue={
|
||||
|
||||
@@ -75,7 +75,7 @@ export function WorkflowBlock({ id, type, config, name }: WorkflowBlockProps) {
|
||||
subBlock.layout === 'half' ? 'flex-1' : 'w-full'
|
||||
}`}
|
||||
>
|
||||
<SubBlock config={subBlock} />
|
||||
<SubBlock blockId={id} config={subBlock} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -11,11 +11,11 @@ export interface BlockState {
|
||||
type: BlockType
|
||||
name: string
|
||||
position: Position
|
||||
subBlocks: Record<string, SubBlock>
|
||||
subBlocks: Record<string, SubBlockState>
|
||||
outputType: OutputType
|
||||
}
|
||||
|
||||
export interface SubBlock {
|
||||
export interface SubBlockState {
|
||||
id: string
|
||||
type: SubBlockType
|
||||
value: string | number | string[][] | null
|
||||
@@ -35,7 +35,7 @@ export interface WorkflowActions {
|
||||
position: Position
|
||||
) => void
|
||||
updateBlockPosition: (id: string, position: Position) => void
|
||||
updateBlockInput: (blockId: string, inputId: string, value: any) => void
|
||||
updateSubBlock: (blockId: string, subBlockId: string, subBlock: SubBlockState) => void
|
||||
removeBlock: (id: string) => void
|
||||
addEdge: (edge: Edge) => void
|
||||
removeEdge: (edgeId: string) => void
|
||||
|
||||
@@ -2,8 +2,7 @@ import { create } from 'zustand'
|
||||
import { devtools } from 'zustand/middleware'
|
||||
import { Edge } from 'reactflow'
|
||||
import { BlockType } from '@/blocks/types/block'
|
||||
import { Position } from '@/stores/workflow/types'
|
||||
import { WorkflowStore } from './types'
|
||||
import { Position, WorkflowStore } from './types'
|
||||
import { getBlock } from '@/blocks/configs'
|
||||
|
||||
const initialState = {
|
||||
@@ -23,8 +22,9 @@ export const useWorkflowStore = create<WorkflowStore>()(
|
||||
|
||||
const subBlocks: Record<string, any> = {}
|
||||
blockConfig.workflow.subBlocks.forEach((subBlock) => {
|
||||
subBlocks[subBlock.id || crypto.randomUUID()] = {
|
||||
id: subBlock.id || crypto.randomUUID(),
|
||||
const subBlockId = subBlock.id || crypto.randomUUID()
|
||||
subBlocks[subBlockId] = {
|
||||
id: subBlockId,
|
||||
type: subBlock.type,
|
||||
value: null,
|
||||
}
|
||||
@@ -60,7 +60,7 @@ export const useWorkflowStore = create<WorkflowStore>()(
|
||||
}))
|
||||
},
|
||||
|
||||
updateBlockInput: (blockId: string, inputId: string, value: any) => {
|
||||
updateSubBlock: (blockId: string, subBlockId: string, value: any) => {
|
||||
set((state) => ({
|
||||
blocks: {
|
||||
...state.blocks,
|
||||
@@ -68,8 +68,8 @@ export const useWorkflowStore = create<WorkflowStore>()(
|
||||
...state.blocks[blockId],
|
||||
subBlocks: {
|
||||
...state.blocks[blockId].subBlocks,
|
||||
[inputId]: {
|
||||
...state.blocks[blockId].subBlocks[inputId],
|
||||
[subBlockId]: {
|
||||
...state.blocks[blockId].subBlocks[subBlockId],
|
||||
value,
|
||||
},
|
||||
},
|
||||
@@ -94,7 +94,14 @@ export const useWorkflowStore = create<WorkflowStore>()(
|
||||
|
||||
addEdge: (edge: Edge) => {
|
||||
set((state) => ({
|
||||
edges: [...state.edges, edge],
|
||||
edges: [
|
||||
...state.edges,
|
||||
{
|
||||
id: edge.id || crypto.randomUUID(),
|
||||
source: edge.source,
|
||||
target: edge.target,
|
||||
},
|
||||
],
|
||||
}))
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user