: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay-vision): stop observation feedback status sticking on saving (#82151)
coryslaterb9078c8bc6da
diff --git a/products/replay_vision/frontend/observations/ObservationLabelControl.tsx b/products/replay_vision/frontend/observations/ObservationLabelControl.tsxindex 03d1e695a998..84309c39f98b 100644--- a/products/replay_vision/frontend/observations/ObservationLabelControl.tsx+++ b/products/replay_vision/frontend/observations/ObservationLabelControl.tsx@@ -37,10 +37,9 @@ function FeedbackEditor({onBlur,}: ObservationLabelProps & { compact: boolean; onBlur?: () => void }): JSX.Element {const logic = observationLabelLogic({ observationId, initialLabel, onChange })- const { label, saving, feedbackDraft } = useValues(logic)+ const { saving, saveFailed, feedbackDraft, feedbackSynced } = useValues(logic)const { setFeedbackDraft } = useActions(logic)const canEdit = !useEditAccess(scannerUserAccessLevel)- const feedbackSynced = feedbackDraft === (label?.feedback ?? '')return (<div className="space-y-1">@@ -56,8 +55,11 @@ function FeedbackEditor({/>{canEdit && (<div className="flex justify-end">- <span className="text-xs text-muted" data-attr="replay-vision-label-feedback-status">- {saving || !feedbackSynced ? 'Saving…' : 'Saved'}+ <span+ className={`text-xs ${saveFailed ? 'text-danger' : 'text-muted'}`}+ data-attr="replay-vision-label-feedback-status"+ >+ {saveFailed ? 'Not saved' : saving || !feedbackSynced ? 'Saving…' : 'Saved'}</span></div>)}diff --git a/products/replay_vision/frontend/observations/observationLabelLogic.test.ts b/products/replay_vision/frontend/observations/observationLabelLogic.test.tsindex 7a8ab04eeddf..bc177ca8db00 100644--- a/products/replay_vision/frontend/observations/observationLabelLogic.test.ts+++ b/products/replay_vision/frontend/observations/observationLabelLogic.test.ts@@ -56,6 +56,33 @@ describe('observationLabelLogic feedback autosave', () => {expect(onChange).toHaveBeenCalledWith({ is_correct: isCorrect, feedback: 'scanner missed the refund step' })})+ it('settles as synced when the API trims the feedback it stores', async () => {+ // The API trims before storing, so the echoed label never matches a draft with trailing whitespace.+ ;(visionObservationsLabelCreate as jest.Mock).mockImplementation((_team, _id, body) =>+ Promise.resolve({ ...body, feedback: body.feedback.trim() })+ )+ mountLogic(false)+ logic.actions.setFeedbackDraft('scanner missed the refund step\n')+ await jest.advanceTimersByTimeAsync(900)++ expect(logic.values.feedbackSynced).toBe(true)+ expect(logic.values.saving).toBe(false)+ // The stored value already covers the draft, so an unchanged draft must not keep resaving.+ logic.actions.setFeedbackDraft('scanner missed the refund step ')+ await jest.advanceTimersByTimeAsync(900)+ expect(visionObservationsLabelCreate).toHaveBeenCalledTimes(1)+ })++ it('reports a failed autosave instead of staying on saving', async () => {+ ;(visionObservationsLabelCreate as jest.Mock).mockRejectedValue({ detail: 'nope' })+ mountLogic(false)+ logic.actions.setFeedbackDraft('scanner missed the refund step')+ await jest.advanceTimersByTimeAsync(900)++ expect(logic.values.saveFailed).toBe(true)+ expect(logic.values.saving).toBe(false)+ })+it('a rating click during the debounce wins over the pending autosave', async () => {mountLogic(false)logic.actions.setFeedbackDraft('scanner missed the refund step')diff --git a/products/replay_vision/frontend/observations/observationLabelLogic.ts b/products/replay_vision/frontend/observations/observationLabelLogic.tsindex 100ada8da96e..f16321c71d21 100644--- a/products/replay_vision/frontend/observations/observationLabelLogic.ts+++ b/products/replay_vision/frontend/observations/observationLabelLogic.ts@@ -1,4 +1,4 @@-import { MakeLogicType, actions, kea, key, listeners, path, props, propsChanged, reducers } from 'kea'+import { MakeLogicType, actions, kea, key, listeners, path, props, propsChanged, reducers, selectors } from 'kea'import { lemonToast } from 'lib/lemon-ui/LemonToast'import { teamLogic } from 'scenes/teamLogic'@@ -6,6 +6,12 @@ import { teamLogic } from 'scenes/teamLogic'import { visionObservationsLabelCreate, visionObservationsLabelDestroy } from '../generated/api'import type { ReplayObservationLabelApi } from '../generated/api.schemas'+/** The API trims feedback before storing it, so compare on those terms: a draft ending in a newline is+ * already saved, and treating it as unsaved leaves the status stuck on "Saving…" with nothing in flight. */+function normalizeFeedback(feedback: string | null | undefined): string {+ return (feedback ?? '').trim()+}+export interface ObservationLabelLogicProps {observationId: string/** The label as loaded with the observation; seeds the logic state on mount. */@@ -17,7 +23,9 @@ export interface ObservationLabelLogicProps {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface observationLabelLogicValues {feedbackDraft: string+ feedbackSynced: booleanlabel: ReplayObservationLabelApi | null+ saveFailed: booleansaving: boolean}@@ -39,6 +47,9 @@ export interface observationLabelLogicActions {setFeedbackDraft: (feedback: string) => {feedback: string}+ setSaveFailed: (saveFailed: boolean) => {+ saveFailed: boolean+ }setSaving: (saving: boolean) => {saving: boolean}@@ -47,6 +58,9 @@ export interface observationLabelLogicActions {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface observationLabelLogicMeta {key: string+ __keaTypeGenInternalSelectorTypes: {+ feedbackSynced: (feedbackDraft: string, label: ReplayObservationLabelApi | null) => boolean+ }}export type observationLabelLogicType = MakeLogicType<@@ -67,6 +81,7 @@ export const observationLabelLogic = kea<observationLabelLogicType>([clearRating: true,labelUpdated: (label: ReplayObservationLabelApi | null) => ({ label }),setSaving: (saving: boolean) => ({ saving }),+ setSaveFailed: (saveFailed: boolean) => ({ saveFailed }),setFeedbackDraft: (feedback: string) => ({ feedback }),}),@@ -83,6 +98,16 @@ export const observationLabelLogic = kea<observationLabelLogicType>([setSaving: (_, { saving }) => saving,},],+ saveFailed: [+ false,+ {+ setSaveFailed: (_, { saveFailed }) => saveFailed,+ // A fresh edit or rating click is a retry, so the last failure stops being the current state.+ setFeedbackDraft: () => false,+ rate: () => false,+ clearRating: () => false,+ },+ ],feedbackDraft: [props.initialLabel?.feedback ?? '',{@@ -94,6 +119,14 @@ export const observationLabelLogic = kea<observationLabelLogicType>([],})),+ selectors({+ feedbackSynced: [+ (s) => [s.feedbackDraft, s.label],+ (feedbackDraft: string, label: ReplayObservationLabelApi | null): boolean =>+ normalizeFeedback(feedbackDraft) === normalizeFeedback(label?.feedback),+ ],+ }),+propsChanged(({ actions, props, values, cache }, oldProps) => {// Adopt a label reloaded from the server (e.g. a teammate's change) so a stale local// value or pending autosave can't overwrite it.@@ -114,7 +147,7 @@ export const observationLabelLogic = kea<observationLabelLogicType>([setFeedbackDraft: async ({ feedback }, breakpoint) => {const label = values.label// Feedback only exists on a rated observation and applies to thumbs-up and thumbs-down alike.- if (!label || (label.feedback ?? '') === feedback) {+ if (!label || normalizeFeedback(label.feedback) === normalizeFeedback(feedback)) {return}const epoch = cache.labelEpoch ?? 0@@ -146,6 +179,7 @@ export const observationLabelLogic = kea<observationLabelLogicType>([actions.labelUpdated(label)props.onChange?.(label)} catch (error: any) {+ actions.setSaveFailed(true)lemonToast.error(`Failed to save rating${error.detail ? `: ${error.detail}` : ''}`)} finally {actions.setSaving(false)@@ -168,6 +202,7 @@ export const observationLabelLogic = kea<observationLabelLogicType>([actions.labelUpdated(null)props.onChange?.(null)} catch (error: any) {+ actions.setSaveFailed(true)lemonToast.error(`Failed to remove rating${error.detail ? `: ${error.detail}` : ''}`)} finally {actions.setSaving(false)