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.

8.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import { organizationLogic } from 'scenes/organizationLogic'

import type { OrganizationMemberNoticeApi } from '~/generated/core/api.schemas'
import { OrganizationType } from '~/types'

export interface MemberNoticeForm {
    message: string
    label: string
    url: string
}

function formFromNotice(notice: OrganizationMemberNoticeApi | null | undefined): MemberNoticeForm {
    return {
        message: notice?.message ?? '',
        label: notice?.action?.label ?? '',
        url: notice?.action?.url ?? '',
    }
}

function noticeFromForm({ message, label, url }: MemberNoticeForm): OrganizationMemberNoticeApi | null {
    if (!message.trim()) {
        return null
    }
    return {
        message: message.trim(),
        action: label.trim() && url.trim() ? { label: label.trim(), url: url.trim() } : null,
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationMemberNoticeLogicValues {
    currentOrganization: OrganizationType | null // organizationLogic
    currentOrganizationLoading: boolean // organizationLogic
    draftMemberNotice: OrganizationMemberNoticeApi | null
    hasUnsavedMemberNotice: boolean
    isMemberNoticeSubmitting: boolean
    isMemberNoticeValid: boolean
    memberNotice: MemberNoticeForm
    memberNoticeAllErrors: Record<string, any>
    memberNoticeChanged: boolean
    memberNoticeErrors: DeepPartialMap<MemberNoticeForm, ValidationErrorType>
    memberNoticeHasErrors: boolean
    memberNoticeManualErrors: Record<string, any>
    memberNoticeTouched: boolean
    memberNoticeTouches: Record<string, boolean>
    memberNoticeValidationErrors: DeepPartialMap<MemberNoticeForm, ValidationErrorType>
    savedMemberNotice: OrganizationMemberNoticeApi | null
    showMemberNoticeErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationMemberNoticeLogicActions {
    loadCurrentOrganizationSuccess: (
        currentOrganization: OrganizationType | null,
        payload?: any
    ) => {
        currentOrganization: OrganizationType | null
        payload?: any
    } // organizationLogic
    updateOrganizationSuccess: (
        currentOrganization: OrganizationType | null,
        payload?:
            | Partial<
                  Pick<
                      OrganizationType,
                      | 'allow_publicly_shared_resources'
                      | 'default_anonymize_ips'
                      | 'default_role_id'
                      | 'enforce_2fa'
                      | 'enforce_verified_domains'
                      | 'is_ai_data_processing_approved'
                      | 'is_ai_training_opted_in'
                      | 'logo_media_id'
                      | 'member_notice'
                      | 'members_can_create_projects'
                      | 'members_can_invite'
                      | 'members_can_see_org_members'
                      | 'members_can_use_personal_api_keys'
                      | 'name'
                      | 'read_only_mcp_access'
                  >
              >
            | undefined
    ) => {
        currentOrganization: OrganizationType | null
        payload?: import('scenes/organizationLogic').OrganizationUpdatePayload
    } // organizationLogic
    removeMemberNotice: () => {
        value: true
    }
    resetMemberNotice: (values?: MemberNoticeForm) => {
        values?: MemberNoticeForm
    }
    setMemberNoticeManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setMemberNoticeValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setMemberNoticeValues: (values: DeepPartial<MemberNoticeForm>) => {
        values: DeepPartial<MemberNoticeForm>
    }
    submitMemberNotice: () => {
        value: boolean
    }
    submitMemberNoticeFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitMemberNoticeRequest: (memberNotice: MemberNoticeForm) => {
        memberNotice: MemberNoticeForm
    }
    submitMemberNoticeSuccess: (memberNotice: MemberNoticeForm) => {
        memberNotice: MemberNoticeForm
    }
    touchMemberNoticeField: (key: string) => {
        key: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationMemberNoticeLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        savedMemberNotice: (currentOrganization: OrganizationType | null) => OrganizationMemberNoticeApi | null
        draftMemberNotice: (memberNotice: MemberNoticeForm) => OrganizationMemberNoticeApi | null
        hasUnsavedMemberNotice: (
            draftMemberNotice: OrganizationMemberNoticeApi | null,
            savedMemberNotice: OrganizationMemberNoticeApi | null
        ) => boolean
    }
}

export type organizationMemberNoticeLogicType = MakeLogicType<
    organizationMemberNoticeLogicValues,
    organizationMemberNoticeLogicActions,
    Record<string, any>,
    organizationMemberNoticeLogicMeta
>

export const organizationMemberNoticeLogic = kea<organizationMemberNoticeLogicType>([
    path(['scenes', 'settings', 'organization', 'organizationMemberNoticeLogic']),
    connect(() => ({
        values: [organizationLogic, ['currentOrganization', 'currentOrganizationLoading']],
        actions: [organizationLogic, ['loadCurrentOrganizationSuccess', 'updateOrganizationSuccess']],
    })),
    actions({
        removeMemberNotice: true,
    }),
    forms(({ values }) => ({
        memberNotice: {
            defaults: formFromNotice(null),
            errors: ({ message, label, url }) => ({
                message: !message.trim() ? 'Add a message' : undefined,
                label: url.trim() && !label.trim() ? 'Add a button label, or clear the link' : undefined,
                url:
                    label.trim() && !url.trim()
                        ? 'Add a link, or clear the button label'
                        : url.trim() && !/^https?:\/\//i.test(url.trim())
                          ? 'The link must start with http:// or https://'
                          : undefined,
            }),
            submit: async () => {
                await organizationLogic.asyncActions.updateOrganization({ member_notice: values.draftMemberNotice })
            },
        },
    })),
    selectors({
        savedMemberNotice: [
            (s) => [s.currentOrganization],
            (currentOrganization: OrganizationType | null): OrganizationMemberNoticeApi | null =>
                currentOrganization?.member_notice ?? null,
        ],
        draftMemberNotice: [
            (s) => [s.memberNotice],
            (memberNotice: MemberNoticeForm): OrganizationMemberNoticeApi | null => noticeFromForm(memberNotice),
        ],
        hasUnsavedMemberNotice: [
            (s) => [s.draftMemberNotice, s.savedMemberNotice],
            (
                draftMemberNotice: OrganizationMemberNoticeApi | null,
                savedMemberNotice: OrganizationMemberNoticeApi | null
            ): boolean => {
                const draft = formFromNotice(draftMemberNotice)
                const saved = formFromNotice(savedMemberNotice)
                return draft.message !== saved.message || draft.label !== saved.label || draft.url !== saved.url
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        // The organization reloads after every save, so the draft follows the saved notice.
        loadCurrentOrganizationSuccess: () => {
            actions.resetMemberNotice(formFromNotice(values.savedMemberNotice))
        },
        updateOrganizationSuccess: () => {
            actions.resetMemberNotice(formFromNotice(values.savedMemberNotice))
        },
        removeMemberNotice: () => {
            organizationLogic.actions.updateOrganization({ member_notice: null })
        },
    })),
    afterMount(({ actions, values }) => {
        actions.resetMemberNotice(formFromNotice(values.savedMemberNotice))
    }),
])