: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, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { UrlTriggerConfig } from 'lib/components/IngestionControls/types'
const NEW_URL_TRIGGER = { url: '', matching: 'regex' }
export function isStringWithLength(x: unknown): x is string {
return typeof x === 'string' && x.trim() !== ''
}
export function ensureAnchored(url: string): string {
url = url.startsWith('^') ? url.substring(1) : url
url = url.endsWith('$') ? url.substring(0, url.length - 1) : url
return `^${url}$`
}
export interface UrlConfigLogicProps {
logicKey: string
initialUrlTriggerConfig: UrlTriggerConfig[]
onChange: (urlTriggerConfig: UrlTriggerConfig[]) => void
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface urlConfigLogicValues {
checkUrlTrigger: string
checkUrlTriggerResults: {
[key: number]: boolean
}
editUrlTriggerIndex: number | null
isAddUrlTriggerConfigFormVisible: boolean
isProposedUrlTriggerSubmitting: boolean
isProposedUrlTriggerValid: boolean
proposedUrlTrigger: UrlTriggerConfig
proposedUrlTriggerAllErrors: Record<string, any>
proposedUrlTriggerChanged: boolean
proposedUrlTriggerErrors: DeepPartialMap<UrlTriggerConfig, ValidationErrorType>
proposedUrlTriggerHasErrors: boolean
proposedUrlTriggerManualErrors: Record<string, any>
proposedUrlTriggerTouched: boolean
proposedUrlTriggerTouches: Record<string, boolean>
proposedUrlTriggerValidationErrors: DeepPartialMap<UrlTriggerConfig, ValidationErrorType>
showProposedUrlTriggerErrors: boolean
urlTriggerConfig: UrlTriggerConfig[]
urlTriggerInputValidationWarning: string | null
urlTriggerToEdit: {
matching: string
url: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface urlConfigLogicActions {
addUrlTrigger: (urlTriggerConfig: UrlTriggerConfig) => {
urlTriggerConfig: UrlTriggerConfig
}
cancelProposingUrlTrigger: () => {
value: true
}
newUrlTrigger: () => {
value: true
}
removeUrlTrigger: (index: number) => {
index: number
}
resetProposedUrlTrigger: (values?: UrlTriggerConfig) => {
values?: UrlTriggerConfig
}
setCheckUrlBlocklist: (url: string) => {
url: string
}
setCheckUrlTrigger: (url: string) => {
url: string
}
setEditUrlTriggerIndex: (originalIndex: number | null) => {
originalIndex: number | null
}
setProposedUrlTriggerManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setProposedUrlTriggerValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setProposedUrlTriggerValues: (values: DeepPartial<UrlTriggerConfig>) => {
values: DeepPartial<UrlTriggerConfig>
}
setUrlTriggerConfig: (urlTriggerConfig: UrlTriggerConfig[]) => {
urlTriggerConfig: UrlTriggerConfig[]
}
submitProposedUrlTrigger: () => {
value: boolean
}
submitProposedUrlTriggerFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitProposedUrlTriggerRequest: (proposedUrlTrigger: UrlTriggerConfig) => {
proposedUrlTrigger: UrlTriggerConfig
}
submitProposedUrlTriggerSuccess: (proposedUrlTrigger: UrlTriggerConfig) => {
proposedUrlTrigger: UrlTriggerConfig
}
touchProposedUrlTriggerField: (key: string) => {
key: string
}
updateUrlTrigger: (
index: number,
urlTriggerConfig: UrlTriggerConfig
) => {
index: number
urlTriggerConfig: UrlTriggerConfig
}
validateUrlInput: (
url: string,
type: 'blocklist' | 'trigger'
) => {
type: 'blocklist' | 'trigger'
url: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface urlConfigLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
isAddUrlTriggerConfigFormVisible: (editUrlTriggerIndex: number | null) => boolean
urlTriggerToEdit: (
urlTriggerConfig: UrlTriggerConfig[],
editUrlTriggerIndex: number | null
) => {
matching: string
url: string
}
checkUrlTriggerResults: (
checkUrlTrigger: string,
urlTriggerConfig: UrlTriggerConfig[]
) => {
[key: number]: boolean
}
}
}
export type urlConfigLogicType = MakeLogicType<
urlConfigLogicValues,
urlConfigLogicActions,
UrlConfigLogicProps,
urlConfigLogicMeta
>
export const urlConfigLogic = kea<urlConfigLogicType>([
props({} as UrlConfigLogicProps),
key((props) => props.logicKey),
path((key) => ['lib', 'components', 'IngestionControls', 'triggers', 'urlConfigLogic', key]),
actions({
setUrlTriggerConfig: (urlTriggerConfig: UrlTriggerConfig[]) => ({ urlTriggerConfig }),
addUrlTrigger: (urlTriggerConfig: UrlTriggerConfig) => ({ urlTriggerConfig }),
removeUrlTrigger: (index: number) => ({ index }),
updateUrlTrigger: (index: number, urlTriggerConfig: UrlTriggerConfig) => ({
index,
urlTriggerConfig,
}),
setEditUrlTriggerIndex: (originalIndex: number | null) => ({ originalIndex }),
newUrlTrigger: true,
cancelProposingUrlTrigger: true,
setCheckUrlTrigger: (url: string) => ({ url }),
setCheckUrlBlocklist: (url: string) => ({ url }),
validateUrlInput: (url: string, type: 'trigger' | 'blocklist') => ({ url, type }),
}),
reducers(({ props }) => ({
urlTriggerConfig: [
props.initialUrlTriggerConfig as UrlTriggerConfig[],
{
setUrlTriggerConfig: (_, { urlTriggerConfig }) => urlTriggerConfig,
addUrlTrigger: (state, { urlTriggerConfig }) => [...(state ?? []), urlTriggerConfig],
updateUrlTrigger: (state, { index, urlTriggerConfig: newUrlTriggerConfig }) =>
(state ?? []).map((triggerConfig, i) => (i === index ? newUrlTriggerConfig : triggerConfig)),
removeUrlTrigger: (state, { index }) => {
return (state ?? []).filter((_, i) => i !== index)
},
},
],
editUrlTriggerIndex: [
null as number | null,
{
setEditUrlTriggerIndex: (_, { originalIndex }) => originalIndex,
removeUrlTrigger: (editUrlTriggerIndex, { index }) =>
editUrlTriggerIndex !== null && index < editUrlTriggerIndex
? editUrlTriggerIndex - 1
: index === editUrlTriggerIndex
? null
: editUrlTriggerIndex,
newUrlTrigger: () => -1,
updateUrlTrigger: () => null,
addUrlTrigger: () => null,
cancelProposingUrlTrigger: () => null,
},
],
checkUrlTrigger: [
'' as string,
{
setCheckUrlTrigger: (_, { url }) => url,
},
],
urlTriggerInputValidationWarning: [
null as string | null,
{
validateUrlInput: (_, { url, type }) => {
if (type !== 'trigger') {
return _
}
// Check if it ends with a TLD
if (/\.[a-z]{2,}\/?$/i.test(url)) {
const sanitizedUrl = url.endsWith('/') ? url.slice(0, -1) : url
return `If you want to match all paths of a domain, you should write " ${sanitizedUrl}(/.*)? ". This would match:
${sanitizedUrl}, ${sanitizedUrl}/, ${sanitizedUrl}/page, etc. Don't forget to include https:// at the beginning of the url.`
}
return null
},
},
],
})),
selectors({
isAddUrlTriggerConfigFormVisible: [
(s) => [s.editUrlTriggerIndex],
(editUrlTriggerIndex: number | null) => editUrlTriggerIndex === -1,
],
urlTriggerToEdit: [
(s) => [s.urlTriggerConfig, s.editUrlTriggerIndex],
(urlTriggerConfig: UrlTriggerConfig[], editUrlTriggerIndex: number | null) => {
if (
editUrlTriggerIndex === null ||
editUrlTriggerIndex === -1 ||
!urlTriggerConfig?.[editUrlTriggerIndex]
) {
return NEW_URL_TRIGGER
}
return urlTriggerConfig[editUrlTriggerIndex]
},
],
checkUrlTriggerResults: [
(s) => [s.checkUrlTrigger, s.urlTriggerConfig],
(checkUrl: string, urlTriggerConfig: UrlTriggerConfig[]): { [key: number]: boolean } => {
if (!checkUrl.trim() || !urlTriggerConfig) {
return {}
}
const results: { [key: number]: boolean } = {}
urlTriggerConfig.forEach((trigger, index) => {
try {
const regex = new RegExp(trigger.url)
results[index] = regex.test(checkUrl)
} catch {
results[index] = false
}
})
return results
},
],
}),
forms(({ values, actions }) => ({
proposedUrlTrigger: {
defaults: { url: '', matching: 'regex' } as UrlTriggerConfig,
errors: ({ url }) => ({
url: !url
? 'Must have a URL'
: (() => {
try {
new RegExp(url)
return undefined
} catch {
return 'Invalid regex pattern'
}
})(),
}),
submit: async ({ url, matching }) => {
if (values.editUrlTriggerIndex !== null && values.editUrlTriggerIndex >= 0) {
actions.updateUrlTrigger(values.editUrlTriggerIndex, { url: ensureAnchored(url), matching })
} else {
actions.addUrlTrigger({ url: ensureAnchored(url), matching })
}
},
},
})),
listeners(({ actions, values, props }) => ({
setEditUrlTriggerIndex: () => {
actions.setProposedUrlTriggerValue('url', values.urlTriggerToEdit.url)
actions.setProposedUrlTriggerValue('matching', values.urlTriggerToEdit.matching)
},
addUrlTrigger: () => props.onChange(values.urlTriggerConfig),
removeUrlTrigger: () => props.onChange(values.urlTriggerConfig),
updateUrlTrigger: () => props.onChange(values.urlTriggerConfig),
submitProposedUrlTriggerSuccess: () => {
actions.setEditUrlTriggerIndex(null)
actions.resetProposedUrlTrigger()
},
setProposedUrlTriggerValue: ({ name, value }) => {
const fieldName = Array.isArray(name) ? name[0] : name
if (fieldName === 'url') {
actions.validateUrlInput(value || '', 'trigger')
}
},
})),
])