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.

5.9 KB
Raw Blame
import { MakeLogicType, actions, kea, key, path, props, reducers } from 'kea'

import { BreakdownFilter, TileFilters } from '~/queries/schema/schema-general'
import { AnyPropertyFilter, IntervalType } from '~/types'

export interface TileLogicProps {
    dashboardId: number
    tileId: number
    filtersOverrides?: TileFilters
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tileLogicValues {
    overrides: TileFilters
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tileLogicActions {
    resetOverrides: () => {
        value: true
    }
    setBreakdown: (breakdown_filter: BreakdownFilter | null | undefined) => {
        breakdown_filter: BreakdownFilter | null | undefined
    }
    setDates: (
        date_from: string | null | undefined,
        date_to: string | null | undefined,
        explicitDate?: boolean
    ) => {
        date_from: string | null | undefined
        date_to: string | null | undefined
        explicitDate: boolean | undefined
    }
    setFilterTestAccounts: (filterTestAccounts: boolean | null) => {
        filterTestAccounts: boolean | null
    }
    setIgnoreDashboardFilters: (ignoreDashboardFilters: boolean) => {
        ignoreDashboardFilters: boolean
    }
    setInterval: (interval: IntervalType | null | undefined) => {
        interval: IntervalType | null | undefined
    }
    setProperties: (properties: AnyPropertyFilter[] | null | undefined) => {
        properties: AnyPropertyFilter[] | null | undefined
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tileLogicMeta {
    key: string
}

export type tileLogicType = MakeLogicType<tileLogicValues, tileLogicActions, TileLogicProps, tileLogicMeta>

export const tileLogic = kea<tileLogicType>([
    path(['scenes', 'dashboard', 'tileLogic']),
    props({} as TileLogicProps),
    key((p: TileLogicProps) => `${p.dashboardId}:${p.tileId}`),

    actions(() => ({
        setDates: (
            date_from: string | null | undefined,
            date_to: string | null | undefined,
            explicitDate?: boolean
        ) => ({
            date_from,
            date_to,
            explicitDate,
        }),
        setProperties: (properties: AnyPropertyFilter[] | null | undefined) => ({ properties }),
        setBreakdown: (breakdown_filter: BreakdownFilter | null | undefined) => ({ breakdown_filter }),
        setInterval: (interval: IntervalType | null | undefined) => ({ interval }),
        setFilterTestAccounts: (filterTestAccounts: boolean | null) => ({ filterTestAccounts }),
        setIgnoreDashboardFilters: (ignoreDashboardFilters: boolean) => ({ ignoreDashboardFilters }),
        resetOverrides: true,
    })),

    reducers(({ props }) => ({
        overrides: [
            (props.filtersOverrides ?? {}) as TileFilters,
            {
                setDates: (state, { date_from, date_to, explicitDate }) => {
                    const newState = { ...state }
                    if (date_from !== undefined && date_from !== null) {
                        newState.date_from = date_from
                    } else {
                        delete newState.date_from
                    }
                    if (date_to !== undefined && date_to !== null) {
                        newState.date_to = date_to
                    } else {
                        delete newState.date_to
                    }
                    if (explicitDate !== undefined) {
                        newState.explicitDate = explicitDate
                    } else {
                        delete newState.explicitDate
                    }
                    return newState
                },
                setProperties: (state, { properties }) => {
                    const newState = { ...state }
                    if (properties && properties.length > 0) {
                        newState.properties = properties
                    } else {
                        delete newState.properties
                    }
                    return newState
                },
                setBreakdown: (state, { breakdown_filter }) => {
                    const newState = { ...state }
                    if (breakdown_filter) {
                        newState.breakdown_filter = breakdown_filter
                    } else {
                        delete newState.breakdown_filter
                    }
                    return newState
                },
                setInterval: (state, { interval }) => {
                    const newState = { ...state }
                    if (interval) {
                        newState.interval = interval
                    } else {
                        delete newState.interval
                    }
                    return newState
                },
                setFilterTestAccounts: (state, { filterTestAccounts }) => {
                    const newState = { ...state }
                    // Tri-state: false means "force test account filtering off", so unlike the other
                    // overrides only null (inherit) clears the key.
                    if (filterTestAccounts === null) {
                        delete newState.filterTestAccounts
                    } else {
                        newState.filterTestAccounts = filterTestAccounts
                    }
                    return newState
                },
                setIgnoreDashboardFilters: (state, { ignoreDashboardFilters }) => {
                    const newState = { ...state }
                    if (ignoreDashboardFilters) {
                        newState.ignoreDashboardFilters = true
                    } else {
                        delete newState.ignoreDashboardFilters
                    }
                    return newState
                },
                resetOverrides: () => ({}),
            },
        ],
    })),
])