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(cohorts): commit scene name before submit validation (#72007)

posthog[bot]5877999e20f6
diff --git a/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx b/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx
new file mode 100644
index 000000000000..882e497bfd67
--- /dev/null
+++ b/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx
@@ -0,0 +1,40 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+
+import { SceneName } from './SceneTitleSection'
+
+describe('SceneName', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ // Guards the cohort "name cannot be empty" desync: the field debounces its
+ // onChange, but a Save click blurs the field first — the pending value must be
+ // committed synchronously on blur so the following submit validates what the user typed.
+ test('flushes the pending debounced change on blur so onChange fires before a submit', () => {
+ const onChange = jest.fn()
+ render(<SceneName name="" onChange={onChange} canEdit renameDebounceMs={1000} />)
+
+ // Enter edit mode (non-forceEdit fields start as a button)
+ fireEvent.click(screen.getByRole('button'))
+
+ const textarea = screen.getByRole('textbox')
+ fireEvent.change(textarea, { target: { value: 'Paying users' } })
+ // Debounce timer has NOT fired yet, so without the flush onChange would be empty here
+ expect(onChange).not.toHaveBeenCalled()
+
+ fireEvent.blur(textarea)
+ expect(onChange).toHaveBeenCalledWith('Paying users')
+ })
+
+ // Guards the reconciliation change: a genuine external update (loading a resource,
+ // an AI-generated name) must still replace the field's value.
+ test('adopts an external name change into the field', () => {
+ const { rerender } = render(<SceneName name="Old name" canEdit onChange={jest.fn()} />)
+ expect(screen.getByText('Old name')).toBeInTheDocument()
+
+ rerender(<SceneName name="Generated name" canEdit onChange={jest.fn()} />)
+ expect(screen.getByText('Generated name')).toBeInTheDocument()
+ })
+})
diff --git a/frontend/src/layout/scenes/components/SceneTitleSection.tsx b/frontend/src/layout/scenes/components/SceneTitleSection.tsx
index 735b29d0307c..15fd79403063 100644
--- a/frontend/src/layout/scenes/components/SceneTitleSection.tsx
+++ b/frontend/src/layout/scenes/components/SceneTitleSection.tsx
@@ -449,9 +449,17 @@ export function SceneName({
}: SceneNameProps): JSX.Element {
const [name, setName] = useState(initialName)
const [prevInitialName, setPrevInitialName] = useState(initialName)
+ // Mirror of the value currently held in the local field. Lets us tell a genuine
+ // external update (loading a resource, an AI-generated name) apart from an echo of
+ // the user's own edit arriving back through the form, so the render-phase
+ // reconciliation below can't overwrite a keystroke that hasn't round-tripped yet.
+ const latestNameRef = useRef(initialName)
if (initialName !== prevInitialName) {
setPrevInitialName(initialName)
- setName(initialName)
+ if (initialName !== latestNameRef.current) {
+ setName(initialName)
+ latestNameRef.current = initialName
+ }
}
const [isEditing, setIsEditing] = useState(forceEdit)
@@ -490,6 +498,10 @@ export function SceneName({
}
if (saveOnBlur && !isGeneratingMetadata && name !== initialName) {
debouncedOnBlurSave(name || '')
+ } else if (!saveOnBlur) {
+ // Commit any pending debounced change synchronously so a submit or
+ // validation that immediately follows blur reads the value the user sees.
+ debouncedOnChange.flush()
}
if (!forceEdit) {
setIsEditing(false)
@@ -509,6 +521,7 @@ export function SceneName({
value={name || ''}
readOnly={isGeneratingMetadata}
onChange={(e) => {
+ latestNameRef.current = e.target.value
setName(e.target.value)
if (forceEdit && !saveOnBlur) {
onChange?.(e.target.value)
@@ -650,9 +663,14 @@ function SceneDescription({
}: SceneDescriptionProps): JSX.Element | null {
const [description, setDescription] = useState(initialDescription)
const [prevInitialDescription, setPrevInitialDescription] = useState(initialDescription)
+ // See SceneName: keep external updates from clobbering an in-flight local edit.
+ const latestDescriptionRef = useRef(initialDescription)
if (initialDescription !== prevInitialDescription) {
setPrevInitialDescription(initialDescription)
- setDescription(initialDescription)
+ if (initialDescription !== latestDescriptionRef.current) {
+ setDescription(initialDescription)
+ latestDescriptionRef.current = initialDescription
+ }
}
const [isEditing, setIsEditing] = useState(forceEdit)
@@ -687,6 +705,8 @@ function SceneDescription({
const handleBlur = (): void => {
if (saveOnBlur && !isGeneratingMetadata && description !== initialDescription) {
debouncedOnBlurSaveDescription(description || '')
+ } else if (!saveOnBlur) {
+ debouncedOnDescriptionChange.flush()
}
if (!forceEdit) {
setIsEditing(false)
@@ -704,6 +724,7 @@ function SceneDescription({
maxLength={maxLength}
readOnly={isGeneratingMetadata}
onChange={(e) => {
+ latestDescriptionRef.current = e.target.value
setDescription(e.target.value)
if (forceEdit && !saveOnBlur) {
onChange?.(e.target.value)