Skip to content

: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.

54.7 KB
Raw Blame
import { MakeLogicType, actions, connect, defaults, kea, listeners, path, props, reducers, selectors } from 'kea'

import { filterTestAccountsDefaultsLogic } from 'scenes/settings/environment/filterTestAccountDefaultsLogic'

import type { ActivityEventsWidgetConfig } from '../../generated/widget-configs.zod'
import { isWidgetConfigValidationError } from '../../utils'
import { resolveWidgetFilterTestAccounts } from '../../widget_types/widgetConfigShared'
import type { WidgetDateFromValue } from '../../widget_types/widgetConfigShared'
import {
    widgetEditModalFilterTestAccountsActions,
    widgetEditModalListFieldActions,
    widgetEditModalPropSelectors,
    widgetEditModalTileActions,
    buildWidgetTileMetadataPatch,
    getWidgetEditModalTileDefaults,
} from '../editWidgetModalBuilders'
import type { DashboardWidgetEditModalProps } from '../registry'
import {
    parseActivityEventsWidgetConfig,
    validateActivityEventsWidgetConfigInput,
    type ActivityEventsWidgetFieldErrors,
} from './activityEventsWidgetConfigValidation'

export type EditActivityEventsWidgetModalLogicProps = Omit<DashboardWidgetEditModalProps, 'isOpen'>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editActivityEventsWidgetModalLogicValues {
    filterTestAccountsDefault: boolean // filterTestAccountsDefaultsLogic
    activeFieldErrors: ActivityEventsWidgetFieldErrors
    defaultTitle: string
    fieldErrors: ActivityEventsWidgetFieldErrors
    filterTestAccounts: boolean
    limit: number
    onClose: () => void
    saveDisabledReason: string | undefined
    saving: boolean
    tileDescription: string
    tileName: string
    validation:
        | {
              fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
              success: false
          }
        | {
              config: {
                  dateRange?:
                      | {
                            date_from?:
                                | '-14d'
                                | '-1h'
                                | '-1M'
                                | '-24h'
                                | '-30d'
                                | '-30M'
                                | '-3h'
                                | '-7d'
                                | '-90d'
                                | null
                                | undefined
                        }
                      | null
                      | undefined
                  eventName?: string | null | undefined
                  filterTestAccounts?: boolean | null | undefined
                  limit: number
                  properties?:
                      | {
                            key: string
                            label?: string | null | undefined
                            operator:
                                | 'between'
                                | 'ends_with'
                                | 'exact'
                                | 'flag_evaluates_to'
                                | 'gt'
                                | 'gte'
                                | 'icontains'
                                | 'icontains_multi'
                                | 'in'
                                | 'is_cleaned_path_exact'
                                | 'is_cleaned_path_not_exact'
                                | 'is_date_after'
                                | 'is_date_before'
                                | 'is_date_exact'
                                | 'is_not'
                                | 'is_not_set'
                                | 'is_set'
                                | 'lt'
                                | 'lte'
                                | 'max'
                                | 'min'
                                | 'not_between'
                                | 'not_ends_with'
                                | 'not_icontains'
                                | 'not_icontains_multi'
                                | 'not_in'
                                | 'not_regex'
                                | 'not_starts_with'
                                | 'regex'
                                | 'semver_caret'
                                | 'semver_eq'
                                | 'semver_gt'
                                | 'semver_gte'
                                | 'semver_lt'
                                | 'semver_lte'
                                | 'semver_neq'
                                | 'semver_tilde'
                                | 'semver_wildcard'
                                | 'starts_with'
                            type: 'event' | 'person'
                            value?: boolean | number | string | (boolean | number | string)[] | null | undefined
                        }[]
                      | null
                      | undefined
                  widgetFilters?:
                      | Record<
                            string,
                            {
                                filterId: string
                                operator:
                                    | 'between'
                                    | 'ends_with'
                                    | 'exact'
                                    | 'flag_evaluates_to'
                                    | 'gt'
                                    | 'gte'
                                    | 'icontains'
                                    | 'icontains_multi'
                                    | 'in'
                                    | 'is_cleaned_path_exact'
                                    | 'is_cleaned_path_not_exact'
                                    | 'is_date_after'
                                    | 'is_date_before'
                                    | 'is_date_exact'
                                    | 'is_not'
                                    | 'is_not_set'
                                    | 'is_set'
                                    | 'lt'
                                    | 'lte'
                                    | 'max'
                                    | 'min'
                                    | 'not_between'
                                    | 'not_ends_with'
                                    | 'not_icontains'
                                    | 'not_icontains_multi'
                                    | 'not_in'
                                    | 'not_regex'
                                    | 'not_starts_with'
                                    | 'regex'
                                    | 'semver_caret'
                                    | 'semver_eq'
                                    | 'semver_gt'
                                    | 'semver_gte'
                                    | 'semver_lt'
                                    | 'semver_lte'
                                    | 'semver_neq'
                                    | 'semver_tilde'
                                    | 'semver_wildcard'
                                    | 'starts_with'
                                optionId: string
                                propertyName: string
                                value?: string | string[] | null | undefined
                            }
                        >
                      | null
                      | undefined
              }
              success: true
          }
    widgetConfig: ActivityEventsWidgetConfig
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editActivityEventsWidgetModalLogicActions {
    clearFieldError: (field: keyof ActivityEventsWidgetFieldErrors) => {
        field: 'dateRange' | 'filterTestAccounts' | 'limit'
    }
    setDateFrom: (dateFrom: WidgetDateFromValue) => {
        dateFrom: WidgetDateFromValue
    }
    setFieldErrors: (fieldErrors: ActivityEventsWidgetFieldErrors) => {
        fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
    }
    setFilterTestAccounts: (filterTestAccounts: boolean) => {
        filterTestAccounts: boolean
    }
    setLimit: (limit: number) => {
        limit: number
    }
    setTileDescription: (tileDescription: string) => {
        tileDescription: string
    }
    setTileName: (tileName: string) => {
        tileName: string
    }
    submit: () => {
        value: true
    }
    submitFailure: () => {
        value: true
    }
    submitSuccess: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editActivityEventsWidgetModalLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        validation: (
            limit: number,
            filterTestAccounts: boolean,
            widgetConfig: {
                dateRange?:
                    | {
                          date_from?:
                              | '-14d'
                              | '-1h'
                              | '-1M'
                              | '-24h'
                              | '-30d'
                              | '-30M'
                              | '-3h'
                              | '-7d'
                              | '-90d'
                              | null
                              | undefined
                      }
                    | null
                    | undefined
                eventName?: string | null | undefined
                filterTestAccounts?: boolean | null | undefined
                limit: number
                properties?:
                    | {
                          key: string
                          label?: string | null | undefined
                          operator:
                              | 'between'
                              | 'ends_with'
                              | 'exact'
                              | 'flag_evaluates_to'
                              | 'gt'
                              | 'gte'
                              | 'icontains'
                              | 'icontains_multi'
                              | 'in'
                              | 'is_cleaned_path_exact'
                              | 'is_cleaned_path_not_exact'
                              | 'is_date_after'
                              | 'is_date_before'
                              | 'is_date_exact'
                              | 'is_not'
                              | 'is_not_set'
                              | 'is_set'
                              | 'lt'
                              | 'lte'
                              | 'max'
                              | 'min'
                              | 'not_between'
                              | 'not_ends_with'
                              | 'not_icontains'
                              | 'not_icontains_multi'
                              | 'not_in'
                              | 'not_regex'
                              | 'not_starts_with'
                              | 'regex'
                              | 'semver_caret'
                              | 'semver_eq'
                              | 'semver_gt'
                              | 'semver_gte'
                              | 'semver_lt'
                              | 'semver_lte'
                              | 'semver_neq'
                              | 'semver_tilde'
                              | 'semver_wildcard'
                              | 'starts_with'
                          type: 'event' | 'person'
                          value?: boolean | number | string | (boolean | number | string)[] | null | undefined
                      }[]
                    | null
                    | undefined
                widgetFilters?:
                    | Record<
                          string,
                          {
                              filterId: string
                              operator:
                                  | 'between'
                                  | 'ends_with'
                                  | 'exact'
                                  | 'flag_evaluates_to'
                                  | 'gt'
                                  | 'gte'
                                  | 'icontains'
                                  | 'icontains_multi'
                                  | 'in'
                                  | 'is_cleaned_path_exact'
                                  | 'is_cleaned_path_not_exact'
                                  | 'is_date_after'
                                  | 'is_date_before'
                                  | 'is_date_exact'
                                  | 'is_not'
                                  | 'is_not_set'
                                  | 'is_set'
                                  | 'lt'
                                  | 'lte'
                                  | 'max'
                                  | 'min'
                                  | 'not_between'
                                  | 'not_ends_with'
                                  | 'not_icontains'
                                  | 'not_icontains_multi'
                                  | 'not_in'
                                  | 'not_regex'
                                  | 'not_starts_with'
                                  | 'regex'
                                  | 'semver_caret'
                                  | 'semver_eq'
                                  | 'semver_gt'
                                  | 'semver_gte'
                                  | 'semver_lt'
                                  | 'semver_lte'
                                  | 'semver_neq'
                                  | 'semver_tilde'
                                  | 'semver_wildcard'
                                  | 'starts_with'
                              optionId: string
                              propertyName: string
                              value?: string | string[] | null | undefined
                          }
                      >
                    | null
                    | undefined
            }
        ) =>
            | {
                  fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
                  success: false
              }
            | {
                  config: {
                      dateRange?:
                          | {
                                date_from?:
                                    | '-14d'
                                    | '-1h'
                                    | '-1M'
                                    | '-24h'
                                    | '-30d'
                                    | '-30M'
                                    | '-3h'
                                    | '-7d'
                                    | '-90d'
                                    | null
                                    | undefined
                            }
                          | null
                          | undefined
                      eventName?: string | null | undefined
                      filterTestAccounts?: boolean | null | undefined
                      limit: number
                      properties?:
                          | {
                                key: string
                                label?: string | null | undefined
                                operator:
                                    | 'between'
                                    | 'ends_with'
                                    | 'exact'
                                    | 'flag_evaluates_to'
                                    | 'gt'
                                    | 'gte'
                                    | 'icontains'
                                    | 'icontains_multi'
                                    | 'in'
                                    | 'is_cleaned_path_exact'
                                    | 'is_cleaned_path_not_exact'
                                    | 'is_date_after'
                                    | 'is_date_before'
                                    | 'is_date_exact'
                                    | 'is_not'
                                    | 'is_not_set'
                                    | 'is_set'
                                    | 'lt'
                                    | 'lte'
                                    | 'max'
                                    | 'min'
                                    | 'not_between'
                                    | 'not_ends_with'
                                    | 'not_icontains'
                                    | 'not_icontains_multi'
                                    | 'not_in'
                                    | 'not_regex'
                                    | 'not_starts_with'
                                    | 'regex'
                                    | 'semver_caret'
                                    | 'semver_eq'
                                    | 'semver_gt'
                                    | 'semver_gte'
                                    | 'semver_lt'
                                    | 'semver_lte'
                                    | 'semver_neq'
                                    | 'semver_tilde'
                                    | 'semver_wildcard'
                                    | 'starts_with'
                                type: 'event' | 'person'
                                value?: boolean | number | string | (boolean | number | string)[] | null | undefined
                            }[]
                          | null
                          | undefined
                      widgetFilters?:
                          | Record<
                                string,
                                {
                                    filterId: string
                                    operator:
                                        | 'between'
                                        | 'ends_with'
                                        | 'exact'
                                        | 'flag_evaluates_to'
                                        | 'gt'
                                        | 'gte'
                                        | 'icontains'
                                        | 'icontains_multi'
                                        | 'in'
                                        | 'is_cleaned_path_exact'
                                        | 'is_cleaned_path_not_exact'
                                        | 'is_date_after'
                                        | 'is_date_before'
                                        | 'is_date_exact'
                                        | 'is_not'
                                        | 'is_not_set'
                                        | 'is_set'
                                        | 'lt'
                                        | 'lte'
                                        | 'max'
                                        | 'min'
                                        | 'not_between'
                                        | 'not_ends_with'
                                        | 'not_icontains'
                                        | 'not_icontains_multi'
                                        | 'not_in'
                                        | 'not_regex'
                                        | 'not_starts_with'
                                        | 'regex'
                                        | 'semver_caret'
                                        | 'semver_eq'
                                        | 'semver_gt'
                                        | 'semver_gte'
                                        | 'semver_lt'
                                        | 'semver_lte'
                                        | 'semver_neq'
                                        | 'semver_tilde'
                                        | 'semver_wildcard'
                                        | 'starts_with'
                                    optionId: string
                                    propertyName: string
                                    value?: string | string[] | null | undefined
                                }
                            >
                          | null
                          | undefined
                  }
                  success: true
              }
        activeFieldErrors: (
            validation:
                | {
                      config: {
                          dateRange?:
                              | {
                                    date_from?:
                                        | '-14d'
                                        | '-1h'
                                        | '-1M'
                                        | '-24h'
                                        | '-30d'
                                        | '-30M'
                                        | '-3h'
                                        | '-7d'
                                        | '-90d'
                                        | null
                                        | undefined
                                }
                              | null
                              | undefined
                          eventName?: string | null | undefined
                          filterTestAccounts?: boolean | null | undefined
                          limit: number
                          properties?:
                              | {
                                    key: string
                                    label?: string | null | undefined
                                    operator:
                                        | 'between'
                                        | 'ends_with'
                                        | 'exact'
                                        | 'flag_evaluates_to'
                                        | 'gt'
                                        | 'gte'
                                        | 'icontains'
                                        | 'icontains_multi'
                                        | 'in'
                                        | 'is_cleaned_path_exact'
                                        | 'is_cleaned_path_not_exact'
                                        | 'is_date_after'
                                        | 'is_date_before'
                                        | 'is_date_exact'
                                        | 'is_not'
                                        | 'is_not_set'
                                        | 'is_set'
                                        | 'lt'
                                        | 'lte'
                                        | 'max'
                                        | 'min'
                                        | 'not_between'
                                        | 'not_ends_with'
                                        | 'not_icontains'
                                        | 'not_icontains_multi'
                                        | 'not_in'
                                        | 'not_regex'
                                        | 'not_starts_with'
                                        | 'regex'
                                        | 'semver_caret'
                                        | 'semver_eq'
                                        | 'semver_gt'
                                        | 'semver_gte'
                                        | 'semver_lt'
                                        | 'semver_lte'
                                        | 'semver_neq'
                                        | 'semver_tilde'
                                        | 'semver_wildcard'
                                        | 'starts_with'
                                    type: 'event' | 'person'
                                    value?: boolean | number | string | (boolean | number | string)[] | null | undefined
                                }[]
                              | null
                              | undefined
                          widgetFilters?:
                              | Record<
                                    string,
                                    {
                                        filterId: string
                                        operator:
                                            | 'between'
                                            | 'ends_with'
                                            | 'exact'
                                            | 'flag_evaluates_to'
                                            | 'gt'
                                            | 'gte'
                                            | 'icontains'
                                            | 'icontains_multi'
                                            | 'in'
                                            | 'is_cleaned_path_exact'
                                            | 'is_cleaned_path_not_exact'
                                            | 'is_date_after'
                                            | 'is_date_before'
                                            | 'is_date_exact'
                                            | 'is_not'
                                            | 'is_not_set'
                                            | 'is_set'
                                            | 'lt'
                                            | 'lte'
                                            | 'max'
                                            | 'min'
                                            | 'not_between'
                                            | 'not_ends_with'
                                            | 'not_icontains'
                                            | 'not_icontains_multi'
                                            | 'not_in'
                                            | 'not_regex'
                                            | 'not_starts_with'
                                            | 'regex'
                                            | 'semver_caret'
                                            | 'semver_eq'
                                            | 'semver_gt'
                                            | 'semver_gte'
                                            | 'semver_lt'
                                            | 'semver_lte'
                                            | 'semver_neq'
                                            | 'semver_tilde'
                                            | 'semver_wildcard'
                                            | 'starts_with'
                                        optionId: string
                                        propertyName: string
                                        value?: string | string[] | null | undefined
                                    }
                                >
                              | null
                              | undefined
                      }
                      success: true
                  }
                | {
                      fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
                      success: false
                  },
            fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
        ) => ActivityEventsWidgetFieldErrors
        saveDisabledReason: (
            saving: boolean,
            validation:
                | {
                      config: {
                          dateRange?:
                              | {
                                    date_from?:
                                        | '-14d'
                                        | '-1h'
                                        | '-1M'
                                        | '-24h'
                                        | '-30d'
                                        | '-30M'
                                        | '-3h'
                                        | '-7d'
                                        | '-90d'
                                        | null
                                        | undefined
                                }
                              | null
                              | undefined
                          eventName?: string | null | undefined
                          filterTestAccounts?: boolean | null | undefined
                          limit: number
                          properties?:
                              | {
                                    key: string
                                    label?: string | null | undefined
                                    operator:
                                        | 'between'
                                        | 'ends_with'
                                        | 'exact'
                                        | 'flag_evaluates_to'
                                        | 'gt'
                                        | 'gte'
                                        | 'icontains'
                                        | 'icontains_multi'
                                        | 'in'
                                        | 'is_cleaned_path_exact'
                                        | 'is_cleaned_path_not_exact'
                                        | 'is_date_after'
                                        | 'is_date_before'
                                        | 'is_date_exact'
                                        | 'is_not'
                                        | 'is_not_set'
                                        | 'is_set'
                                        | 'lt'
                                        | 'lte'
                                        | 'max'
                                        | 'min'
                                        | 'not_between'
                                        | 'not_ends_with'
                                        | 'not_icontains'
                                        | 'not_icontains_multi'
                                        | 'not_in'
                                        | 'not_regex'
                                        | 'not_starts_with'
                                        | 'regex'
                                        | 'semver_caret'
                                        | 'semver_eq'
                                        | 'semver_gt'
                                        | 'semver_gte'
                                        | 'semver_lt'
                                        | 'semver_lte'
                                        | 'semver_neq'
                                        | 'semver_tilde'
                                        | 'semver_wildcard'
                                        | 'starts_with'
                                    type: 'event' | 'person'
                                    value?: boolean | number | string | (boolean | number | string)[] | null | undefined
                                }[]
                              | null
                              | undefined
                          widgetFilters?:
                              | Record<
                                    string,
                                    {
                                        filterId: string
                                        operator:
                                            | 'between'
                                            | 'ends_with'
                                            | 'exact'
                                            | 'flag_evaluates_to'
                                            | 'gt'
                                            | 'gte'
                                            | 'icontains'
                                            | 'icontains_multi'
                                            | 'in'
                                            | 'is_cleaned_path_exact'
                                            | 'is_cleaned_path_not_exact'
                                            | 'is_date_after'
                                            | 'is_date_before'
                                            | 'is_date_exact'
                                            | 'is_not'
                                            | 'is_not_set'
                                            | 'is_set'
                                            | 'lt'
                                            | 'lte'
                                            | 'max'
                                            | 'min'
                                            | 'not_between'
                                            | 'not_ends_with'
                                            | 'not_icontains'
                                            | 'not_icontains_multi'
                                            | 'not_in'
                                            | 'not_regex'
                                            | 'not_starts_with'
                                            | 'regex'
                                            | 'semver_caret'
                                            | 'semver_eq'
                                            | 'semver_gt'
                                            | 'semver_gte'
                                            | 'semver_lt'
                                            | 'semver_lte'
                                            | 'semver_neq'
                                            | 'semver_tilde'
                                            | 'semver_wildcard'
                                            | 'starts_with'
                                        optionId: string
                                        propertyName: string
                                        value?: string | string[] | null | undefined
                                    }
                                >
                              | null
                              | undefined
                      }
                      success: true
                  }
                | {
                      fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
                      success: false
                  }
        ) => string | undefined
        widgetConfig: (config: Record<string, unknown>) => ActivityEventsWidgetConfig
    }
}

export type editActivityEventsWidgetModalLogicType = MakeLogicType<
    editActivityEventsWidgetModalLogicValues,
    editActivityEventsWidgetModalLogicActions,
    EditActivityEventsWidgetModalLogicProps,
    editActivityEventsWidgetModalLogicMeta
>

export const editActivityEventsWidgetModalLogic = kea<editActivityEventsWidgetModalLogicType>([
    path(['products', 'dashboards', 'widgets', 'activity', 'editActivityEventsWidgetModalLogic']),

    props({
        config: {},
        onSave: async () => {},
        onClose: () => {},
        name: '',
        defaultTitle: 'Untitled',
        description: '',
    } as EditActivityEventsWidgetModalLogicProps),

    connect(() => ({
        values: [filterTestAccountsDefaultsLogic, ['filterTestAccountsDefault']],
    })),

    actions({
        ...widgetEditModalListFieldActions,
        ...widgetEditModalTileActions,
        ...widgetEditModalFilterTestAccountsActions,
        setFieldErrors: (fieldErrors: ActivityEventsWidgetFieldErrors) => ({ fieldErrors }),
        clearFieldError: (field: keyof ActivityEventsWidgetFieldErrors) => ({ field }),
        submit: true,
        submitSuccess: true,
        submitFailure: true,
    }),

    reducers({
        limit: [
            25,
            {
                setLimit: (_: number, { limit }: { limit: number }) => limit,
            },
        ],
        tileName: [
            '',
            {
                setTileName: (_: string, { tileName }: { tileName: string }) => tileName,
            },
        ],
        tileDescription: [
            '',
            {
                setTileDescription: (_: string, { tileDescription }: { tileDescription: string }) => tileDescription,
            },
        ],
        filterTestAccounts: [
            false,
            {
                setFilterTestAccounts: (_: boolean, { filterTestAccounts }: { filterTestAccounts: boolean }) =>
                    filterTestAccounts,
            },
        ],
        fieldErrors: [
            {} as ActivityEventsWidgetFieldErrors,
            {
                setFieldErrors: (
                    _: ActivityEventsWidgetFieldErrors,
                    { fieldErrors }: { fieldErrors: ActivityEventsWidgetFieldErrors }
                ) => fieldErrors,
                clearFieldError: (
                    state: ActivityEventsWidgetFieldErrors,
                    { field }: { field: keyof ActivityEventsWidgetFieldErrors }
                ) => {
                    if (!state[field]) {
                        return state
                    }
                    const next = { ...state }
                    delete next[field]
                    return next
                },
            },
        ],
        saving: [
            false,
            {
                submit: (_state: boolean, _payload: { value: true }) => true,
                submitSuccess: (_state: boolean, _payload: { value: true }) => false,
                submitFailure: (_state: boolean, _payload: { value: true }) => false,
            },
        ],
    }),

    selectors({
        widgetConfig: [
            (_, p) => [p.config],
            (config: Record<string, unknown>): ActivityEventsWidgetConfig => parseActivityEventsWidgetConfig(config),
        ],
        ...widgetEditModalPropSelectors,
        validation: [
            (s) => [s.limit, s.filterTestAccounts, s.widgetConfig],
            (limit: number, filterTestAccounts: boolean, widgetConfig: ActivityEventsWidgetConfig) =>
                validateActivityEventsWidgetConfigInput({
                    limit,
                    filterTestAccounts,
                    baseConfig: widgetConfig,
                }),
        ],
        activeFieldErrors: [
            (s) => [s.validation, s.fieldErrors],
            (
                validation:
                    | {
                          config: {
                              dateRange?:
                                  | {
                                        date_from?:
                                            | '-14d'
                                            | '-1h'
                                            | '-1M'
                                            | '-24h'
                                            | '-30d'
                                            | '-30M'
                                            | '-3h'
                                            | '-7d'
                                            | '-90d'
                                            | null
                                            | undefined
                                    }
                                  | null
                                  | undefined
                              eventName?: string | null | undefined
                              filterTestAccounts?: boolean | null | undefined
                              limit: number
                              properties?:
                                  | {
                                        key: string
                                        label?: string | null | undefined
                                        operator:
                                            | 'between'
                                            | 'ends_with'
                                            | 'exact'
                                            | 'flag_evaluates_to'
                                            | 'gt'
                                            | 'gte'
                                            | 'icontains'
                                            | 'icontains_multi'
                                            | 'in'
                                            | 'is_cleaned_path_exact'
                                            | 'is_cleaned_path_not_exact'
                                            | 'is_date_after'
                                            | 'is_date_before'
                                            | 'is_date_exact'
                                            | 'is_not'
                                            | 'is_not_set'
                                            | 'is_set'
                                            | 'lt'
                                            | 'lte'
                                            | 'max'
                                            | 'min'
                                            | 'not_between'
                                            | 'not_ends_with'
                                            | 'not_icontains'
                                            | 'not_icontains_multi'
                                            | 'not_in'
                                            | 'not_regex'
                                            | 'not_starts_with'
                                            | 'regex'
                                            | 'semver_caret'
                                            | 'semver_eq'
                                            | 'semver_gt'
                                            | 'semver_gte'
                                            | 'semver_lt'
                                            | 'semver_lte'
                                            | 'semver_neq'
                                            | 'semver_tilde'
                                            | 'semver_wildcard'
                                            | 'starts_with'
                                        type: 'event' | 'person'
                                        value?:
                                            | boolean
                                            | number
                                            | string
                                            | (boolean | number | string)[]
                                            | null
                                            | undefined
                                    }[]
                                  | null
                                  | undefined
                              widgetFilters?:
                                  | Record<
                                        string,
                                        {
                                            filterId: string
                                            operator:
                                                | 'between'
                                                | 'ends_with'
                                                | 'exact'
                                                | 'flag_evaluates_to'
                                                | 'gt'
                                                | 'gte'
                                                | 'icontains'
                                                | 'icontains_multi'
                                                | 'in'
                                                | 'is_cleaned_path_exact'
                                                | 'is_cleaned_path_not_exact'
                                                | 'is_date_after'
                                                | 'is_date_before'
                                                | 'is_date_exact'
                                                | 'is_not'
                                                | 'is_not_set'
                                                | 'is_set'
                                                | 'lt'
                                                | 'lte'
                                                | 'max'
                                                | 'min'
                                                | 'not_between'
                                                | 'not_ends_with'
                                                | 'not_icontains'
                                                | 'not_icontains_multi'
                                                | 'not_in'
                                                | 'not_regex'
                                                | 'not_starts_with'
                                                | 'regex'
                                                | 'semver_caret'
                                                | 'semver_eq'
                                                | 'semver_gt'
                                                | 'semver_gte'
                                                | 'semver_lt'
                                                | 'semver_lte'
                                                | 'semver_neq'
                                                | 'semver_tilde'
                                                | 'semver_wildcard'
                                                | 'starts_with'
                                            optionId: string
                                            propertyName: string
                                            value?: string | string[] | null | undefined
                                        }
                                    >
                                  | null
                                  | undefined
                          }
                          success: true
                      }
                    | {
                          fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
                          success: false
                      },
                fieldErrors: ActivityEventsWidgetFieldErrors
            ): ActivityEventsWidgetFieldErrors => {
                if (!validation.success) {
                    return { ...validation.fieldErrors, ...fieldErrors }
                }
                return fieldErrors
            },
        ],
        saveDisabledReason: [
            (s) => [s.saving, s.validation],
            (
                saving: boolean,
                validation:
                    | {
                          config: {
                              dateRange?:
                                  | {
                                        date_from?:
                                            | '-14d'
                                            | '-1h'
                                            | '-1M'
                                            | '-24h'
                                            | '-30d'
                                            | '-30M'
                                            | '-3h'
                                            | '-7d'
                                            | '-90d'
                                            | null
                                            | undefined
                                    }
                                  | null
                                  | undefined
                              eventName?: string | null | undefined
                              filterTestAccounts?: boolean | null | undefined
                              limit: number
                              properties?:
                                  | {
                                        key: string
                                        label?: string | null | undefined
                                        operator:
                                            | 'between'
                                            | 'ends_with'
                                            | 'exact'
                                            | 'flag_evaluates_to'
                                            | 'gt'
                                            | 'gte'
                                            | 'icontains'
                                            | 'icontains_multi'
                                            | 'in'
                                            | 'is_cleaned_path_exact'
                                            | 'is_cleaned_path_not_exact'
                                            | 'is_date_after'
                                            | 'is_date_before'
                                            | 'is_date_exact'
                                            | 'is_not'
                                            | 'is_not_set'
                                            | 'is_set'
                                            | 'lt'
                                            | 'lte'
                                            | 'max'
                                            | 'min'
                                            | 'not_between'
                                            | 'not_ends_with'
                                            | 'not_icontains'
                                            | 'not_icontains_multi'
                                            | 'not_in'
                                            | 'not_regex'
                                            | 'not_starts_with'
                                            | 'regex'
                                            | 'semver_caret'
                                            | 'semver_eq'
                                            | 'semver_gt'
                                            | 'semver_gte'
                                            | 'semver_lt'
                                            | 'semver_lte'
                                            | 'semver_neq'
                                            | 'semver_tilde'
                                            | 'semver_wildcard'
                                            | 'starts_with'
                                        type: 'event' | 'person'
                                        value?:
                                            | boolean
                                            | number
                                            | string
                                            | (boolean | number | string)[]
                                            | null
                                            | undefined
                                    }[]
                                  | null
                                  | undefined
                              widgetFilters?:
                                  | Record<
                                        string,
                                        {
                                            filterId: string
                                            operator:
                                                | 'between'
                                                | 'ends_with'
                                                | 'exact'
                                                | 'flag_evaluates_to'
                                                | 'gt'
                                                | 'gte'
                                                | 'icontains'
                                                | 'icontains_multi'
                                                | 'in'
                                                | 'is_cleaned_path_exact'
                                                | 'is_cleaned_path_not_exact'
                                                | 'is_date_after'
                                                | 'is_date_before'
                                                | 'is_date_exact'
                                                | 'is_not'
                                                | 'is_not_set'
                                                | 'is_set'
                                                | 'lt'
                                                | 'lte'
                                                | 'max'
                                                | 'min'
                                                | 'not_between'
                                                | 'not_ends_with'
                                                | 'not_icontains'
                                                | 'not_icontains_multi'
                                                | 'not_in'
                                                | 'not_regex'
                                                | 'not_starts_with'
                                                | 'regex'
                                                | 'semver_caret'
                                                | 'semver_eq'
                                                | 'semver_gt'
                                                | 'semver_gte'
                                                | 'semver_lt'
                                                | 'semver_lte'
                                                | 'semver_neq'
                                                | 'semver_tilde'
                                                | 'semver_wildcard'
                                                | 'starts_with'
                                            optionId: string
                                            propertyName: string
                                            value?: string | string[] | null | undefined
                                        }
                                    >
                                  | null
                                  | undefined
                          }
                          success: true
                      }
                    | {
                          fieldErrors: Partial<Record<'dateRange' | 'filterTestAccounts' | 'limit', string>>
                          success: false
                      }
            ): string | undefined => {
                if (saving) {
                    return 'Saving…'
                }
                if (!validation.success) {
                    return 'Fix validation errors to save'
                }
                return undefined
            },
        ],
    }),

    defaults(({ props, values }) => {
        const baseConfig = parseActivityEventsWidgetConfig(props.config)

        return {
            limit: baseConfig.limit,
            ...getWidgetEditModalTileDefaults(props),
            filterTestAccounts: resolveWidgetFilterTestAccounts(
                baseConfig.filterTestAccounts,
                values.filterTestAccountsDefault
            ),
            fieldErrors: {},
            saving: false,
        }
    }),

    listeners(({ actions, props, values }) => ({
        submit: async () => {
            const result = values.validation

            if (!result.success) {
                actions.setFieldErrors(result.fieldErrors)
                return
            }

            try {
                await props.onSave(
                    result.config,
                    buildWidgetTileMetadataPatch(props, values.tileName, values.tileDescription)
                )
                actions.setFieldErrors({})
                props.onClose()
                actions.submitSuccess()
            } catch (error) {
                actions.submitFailure()
                if (isWidgetConfigValidationError(error)) {
                    actions.setFieldErrors(error.fieldErrors as ActivityEventsWidgetFieldErrors)
                    return
                }
                throw error
            }
        },
    })),
])