Skip to content

Commit f6960a4

Browse files
authored
fix(wand): improved flickering for invalid JSON icon while streaming (#2868)
1 parent 8740566 commit f6960a4

File tree

2 files changed

+30
-40
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block

2 files changed

+30
-40
lines changed

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -307,25 +307,18 @@ export function Code({
307307
? getDefaultValueString()
308308
: storeValue
309309

310-
const lastValidationStatus = useRef<boolean>(true)
311-
312310
useEffect(() => {
313311
if (!onValidationChange) return
314312

315-
const nextStatus = shouldValidateJson ? isValidJson : true
316-
if (lastValidationStatus.current === nextStatus) {
317-
return
318-
}
319-
320-
lastValidationStatus.current = nextStatus
313+
const isValid = !shouldValidateJson || isValidJson
321314

322-
if (!shouldValidateJson) {
323-
onValidationChange(nextStatus)
315+
if (isValid) {
316+
onValidationChange(true)
324317
return
325318
}
326319

327320
const timeoutId = setTimeout(() => {
328-
onValidationChange(nextStatus)
321+
onValidationChange(false)
329322
}, 150)
330323

331324
return () => clearTimeout(timeoutId)
@@ -337,7 +330,7 @@ export function Code({
337330
}
338331

339332
handleStreamChunkRef.current = (chunk: string) => {
340-
setCode((prev) => prev + chunk)
333+
setCode((prev: string) => prev + chunk)
341334
}
342335

343336
handleGeneratedContentRef.current = (generatedCode: string) => {
@@ -434,12 +427,12 @@ export function Code({
434427
`
435428
document.body.appendChild(tempContainer)
436429

437-
lines.forEach((line) => {
430+
lines.forEach((line: string) => {
438431
const lineDiv = document.createElement('div')
439432

440433
if (line.includes('<') && line.includes('>')) {
441434
const parts = line.split(/(<[^>]+>)/g)
442-
parts.forEach((part) => {
435+
parts.forEach((part: string) => {
443436
const span = document.createElement('span')
444437
span.textContent = part
445438
lineDiv.appendChild(span)
@@ -472,7 +465,6 @@ export function Code({
472465
}
473466
}, [code])
474467

475-
// Event Handlers
476468
/**
477469
* Handles drag-and-drop events for inserting reference tags into the code editor.
478470
* @param e - The drag event
@@ -500,7 +492,6 @@ export function Code({
500492
textarea.selectionStart = newCursorPosition
501493
textarea.selectionEnd = newCursorPosition
502494

503-
// Show tag dropdown after cursor is positioned
504495
setShowTags(true)
505496
if (data.connectionData?.sourceBlockId) {
506497
setActiveSourceBlockId(data.connectionData.sourceBlockId)
@@ -559,7 +550,6 @@ export function Code({
559550
}
560551
}
561552

562-
// Helper Functions
563553
/**
564554
* Determines whether a `<...>` segment should be highlighted as a reference.
565555
* @param part - The code segment to check
@@ -596,7 +586,6 @@ export function Code({
596586
return accessiblePrefixes.has(normalizedPrefix)
597587
}
598588

599-
// Expose wand control handlers to parent via ref
600589
useImperativeHandle(
601590
wandControlRef,
602591
() => ({
@@ -617,7 +606,7 @@ export function Code({
617606
const numbers: ReactElement[] = []
618607
let lineNumber = 1
619608

620-
visualLineHeights.forEach((height) => {
609+
visualLineHeights.forEach((height: number) => {
621610
const isActive = lineNumber === activeLineNumber
622611
numbers.push(
623612
<div
@@ -724,7 +713,7 @@ export function Code({
724713

725714
<Editor
726715
value={code}
727-
onValueChange={(newCode) => {
716+
onValueChange={(newCode: string) => {
728717
if (!isAiStreaming && !isPreview && !disabled && !readOnly) {
729718
hasEditedSinceFocusRef.current = true
730719
setCode(newCode)
@@ -761,7 +750,6 @@ export function Code({
761750
}}
762751
onFocus={() => {
763752
hasEditedSinceFocusRef.current = false
764-
// Show tag dropdown on focus when code is empty
765753
if (!isPreview && !disabled && !readOnly && code.trim() === '') {
766754
setShowTags(true)
767755
setCursorPosition(0)

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,21 @@ const renderLabel = (
207207
<Label className='flex items-center gap-[6px] whitespace-nowrap'>
208208
{config.title}
209209
{required && <span className='ml-0.5'>*</span>}
210-
{config.type === 'code' && config.language === 'json' && (
211-
<Tooltip.Root>
212-
<Tooltip.Trigger asChild>
213-
<AlertTriangle
214-
className={cn(
215-
'h-4 w-4 cursor-pointer text-destructive',
216-
!isValidJson ? 'opacity-100' : 'opacity-0'
217-
)}
218-
/>
219-
</Tooltip.Trigger>
220-
<Tooltip.Content side='top'>
221-
<p>Invalid JSON</p>
222-
</Tooltip.Content>
223-
</Tooltip.Root>
224-
)}
210+
{config.type === 'code' &&
211+
config.language === 'json' &&
212+
!isValidJson &&
213+
!wandState?.isStreaming && (
214+
<Tooltip.Root>
215+
<Tooltip.Trigger asChild>
216+
<span className='inline-flex'>
217+
<AlertTriangle className='h-3 w-3 flex-shrink-0 cursor-pointer text-destructive' />
218+
</span>
219+
</Tooltip.Trigger>
220+
<Tooltip.Content side='top'>
221+
<p>Invalid JSON</p>
222+
</Tooltip.Content>
223+
</Tooltip.Root>
224+
)}
225225
</Label>
226226
<div className='flex items-center gap-[6px]'>
227227
{showWand && (
@@ -239,9 +239,11 @@ const renderLabel = (
239239
<Input
240240
ref={wandState.searchInputRef}
241241
value={wandState.isStreaming ? 'Generating...' : wandState.searchQuery}
242-
onChange={(e) => wandState.onSearchChange(e.target.value)}
242+
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
243+
wandState.onSearchChange(e.target.value)
244+
}
243245
onBlur={wandState.onSearchBlur}
244-
onKeyDown={(e) => {
246+
onKeyDown={(e: React.KeyboardEvent<HTMLInputElement>) => {
245247
if (
246248
e.key === 'Enter' &&
247249
wandState.searchQuery.trim() &&
@@ -262,11 +264,11 @@ const renderLabel = (
262264
<Button
263265
variant='tertiary'
264266
disabled={!wandState.searchQuery.trim() || wandState.isStreaming}
265-
onMouseDown={(e) => {
267+
onMouseDown={(e: React.MouseEvent) => {
266268
e.preventDefault()
267269
e.stopPropagation()
268270
}}
269-
onClick={(e) => {
271+
onClick={(e: React.MouseEvent) => {
270272
e.stopPropagation()
271273
wandState.onSearchSubmit()
272274
}}

0 commit comments

Comments
 (0)