mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-13 07:55:09 -05:00
Compare commits
1 Commits
v0.5.89
...
fix/cancel
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
529ff71b90 |
@@ -1,9 +1,10 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useRef } from 'react'
|
import { useEffect, useRef } from 'react'
|
||||||
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
|
|
||||||
import { SubBlock } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/sub-block'
|
import { SubBlock } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/sub-block'
|
||||||
import type { SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types'
|
import type { SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types'
|
||||||
|
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
|
||||||
|
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
|
||||||
|
|
||||||
interface ToolSubBlockRendererProps {
|
interface ToolSubBlockRendererProps {
|
||||||
blockId: string
|
blockId: string
|
||||||
@@ -44,53 +45,43 @@ export function ToolSubBlockRenderer({
|
|||||||
canonicalToggle,
|
canonicalToggle,
|
||||||
}: ToolSubBlockRendererProps) {
|
}: ToolSubBlockRendererProps) {
|
||||||
const syntheticId = `${subBlockId}-tool-${toolIndex}-${effectiveParamId}`
|
const syntheticId = `${subBlockId}-tool-${toolIndex}-${effectiveParamId}`
|
||||||
const [storeValue, setStoreValue] = useSubBlockValue(blockId, syntheticId)
|
|
||||||
|
|
||||||
const toolParamValue = toolParams?.[effectiveParamId] ?? ''
|
const toolParamValue = toolParams?.[effectiveParamId] ?? ''
|
||||||
const isObjectType = OBJECT_SUBBLOCK_TYPES.has(subBlock.type)
|
const isObjectType = OBJECT_SUBBLOCK_TYPES.has(subBlock.type)
|
||||||
|
|
||||||
const lastPushedToStoreRef = useRef<string | null>(null)
|
const syncedRef = useRef<string | null>(null)
|
||||||
const lastPushedToParamsRef = useRef<string | null>(null)
|
const onParamChangeRef = useRef(onParamChange)
|
||||||
|
onParamChangeRef.current = onParamChange
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!toolParamValue && lastPushedToStoreRef.current === null) {
|
const unsub = useSubBlockStore.subscribe((state, prevState) => {
|
||||||
lastPushedToStoreRef.current = toolParamValue
|
const wfId = useWorkflowRegistry.getState().activeWorkflowId
|
||||||
lastPushedToParamsRef.current = toolParamValue
|
if (!wfId) return
|
||||||
return
|
const newVal = state.workflowValues[wfId]?.[blockId]?.[syntheticId]
|
||||||
}
|
const oldVal = prevState.workflowValues[wfId]?.[blockId]?.[syntheticId]
|
||||||
if (toolParamValue !== lastPushedToStoreRef.current) {
|
if (newVal === oldVal) return
|
||||||
lastPushedToStoreRef.current = toolParamValue
|
const stringified =
|
||||||
lastPushedToParamsRef.current = toolParamValue
|
newVal == null ? '' : typeof newVal === 'string' ? newVal : JSON.stringify(newVal)
|
||||||
|
if (stringified === syncedRef.current) return
|
||||||
|
syncedRef.current = stringified
|
||||||
|
onParamChangeRef.current(toolIndex, effectiveParamId, stringified)
|
||||||
|
})
|
||||||
|
return unsub
|
||||||
|
}, [blockId, syntheticId, toolIndex, effectiveParamId])
|
||||||
|
|
||||||
if (isObjectType && typeof toolParamValue === 'string' && toolParamValue) {
|
useEffect(() => {
|
||||||
try {
|
if (toolParamValue === syncedRef.current) return
|
||||||
const parsed = JSON.parse(toolParamValue)
|
syncedRef.current = toolParamValue
|
||||||
if (typeof parsed === 'object' && parsed !== null) {
|
if (isObjectType && toolParamValue) {
|
||||||
setStoreValue(parsed)
|
try {
|
||||||
return
|
const parsed = JSON.parse(toolParamValue)
|
||||||
}
|
if (typeof parsed === 'object' && parsed !== null) {
|
||||||
} catch {
|
useSubBlockStore.getState().setValue(blockId, syntheticId, parsed)
|
||||||
// Not valid JSON — fall through to set as string
|
return
|
||||||
}
|
}
|
||||||
}
|
} catch {}
|
||||||
setStoreValue(toolParamValue)
|
|
||||||
}
|
}
|
||||||
}, [toolParamValue, setStoreValue, isObjectType])
|
useSubBlockStore.getState().setValue(blockId, syntheticId, toolParamValue)
|
||||||
|
}, [toolParamValue, blockId, syntheticId, isObjectType])
|
||||||
useEffect(() => {
|
|
||||||
if (storeValue == null && lastPushedToParamsRef.current === null) return
|
|
||||||
const stringValue =
|
|
||||||
storeValue == null
|
|
||||||
? ''
|
|
||||||
: typeof storeValue === 'string'
|
|
||||||
? storeValue
|
|
||||||
: JSON.stringify(storeValue)
|
|
||||||
if (stringValue !== lastPushedToParamsRef.current) {
|
|
||||||
lastPushedToParamsRef.current = stringValue
|
|
||||||
lastPushedToStoreRef.current = stringValue
|
|
||||||
onParamChange(toolIndex, effectiveParamId, stringValue)
|
|
||||||
}
|
|
||||||
}, [storeValue, toolIndex, effectiveParamId, onParamChange])
|
|
||||||
|
|
||||||
const visibility = subBlock.paramVisibility ?? 'user-or-llm'
|
const visibility = subBlock.paramVisibility ?? 'user-or-llm'
|
||||||
const isOptionalForUser = visibility !== 'user-only'
|
const isOptionalForUser = visibility !== 'user-only'
|
||||||
|
|||||||
Reference in New Issue
Block a user