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.

4.6 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, path, props, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { dayjs } from 'lib/dayjs'
import { preflightLogic } from 'lib/logic/preflightLogic'

import { TwilioPhoneNumberType } from '~/types'

import type { PreflightStatus } from '../../types'

export const TWILIO_CHANNELS_MIN_REFRESH_INTERVAL_MINUTES = 5

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twilioIntegrationLogicValues {
    preflight: PreflightStatus | null // preflightLogic
    siteUrlMisconfigured: boolean // preflightLogic
    allTwilioPhoneNumbers: {
        lastRefreshedAt: string
        phone_numbers: TwilioPhoneNumberType[]
    } | null
    allTwilioPhoneNumbersLoading: boolean
    getPhoneNumberRefreshButtonDisabledReason: () => string
    twilioPhoneNumbers: TwilioPhoneNumberType[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twilioIntegrationLogicActions {
    loadAllTwilioPhoneNumbers: (forceRefresh?: boolean) => {
        forceRefresh: boolean
    }
    loadAllTwilioPhoneNumbersFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAllTwilioPhoneNumbersSuccess: (
        allTwilioPhoneNumbers: {
            lastRefreshedAt: string
            phone_numbers: TwilioPhoneNumberType[]
        },
        payload?: {
            forceRefresh: boolean
        }
    ) => {
        allTwilioPhoneNumbers: {
            lastRefreshedAt: string
            phone_numbers: TwilioPhoneNumberType[]
        }
        payload?: {
            forceRefresh: boolean
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twilioIntegrationLogicProps {
    id: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twilioIntegrationLogicMeta {
    key: number
    __keaTypeGenInternalSelectorTypes: {
        twilioPhoneNumbers: (
            allTwilioPhoneNumbers: {
                lastRefreshedAt: string
                phone_numbers: TwilioPhoneNumberType[]
            } | null
        ) => TwilioPhoneNumberType[]
        getPhoneNumberRefreshButtonDisabledReason: (
            allTwilioPhoneNumbers: {
                lastRefreshedAt: string
                phone_numbers: TwilioPhoneNumberType[]
            } | null
        ) => () => string
    }
}

export type twilioIntegrationLogicType = MakeLogicType<
    twilioIntegrationLogicValues,
    twilioIntegrationLogicActions,
    twilioIntegrationLogicProps,
    twilioIntegrationLogicMeta
>

export const twilioIntegrationLogic = kea<twilioIntegrationLogicType>([
    props({} as { id: number }),
    key((props) => props.id),
    path((key) => ['lib', 'integrations', 'twilioIntegrationLogic', key]),
    connect(() => ({
        values: [preflightLogic, ['siteUrlMisconfigured', 'preflight']],
    })),
    actions({
        loadAllTwilioPhoneNumbers: (forceRefresh: boolean = false) => ({ forceRefresh }),
    }),

    loaders(({ props }) => ({
        allTwilioPhoneNumbers: [
            null as { phone_numbers: TwilioPhoneNumberType[]; lastRefreshedAt: string } | null,
            {
                loadAllTwilioPhoneNumbers: async ({ forceRefresh }) => {
                    return await api.integrations.twilioPhoneNumbers(props.id, forceRefresh)
                },
            },
        ],
    })),

    selectors({
        twilioPhoneNumbers: [
            (s) => [s.allTwilioPhoneNumbers],
            (allTwilioPhoneNumbers: { phone_numbers: TwilioPhoneNumberType[]; lastRefreshedAt: string } | null) => {
                return allTwilioPhoneNumbers?.phone_numbers ?? []
            },
        ],
        getPhoneNumberRefreshButtonDisabledReason: [
            (s) => [s.allTwilioPhoneNumbers],
            (allTwilioPhoneNumbers: { phone_numbers: TwilioPhoneNumberType[]; lastRefreshedAt: string } | null) =>
                (): string => {
                    const now = dayjs()
                    if (allTwilioPhoneNumbers) {
                        const earliestRefresh = dayjs(allTwilioPhoneNumbers.lastRefreshedAt).add(
                            TWILIO_CHANNELS_MIN_REFRESH_INTERVAL_MINUTES,
                            'minutes'
                        )
                        if (now.isBefore(earliestRefresh)) {
                            return `You can refresh the phone numbers again ${earliestRefresh.from(now)}`
                        }
                    }
                    return ''
                },
        ],
    }),
])