: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 clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { useEffect, useState } from 'react'
import { AutoSizer } from 'lib/components/AutoSizer'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { Link } from 'lib/lemon-ui/Link'
import { CodeEditor } from 'lib/monaco/CodeEditor'
import { urls } from 'scenes/urls'
import { queryEditorLogic } from '~/queries/QueryEditor/queryEditorLogic'
import { QueryContext } from '~/queries/types'
export interface QueryEditorProps {
query: string
setQuery?: (query: string) => void
className?: string
aboveButton?: JSX.Element
context?: QueryContext
}
let i = 0
export function QueryEditor(props: QueryEditorProps): JSX.Element {
const [key] = useState(() => i++)
const [schema, setSchema] = useState<Record<string, any> | undefined>(undefined)
const { queryInput, error, inputChanged } = useValues(queryEditorLogic({ ...props, key }))
const { setQueryInput, saveQuery } = useActions(queryEditorLogic({ ...props, key }))
useEffect(() => {
let cancelled = false
import('~/queries/schema.json')
.then((m) => {
if (!cancelled) {
setSchema(m.default)
}
})
.catch((err) => {
if (!cancelled) {
console.warn('Failed to load query schema for editor', err)
}
})
return () => {
cancelled = true
}
}, [])
return (
<>
{props.context?.showQueryHelp ? (
<div className="mb-2 flex flex-row flex-wrap justify-between items-center">
<div>
Insight configurations follow a declarative schema. Edit them as code here. Open under{' '}
<Link to={urls.debugQuery(queryInput)}>/debug</Link>.
</div>
</div>
) : null}
<div
data-attr="query-editor"
className={clsx(
'flex flex-col p-2 bg-primary deprecated-space-y-2 resize-y overflow-auto min-h-80 rounded',
props.className
)}
>
<div className="flex-1">
<AutoSizer
disableWidth
renderProp={({ height }) =>
height ? (
<CodeEditor
className="border"
language="json"
value={queryInput}
onChange={(v) => setQueryInput(v ?? '')}
height={height}
schema={schema}
/>
) : null
}
/>
</div>
{error ? (
<div className="bg-danger text-white p-2">
<strong>Error parsing JSON:</strong> {error}
</div>
) : null}
{props.aboveButton}
<LemonButton
onClick={saveQuery}
type="primary"
status={error ? 'danger' : 'default'}
disabled={!props.setQuery || !!error || !inputChanged}
fullWidth
center
data-attr="query-editor-save"
>
{!props.setQuery ? 'No permission to update' : 'Update and run'}
</LemonButton>
</div>
</>
)
}