: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
import { MakeLogicType, actions, kea, key, listeners, path, props, propsChanged, reducers, selectors } from 'kea'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { QueryEditorProps } from '~/queries/QueryEditor/QueryEditor'
import { Node } from '~/queries/schema/schema-general'
function prettyJSON(source: string): string {
try {
return JSON.stringify(JSON.parse(source), null, 2) + '\n'
} catch {
return source
}
}
export interface QueryEditorLogicProps extends QueryEditorProps {
key: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface queryEditorLogicValues {
JSONQuery: Node | null
error: string | null
inputChanged: boolean
parsedQuery: {
error: string | null
JSONQuery: Node | null
}
queryInput: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface queryEditorLogicActions {
saveQuery: () => {
value: true
}
setQueryInput: (queryInput: string) => {
queryInput: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface queryEditorLogicMeta {
key: number
__keaTypeGenInternalSelectorTypes: {
parsedQuery: (queryInput: string) => {
error: string | null
JSONQuery: Node | null
}
JSONQuery: (parsedQuery: { error: string | null; JSONQuery: Node | null }) => Node | null
error: (parsedQuery: { error: string | null; JSONQuery: Node | null }) => string | null
inputChanged: (query: string, queryInput: string) => boolean
}
}
export type queryEditorLogicType = MakeLogicType<
queryEditorLogicValues,
queryEditorLogicActions,
QueryEditorLogicProps,
queryEditorLogicMeta
>
export const queryEditorLogic = kea<queryEditorLogicType>([
path(['queries', 'QueryEditor', 'queryEditorLogic']),
props({} as QueryEditorLogicProps),
key((props) => props.key),
propsChanged(({ actions, props }, oldProps) => {
if (props.query !== oldProps.query) {
actions.setQueryInput(prettyJSON(props.query))
}
}),
actions({
saveQuery: true,
setQueryInput: (queryInput: string) => ({ queryInput }),
}),
reducers(({ props }) => ({
queryInput: [prettyJSON(props.query), { setQueryInput: (_, { queryInput }) => queryInput }],
})),
selectors({
parsedQuery: [
(s) => [s.queryInput],
(query: string): { JSONQuery: Node | null; error: string | null } => {
let JSONQuery: Node | null = null
let error = null
try {
JSONQuery = JSON.parse(query)
} catch (e: any) {
error = e.message
}
return { JSONQuery, error }
},
],
JSONQuery: [(s) => [s.parsedQuery], ({ JSONQuery }): Node | null => JSONQuery],
error: [(s) => [s.parsedQuery], ({ error }): string | null => error],
inputChanged: [(s, p) => [p.query, s.queryInput], (query: string, queryInput: string) => query !== queryInput],
}),
listeners(({ actions, props, values }) => ({
saveQuery: () => {
if (values.error) {
lemonToast.error(`Error parsing JSON: ${values.error}`)
} else {
const withoutFormatting = JSON.stringify(JSON.parse(values.queryInput))
actions.setQueryInput(prettyJSON(withoutFormatting))
props.setQuery?.(withoutFormatting)
}
},
})),
])