: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, kea } from 'kea'
export interface CyclotronJobInputLogicProps {
fieldKey: string
initialValue?: string
onChange?: (value: string) => void
}
/**
* Format a JSON value for display in the editor
*/
export function formatJsonValue(value: any): string {
if (value === undefined || value === null) {
return '{}'
}
return typeof value !== 'string' ? JSON.stringify(value, null, 2) : value
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cyclotronJobInputLogicValues {
error: string | null
hasError: boolean
jsonValue: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cyclotronJobInputLogicActions {
setError: (error: string | null) => {
error: string | null
}
setJsonValue: (value: string) => {
value: string
}
validateJson: (value: string) => {
value: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cyclotronJobInputLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
hasError: (error: any) => boolean
}
}
export type cyclotronJobInputLogicType = MakeLogicType<
cyclotronJobInputLogicValues,
cyclotronJobInputLogicActions,
CyclotronJobInputLogicProps,
cyclotronJobInputLogicMeta
>
export const cyclotronJobInputLogic = kea<cyclotronJobInputLogicType>({
path: ['lib', 'components', 'CyclotronJob', 'cyclotronJobInputLogic'],
props: {} as CyclotronJobInputLogicProps,
key: (props: CyclotronJobInputLogicProps) => `json_validation_${props.fieldKey}`,
actions: {
setJsonValue: (value: string) => ({ value }),
validateJson: (value: string) => ({ value }),
setError: (error: string | null) => ({ error }),
},
reducers: {
jsonValue: [
'' as string,
{
setJsonValue: (_state: string, { value }: { value: string }) => value,
},
],
error: [
null as string | null,
{
setError: (_state: string | null, { error }: { error: string | null }) => error,
},
],
},
selectors: {
hasError: [(s: any) => [s.error], (error: string | null) => !!error],
},
listeners: ({ actions, props }) => ({
setJsonValue: async ({ value }, breakpoint) => {
// Don't validate empty values
if (!value || value.trim() === '') {
actions.setError(null)
return
}
// Notify parent about the change
props.onChange?.(value)
// Wait for 600ms before validating, and break if setJsonValue is called again
await breakpoint(600)
try {
JSON.parse(value)
actions.setError(null)
} catch (e: any) {
// Prettify common errors
let errorMessage = e.message || 'Invalid JSON'
// Special case for undefined
if (errorMessage.includes("'u'") && value.includes('undefined')) {
errorMessage = "Error: 'undefined' is not allowed in JSON. Use null instead."
}
actions.setError(errorMessage)
}
},
}),
// Initialize on props changes or mounting
propsChanged({ actions, props }: { actions: any; props: CyclotronJobInputLogicProps }) {
if (props.initialValue !== undefined) {
actions.setJsonValue(formatJsonValue(props.initialValue))
}
},
})