diff --git a/apps/web/src/dashboard/query-builder.js b/apps/web/src/dashboard/query-builder.js index 9161475..b1b4fa7 100644 --- a/apps/web/src/dashboard/query-builder.js +++ b/apps/web/src/dashboard/query-builder.js @@ -331,26 +331,33 @@ class QueryBuilder { } } -// Compact-but-readable JSON formatter. Inlines any subtree that fits on a single -// line within MAX_INLINE chars; otherwise breaks across lines with 2-space indent. +// Standard 2-space JSON prettifier with one twist: any subtree whose single-line +// form fits within MAX_INLINE chars at its current column gets inlined. Breaks +// always use clean 2-space indent — never key-aligned. const MAX_INLINE = 60; -function formatQueryJson(value, indent = 0) { +function formatQueryJson(value, indent = 0, startCol = indent) { const inline = inlineForm(value); - if (inline.length + indent <= MAX_INLINE) return inline; + if (startCol + inline.length <= MAX_INLINE) return inline; + + const childIndent = indent + 2; + const pad = ' '.repeat(childIndent); + const close = ' '.repeat(indent); + if (Array.isArray(value)) { if (value.length === 0) return '[]'; - const pad = ' '.repeat(indent + 2); - const close = ' '.repeat(indent); - const items = value.map(v => pad + formatQueryJson(v, indent + 2)); + const items = value.map(v => pad + formatQueryJson(v, childIndent)); return '[\n' + items.join(',\n') + '\n' + close + ']'; } if (value && typeof value === 'object') { const entries = Object.entries(value); if (entries.length === 0) return '{}'; - const pad = ' '.repeat(indent + 2); - const close = ' '.repeat(indent); - const items = entries.map(([k, v]) => pad + JSON.stringify(k) + ': ' + formatQueryJson(v, indent + 2 + JSON.stringify(k).length + 2)); + const items = entries.map(([k, v]) => { + const keyStr = JSON.stringify(k) + ': '; + // Recurse at childIndent for break depth, but tell the recursion the + // value's actual column for the inline-fit check. + return pad + keyStr + formatQueryJson(v, childIndent, childIndent + keyStr.length); + }); return '{\n' + items.join(',\n') + '\n' + close + '}'; } return JSON.stringify(value);