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.

6.2 KB
Raw Blame
import { MakeLogicType, connect, kea, path, props } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'

import { IntegrationType } from '~/types'

export interface S3CompatibleSetupModalLogicProps {
    isOpen: boolean
    integration?: IntegrationType | null
    onComplete: (integrationId?: number) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface s3CompatibleSetupModalLogicValues {
    isS3CompatibleIntegrationSubmitting: boolean
    isS3CompatibleIntegrationValid: boolean
    s3CompatibleIntegration: {
        awsAccessKeyId: string
        awsSecretAccessKey: string
        endpointUrl: string
        name: string
    }
    s3CompatibleIntegrationAllErrors: Record<string, any>
    s3CompatibleIntegrationChanged: boolean
    s3CompatibleIntegrationErrors: DeepPartialMap<
        {
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        },
        ValidationErrorType
    >
    s3CompatibleIntegrationHasErrors: boolean
    s3CompatibleIntegrationManualErrors: Record<string, any>
    s3CompatibleIntegrationTouched: boolean
    s3CompatibleIntegrationTouches: Record<string, boolean>
    s3CompatibleIntegrationValidationErrors: DeepPartialMap<
        {
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        },
        ValidationErrorType
    >
    showS3CompatibleIntegrationErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface s3CompatibleSetupModalLogicActions {
    loadIntegrations: (_?: any) => any // integrationsLogic
    resetS3CompatibleIntegration: (values?: {
        awsAccessKeyId: string
        awsSecretAccessKey: string
        endpointUrl: string
        name: string
    }) => {
        values?: {
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        }
    }
    setS3CompatibleIntegrationManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setS3CompatibleIntegrationValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setS3CompatibleIntegrationValues: (
        values: DeepPartial<{
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        }>
    ) => {
        values: DeepPartial<{
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        }>
    }
    submitS3CompatibleIntegration: () => {
        value: boolean
    }
    submitS3CompatibleIntegrationFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitS3CompatibleIntegrationRequest: (s3CompatibleIntegration: {
        awsAccessKeyId: string
        awsSecretAccessKey: string
        endpointUrl: string
        name: string
    }) => {
        s3CompatibleIntegration: {
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        }
    }
    submitS3CompatibleIntegrationSuccess: (s3CompatibleIntegration: {
        awsAccessKeyId: string
        awsSecretAccessKey: string
        endpointUrl: string
        name: string
    }) => {
        s3CompatibleIntegration: {
            awsAccessKeyId: string
            awsSecretAccessKey: string
            endpointUrl: string
            name: string
        }
    }
    touchS3CompatibleIntegrationField: (key: string) => {
        key: string
    }
}

export type s3CompatibleSetupModalLogicType = MakeLogicType<
    s3CompatibleSetupModalLogicValues,
    s3CompatibleSetupModalLogicActions,
    S3CompatibleSetupModalLogicProps
>

export const s3CompatibleSetupModalLogic = kea<s3CompatibleSetupModalLogicType>([
    path(['integrations', 's3-compatible', 's3CompatibleSetupModalLogic']),
    props({} as S3CompatibleSetupModalLogicProps),
    connect(() => ({
        actions: [integrationsLogic, ['loadIntegrations']],
    })),
    forms(({ props, actions, values }) => ({
        s3CompatibleIntegration: {
            defaults: {
                name: '',
                endpointUrl: '',
                awsAccessKeyId: '',
                awsSecretAccessKey: '',
            },
            errors: ({ name, endpointUrl, awsAccessKeyId, awsSecretAccessKey }) => ({
                name: name.trim() ? undefined : 'Name is required',
                endpointUrl: endpointUrl.trim() ? undefined : 'Endpoint URL is required',
                awsAccessKeyId: awsAccessKeyId.trim() ? undefined : 'Access Key ID is required',
                awsSecretAccessKey: awsSecretAccessKey.trim() ? undefined : 'Secret Access Key is required',
            }),
            submit: async () => {
                try {
                    const integration = await api.integrations.create({
                        kind: 's3-compatible',
                        config: {
                            name: values.s3CompatibleIntegration.name,
                            endpoint_url: values.s3CompatibleIntegration.endpointUrl,
                            aws_access_key_id: values.s3CompatibleIntegration.awsAccessKeyId,
                            aws_secret_access_key: values.s3CompatibleIntegration.awsSecretAccessKey,
                        },
                    })
                    actions.loadIntegrations()
                    lemonToast.success('S3-compatible storage connection created successfully!')
                    props.onComplete(integration.id)
                } catch (error: any) {
                    lemonToast.error(error.detail || 'Failed to create S3-compatible storage connection')
                    throw error
                }
            },
        },
    })),
])