Skip to content

Commit 8e92af9

Browse files
committed
simplify
1 parent db7d97f commit 8e92af9

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ const BOOLEAN_OPTIONS: ComboboxOption[] = [
7474
{ label: 'false', value: 'false' },
7575
]
7676

77-
/**
78-
* Creates a new field with default values
79-
*/
80-
const createDefaultField = (): Field => createDefaultInputFormatField()
81-
8277
/**
8378
* Validates and sanitizes field names by removing control characters and quotes
8479
*/
@@ -127,7 +122,7 @@ export function FieldFormat({
127122
* the same id instead of a freshly generated one.
128123
*/
129124
const fallbackFieldRef = useRef<Field | null>(null)
130-
const fallbackField = (fallbackFieldRef.current ??= createDefaultField())
125+
const fallbackField = (fallbackFieldRef.current ??= createDefaultInputFormatField())
131126

132127
const value = isPreview ? previewValue : storeValue
133128
const fields: Field[] = Array.isArray(value) && value.length > 0 ? value : [fallbackField]
@@ -140,7 +135,7 @@ export function FieldFormat({
140135
*/
141136
const addField = () => {
142137
if (isReadOnly) return
143-
setStoreValue([...fields, createDefaultField()])
138+
setStoreValue([...fields, createDefaultInputFormatField()])
144139
}
145140

146141
/**
@@ -150,7 +145,7 @@ export function FieldFormat({
150145
if (isReadOnly) return
151146

152147
if (fields.length === 1) {
153-
setStoreValue([createDefaultField()])
148+
setStoreValue([createDefaultInputFormatField()])
154149
return
155150
}
156151

apps/sim/lib/workflows/input-format.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import {
3+
createDefaultInputFormatField,
34
extractInputFieldsFromBlocks,
45
normalizeInputFormatValue,
56
} from '@/lib/workflows/input-format'
@@ -227,3 +228,28 @@ describe('normalizeInputFormatValue', () => {
227228
expect(normalizeInputFormatValue(input)).toEqual(input)
228229
})
229230
})
231+
232+
describe('createDefaultInputFormatField', () => {
233+
it.concurrent('creates an empty field with the canonical default shape', () => {
234+
const field = createDefaultInputFormatField()
235+
expect(field).toEqual({
236+
id: expect.any(String),
237+
name: '',
238+
type: 'string',
239+
value: '',
240+
collapsed: false,
241+
})
242+
expect(field.id.length).toBeGreaterThan(0)
243+
})
244+
245+
it.concurrent('omits description so it is not persisted by default', () => {
246+
expect('description' in createDefaultInputFormatField()).toBe(false)
247+
})
248+
249+
it.concurrent('returns a fresh id and a new object on each call', () => {
250+
const first = createDefaultInputFormatField()
251+
const second = createDefaultInputFormatField()
252+
expect(first.id).not.toBe(second.id)
253+
expect(first).not.toBe(second)
254+
})
255+
})

apps/sim/lib/workflows/input-format.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ export interface WorkflowInputField {
1212
}
1313

1414
/**
15-
* Stateful input-format field as stored in sub-block values.
16-
*
17-
* Extends the wire-level {@link InputFormatField} with the editor-only `id` and
18-
* `collapsed` fields maintained per row in the inputs editor.
15+
* Stateful input-format field as stored in sub-block values: the editor's
16+
* per-row shape, including the editor-only `id` and `collapsed` fields. Stricter
17+
* than the wire-level {@link InputFormatField} (required `name`/`type`/`value`).
1918
*/
20-
export interface InputFormatFieldState {
19+
interface InputFormatFieldState {
2120
id: string
2221
name: string
2322
type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file[]'

0 commit comments

Comments
 (0)