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.

feat(feature-flags): pretty-print json payloads in the flag editor (#64930)

Patriciod2cd83b70d7d
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index c96d7d937964..1e65592475a7 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -5125,9 +5125,9 @@ snapshots:
scenes-app-experiments--experiment-asymmetric-intervals--light:
hash: v1.k794b7964.c8bb88e7e610f901989a0a06ad295ccb0cf9e20366a3f5bafac532116be38ca9.TsIYV_gxMZ9GzBxCBLMPuqhxiOVIg-eZ4wivz65yOfA
scenes-app-experiments--experiment-frequentist-five-variants--dark:
- hash: v1.k794b7964.09d23fd44208249b4fd1555fcbdb941118bc2825dc7142bd6ba96335c9cb8f32.AipSypBKnQguXtQO9tkCsdO9IVj-_0bHqjCnFe0BE-U
+ hash: v1.k794b7964.57adc242204513880214e9d25ef1db09215b72b6f1e17d740d403fc65aee2ace.D3Me8QFrJqQ86K0jGGoji9RisRhL6sy9jSRBUigO_nA
scenes-app-experiments--experiment-frequentist-five-variants--light:
- hash: v1.k794b7964.a9f586e457e952a00340ab550abf3540b2c72f710d1ce01b91f5101eb786b4cc.1q1GJhPP-svnMMibJ7Vjg3n-81mY9R_djneShAQAtmc
+ hash: v1.k794b7964.9c25acd69f9c4408cfff5677fdc0c67284f0e4f76a47f4319ca1aba544131133.H_M25fn6pdw3IEjyvLVRYMSz497BZmaEuuWVvQ8UNYM
scenes-app-experiments--experiment-frequentist-two-variants--dark:
hash: v1.k794b7964.e09386503a4afe1b35afea46561b001c53fe4a0b33462b38411cf946062964d9.Wt1HdAZFHB1UJRhWLISfuBT-PnBvc6KPpc3VT1D4kF4
scenes-app-experiments--experiment-frequentist-two-variants--light:
@@ -5177,9 +5177,9 @@ snapshots:
scenes-app-feature-flags--edit-multi-variate-feature-flag--light:
hash: v1.k794b7964.a12e6c7ca6f1d704063908b2ca71ecae8a0e30411d6de77431d513488cad6e89.tMbL5NI4HivFAGYfugwyJ1MM1v77HdYWoyOYtAcB4Es
scenes-app-feature-flags--edit-remote-config-feature-flag--dark:
- hash: v1.k794b7964.9b5ce101c55c2b4edadacb72617f501b465bfd3ede36ef40862e1bc3a049e4b7.uE348cJaMeE3D-C0Olq-gJr1qEL8TzirwHjFC4EU4N0
+ hash: v1.k794b7964.908e1b828fbe003baf0b5acc88dec6d318d7e2ee676eb16628b70f61418d12d8.JIOJS_5MwkfSDHECNlVYcD1YPzX4FesxDFrNF9T3Nns
scenes-app-feature-flags--edit-remote-config-feature-flag--light:
- hash: v1.k794b7964.dd65da2ecfb1fec107e0807b528a4f7b834845992e4354302c9e1706f3befaee.4y2oUfTDfnBqIotvBSrOyIGJ8_NyX-fhIp4Qiw4JFyc
+ hash: v1.k794b7964.1960b35293acf15bf2309103ae87146a389026c41d9d18e9859ca541557f0d2c.C8HtqN3zIRdxDu49CHn8s-_-2xQeThJvtVOw1vQCfiw
scenes-app-feature-flags--feature-flag-not-found--dark:
hash: v1.k794b7964.36bbf6870a8070c1b5284a2bba1b659324f1986f1af18220b97d0ef22af5b314.0fdhZmbn_ck5kOOvzOzuMeg6oFHGAonFrOQ037BzwCU
scenes-app-feature-flags--feature-flag-not-found--light:
@@ -6381,9 +6381,9 @@ snapshots:
scenes-app-settings-organization--settings-organization-members--light:
hash: v1.k794b7964.d6ce401695f8040ed277c5f47f729d9d36f6a192842f8e2f19993396b117b94c.Sm82nQANYBGJevASnH2mK1W7UWCff6tw-pDZjrbOnKM
scenes-app-settings-organization--settings-organization-proxy--dark:
- hash: v1.k794b7964.e7c5d07c7381c53156a39fea68dffc63cb0a003f7361bd9cad4458621826ce0d.C5IxnQWSaa9bgI5yyyLD1HMzWddGOS8YofMkWSh33Kg
+ hash: v1.k794b7964.0ae4fc0d3d6b6299e1ea31cc5ec6c1a809f6414be2ddfac6991aea253125d148.BSdPMEIJsqGnJcy5sfzwOYzn949g4FDgbLfLqGvpOiM
scenes-app-settings-organization--settings-organization-proxy--light:
- hash: v1.k794b7964.f5f30746d5c24161e1d5aa85fc83543f306de2f572e1b93418514754631bfea5.ZHGqmFqo-tZh9yOm5E7urveonhv-nB_hg1eo45Bz35A
+ hash: v1.k794b7964.ce4e2dc16aa03778caa4340c89e35552309dc06161e272f50ea8fc4946c98b84.z5lS6F0qm_VP283uRV8VLF_6gvULrNukTvNNCl5Y7bA
scenes-app-settings-organization--settings-organization-roles--dark:
hash: v1.k794b7964.2b30448b89bb64232fa51cb60282cf6bf34e7e40dfa963bc510e2f0f3044977e.HgWf-ieys0UwHAqtbOpfF8R270jLauN9VkrrLarIAXA
scenes-app-settings-organization--settings-organization-roles--light:
diff --git a/frontend/src/scenes/feature-flags/JSONEditorInput.tsx b/frontend/src/scenes/feature-flags/JSONEditorInput.tsx
index 135a3f668d8c..c2a2559a496b 100644
--- a/frontend/src/scenes/feature-flags/JSONEditorInput.tsx
+++ b/frontend/src/scenes/feature-flags/JSONEditorInput.tsx
@@ -1,6 +1,6 @@
import './JSONEditorInput.scss'
-import { useMemo, useState } from 'react'
+import { useEffect, useRef, useState } from 'react'
import { CodeEditorResizeable } from 'lib/monaco/CodeEditorResizable'
@@ -13,18 +13,61 @@ interface EditorProps {
placeholder?: string
}
+// Pretty-print objects and JSON-string values (objects/arrays) with 2-space indentation so payloads
+// are readable. Bare strings, numbers, and partial/invalid input are passed through untouched so the
+// editor never fights the user mid-typing.
+function formatJSON(value: JsonType): string {
+ if (typeof value === 'object') {
+ // Includes `null` -> 'null', matching the prior rendering for null payloads
+ return JSON.stringify(value, null, 2)
+ }
+ const str = value?.toString() || ''
+ try {
+ const parsed = JSON.parse(str)
+ if (parsed !== null && typeof parsed === 'object') {
+ return JSON.stringify(parsed, null, 2)
+ }
+ } catch {
+ // Not valid JSON (e.g. still being typed) — leave it as-is
+ }
+ return str
+}
+
export function JSONEditorInput({ onChange, placeholder, value = '', readOnly = false }: EditorProps): JSX.Element {
const [focused, setFocused] = useState(false)
- // Memoize the string conversion to avoid recalculating on every render
- // In practice, `value` is always a string, but the type allows for object too
- const valString = useMemo(
- () => (typeof value === 'object' ? JSON.stringify(value, null, 2) : value?.toString() || ''),
- [value]
- )
+ // The editor is the source of truth for what's displayed. We seed it from `value` (pretty-printed)
+ // and only re-sync when `value` changes externally — never on the echo of our own onChange, which
+ // would reformat while the user types and jump the cursor.
+ const [valString, setValString] = useState<string>(() => formatJSON(value))
+ const lastEmitted = useRef<string | undefined>(undefined)
+
+ useEffect(() => {
+ // Normalize to a string so the comparison is type-safe even when `value` is an object —
+ // `onChange` only ever emits strings, so an object `value` is always an external change.
+ const incoming = typeof value === 'object' ? JSON.stringify(value) : (value?.toString() ?? '')
+ if (incoming !== lastEmitted.current) {
+ setValString(formatJSON(value))
+ }
+ }, [value])
+
+ const handleChange = (val: string | undefined): void => {
+ lastEmitted.current = val
+ setValString(val ?? '')
+ onChange?.(val)
+ }
const onFocus = (): void => setFocused(true)
- const onBlur = (): void => setFocused(false)
+ const onBlur = (): void => {
+ setFocused(false)
+ // Tidy up the user's edits once they leave the field, but only if it actually changes something.
+ if (!readOnly) {
+ const formatted = formatJSON(valString)
+ if (formatted !== valString) {
+ handleChange(formatted)
+ }
+ }
+ }
return (
<div className="JsonEditorInput" onFocus={onFocus} onBlur={onBlur}>
@@ -59,7 +102,7 @@ export function JSONEditorInput({ onChange, placeholder, value = '', readOnly =
quickSuggestions: false,
contextmenu: false,
}}
- onChange={onChange}
+ onChange={handleChange}
/>
{!focused && !valString && placeholder && (
<div className="placeholder">