Skip to content

Commit e425d06

Browse files
authored
improvement(condition): added variable and envvar highlighting for condition input (#1718)
1 parent bcd1a2f commit e425d06

File tree

1 file changed

+79
-1
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/components

1 file changed

+79
-1
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/components/condition-input.tsx

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import { checkTagTrigger, TagDropdown } from '@/components/ui/tag-dropdown'
1414
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
1515
import { createLogger } from '@/lib/logs/console/logger'
1616
import { cn } from '@/lib/utils'
17+
import { isLikelyReferenceSegment, SYSTEM_REFERENCE_PREFIXES } from '@/lib/workflows/references'
1718
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/hooks/use-sub-block-value'
19+
import { useAccessibleReferencePrefixes } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-accessible-reference-prefixes'
1820
import { useTagSelection } from '@/hooks/use-tag-selection'
21+
import { normalizeBlockName } from '@/stores/workflows/utils'
1922
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2023

2124
const logger = createLogger('ConditionInput')
@@ -58,8 +61,33 @@ export function ConditionInput({
5861
const [storeValue, setStoreValue] = useSubBlockValue(blockId, subBlockId)
5962

6063
const emitTagSelection = useTagSelection(blockId, subBlockId)
64+
const accessiblePrefixes = useAccessibleReferencePrefixes(blockId)
6165

6266
const containerRef = useRef<HTMLDivElement>(null)
67+
68+
const shouldHighlightReference = (part: string): boolean => {
69+
if (!part.startsWith('<') || !part.endsWith('>')) {
70+
return false
71+
}
72+
73+
if (!isLikelyReferenceSegment(part)) {
74+
return false
75+
}
76+
77+
if (!accessiblePrefixes) {
78+
return true
79+
}
80+
81+
const inner = part.slice(1, -1)
82+
const [prefix] = inner.split('.')
83+
const normalizedPrefix = normalizeBlockName(prefix)
84+
85+
if (SYSTEM_REFERENCE_PREFIXES.has(normalizedPrefix)) {
86+
return true
87+
}
88+
89+
return accessiblePrefixes.has(normalizedPrefix)
90+
}
6391
const [visualLineHeights, setVisualLineHeights] = useState<{
6492
[key: string]: number[]
6593
}>({})
@@ -778,7 +806,57 @@ export function ConditionInput({
778806
)
779807
}
780808
}}
781-
highlight={(code) => highlight(code, languages.javascript, 'javascript')}
809+
highlight={(codeToHighlight) => {
810+
const placeholders: {
811+
placeholder: string
812+
original: string
813+
type: 'var' | 'env'
814+
}[] = []
815+
let processedCode = codeToHighlight
816+
817+
// Replace environment variables with placeholders
818+
processedCode = processedCode.replace(/\{\{([^}]+)\}\}/g, (match) => {
819+
const placeholder = `__ENV_VAR_${placeholders.length}__`
820+
placeholders.push({ placeholder, original: match, type: 'env' })
821+
return placeholder
822+
})
823+
824+
// Replace variable references with placeholders
825+
processedCode = processedCode.replace(/<([^>]+)>/g, (match) => {
826+
if (shouldHighlightReference(match)) {
827+
const placeholder = `__VAR_REF_${placeholders.length}__`
828+
placeholders.push({ placeholder, original: match, type: 'var' })
829+
return placeholder
830+
}
831+
return match
832+
})
833+
834+
// Apply Prism syntax highlighting
835+
let highlightedCode = highlight(
836+
processedCode,
837+
languages.javascript,
838+
'javascript'
839+
)
840+
841+
// Restore and highlight the placeholders
842+
placeholders.forEach(({ placeholder, original, type }) => {
843+
if (type === 'env') {
844+
highlightedCode = highlightedCode.replace(
845+
placeholder,
846+
`<span class="text-blue-500">${original}</span>`
847+
)
848+
} else if (type === 'var') {
849+
// Escape the < and > for display
850+
const escaped = original.replace(/</g, '&lt;').replace(/>/g, '&gt;')
851+
highlightedCode = highlightedCode.replace(
852+
placeholder,
853+
`<span class="text-blue-500">${escaped}</span>`
854+
)
855+
}
856+
})
857+
858+
return highlightedCode
859+
}}
782860
padding={12}
783861
style={{
784862
fontFamily: 'inherit',

0 commit comments

Comments
 (0)