Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): keep source switch-group toggles on after save (#76036)

Daniel RC0c2674fda9e1
diff --git a/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx b/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx
index 5488f2ca99be..e30d0b6e9a2e 100644
--- a/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx
+++ b/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx
@@ -1,4 +1,4 @@
-import { SourceConfig, SourceFieldSelectConfig } from '~/queries/schema/schema-general'
+import { SourceConfig, SourceFieldSelectConfig, SourceFieldSwitchGroupConfig } from '~/queries/schema/schema-general'
import { sourceFieldToElement } from './SourceForm'
@@ -17,6 +17,36 @@ const SELECT_FIELD: SourceFieldSelectConfig = {
const SOURCE_CONFIG = { name: 'Typeform', fields: [] } as unknown as SourceConfig
+const SWITCH_GROUP_FIELD: SourceFieldSwitchGroupConfig = {
+ type: 'switch-group',
+ name: 'custom_properties',
+ label: 'Customize synced properties',
+ default: false,
+ fields: [
+ {
+ type: 'textarea',
+ name: 'deals_properties',
+ label: 'Deals properties',
+ required: false,
+ placeholder: '',
+ secret: false,
+ },
+ ],
+}
+
+// Renders the switch group and reports the toggle's checked state alongside whether its child
+// fields expanded — the bug was precisely those two disagreeing.
+const switchGroupState = (storedGroupValue: any, formValue?: any): { checked: boolean; childrenVisible: boolean } => {
+ const element = sourceFieldToElement(SWITCH_GROUP_FIELD, SOURCE_CONFIG, storedGroupValue)
+ const rendered = element.props.children({ value: formValue, onChange: jest.fn() })
+ const children = Array.isArray(rendered.props.children) ? rendered.props.children : [rendered.props.children]
+ const toggle = children.find((child: any) => child && child.props && 'checked' in child.props)
+ if (!toggle) {
+ throw new Error('switch group rendered no toggle')
+ }
+ return { checked: toggle.props.checked, childrenVisible: !!children.find((child: any) => child?.props?.name) }
+}
+
describe('sourceFieldToElement', () => {
it('renders a select field caption as field help text', () => {
const element = sourceFieldToElement(SELECT_FIELD, SOURCE_CONFIG)
@@ -27,4 +57,21 @@ describe('sourceFieldToElement', () => {
const element = sourceFieldToElement({ ...SELECT_FIELD, caption: undefined }, SOURCE_CONFIG)
expect(element.props.help).toBeUndefined()
})
+
+ // job_inputs cross an encrypted field that stringifies booleans, so the form prefills `enabled`
+ // as "True"/"False". LemonSwitch checks `checked === true`, so an untranslated "True" rendered
+ // the toggle off while the truthy string still expanded the children below it.
+ it.each([
+ ['prefilled as the string True', undefined, 'True', true],
+ ['prefilled as the string False', undefined, 'False', false],
+ ['toggled on in the form', undefined, true, true],
+ ['stored as the string True before the prefill lands', { enabled: 'True' }, undefined, true],
+ ['never configured', undefined, undefined, false],
+ ['toggled off in the form over a stored True', { enabled: 'True' }, false, false],
+ ])('reflects a switch group %s', (_name, storedGroupValue, formValue, expected) => {
+ expect(switchGroupState(storedGroupValue, formValue)).toEqual({
+ checked: expected,
+ childrenVisible: expected,
+ })
+ })
})
diff --git a/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx b/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx
index 4f7990fde18f..bc2b600a9d9a 100644
--- a/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx
+++ b/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx
@@ -196,11 +196,15 @@ export const sourceFieldToElement = (
}
if (field.type === 'switch-group') {
- const enabled = !!lastValue?.[field.name]?.enabled || lastValue?.[field.name]?.enabled === 'True'
+ // job_inputs booleans round-trip through the encrypted field as the strings "True"/"False",
+ // and LemonSwitch treats anything but a real `true` as off — translate before rendering.
+ // `lastValue` is already scoped to this group by the caller.
+ const toBool = (flag: unknown): boolean => flag === true || flag === 'True'
+ const enabled = toBool(lastValue?.enabled)
return (
<LemonField key={field.name} name={[field.name, 'enabled']} label={field.label}>
{({ value, onChange }) => {
- const isEnabled = value === undefined || value === null || value === 'False' ? enabled : value
+ const isEnabled = value === undefined || value === null ? enabled : toBool(value)
return (
<>
{!!field.caption && <p className="mb-0">{field.caption}</p>}