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.7 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'

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

import { SharePasswordType } from '~/types'

export interface SharePasswordsLogicProps {
    dashboardId?: number
    insightId?: number
    recordingId?: string
    notebookShortId?: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sharePasswordsLogicValues {
    createdPasswordResult: {
        created_at: string
        created_by_email: string
        id: string
        note: string
        password: string
    } | null
    isCreatingPassword: boolean
    newPasswordModalOpen: boolean
    sharePasswords: SharePasswordType[]
    sharePasswordsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sharePasswordsLogicActions {
    clearCreatedPasswordResult: () => {
        value: true
    }
    createPassword: (
        password?: string,
        note?: string
    ) => {
        note: string | undefined
        password: string | undefined
    }
    createPasswordFailure: (error: any) => {
        error: any
    }
    createPasswordSuccess: (response: any) => {
        response: any
    }
    deletePassword: (passwordId: string) => {
        passwordId: string
    }
    loadSharePasswords: () => {
        value: true
    }
    loadSharePasswordsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSharePasswordsSuccess: (
        sharePasswords: SharePasswordType[],
        payload?: {
            value: true
        }
    ) => {
        sharePasswords: SharePasswordType[]
        payload?: {
            value: true
        }
    }
    setNewPasswordModalOpen: (open: boolean) => {
        open: boolean
    }
    setSharePasswords: (passwords: SharePasswordType[]) => {
        passwords: SharePasswordType[]
    }
}

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

export type sharePasswordsLogicType = MakeLogicType<
    sharePasswordsLogicValues,
    sharePasswordsLogicActions,
    SharePasswordsLogicProps,
    sharePasswordsLogicMeta
>

export const sharePasswordsLogic = kea<sharePasswordsLogicType>([
    path(['lib', 'components', 'Sharing', 'sharePasswordsLogic']),
    props({} as SharePasswordsLogicProps),
    key(
        ({ dashboardId, insightId, recordingId, notebookShortId }) =>
            `${dashboardId || 'no-dashboard'}-${insightId || 'no-insight'}-${recordingId || 'no-recording'}-${notebookShortId || 'no-notebook'}`
    ),

    actions({
        setNewPasswordModalOpen: (open: boolean) => ({ open }),
        createPassword: (password?: string, note?: string) => ({ password, note }),
        createPasswordSuccess: (response: any) => ({ response }),
        createPasswordFailure: (error: any) => ({ error }),
        deletePassword: (passwordId: string) => ({ passwordId }),
        setSharePasswords: (passwords: SharePasswordType[]) => ({ passwords }),
        loadSharePasswords: true,
        clearCreatedPasswordResult: true,
    }),

    loaders(({ props }) => ({
        sharePasswords: {
            __default: [] as SharePasswordType[],
            loadSharePasswords: async (): Promise<SharePasswordType[]> => {
                const response = await api.sharing.get({
                    dashboardId: props.dashboardId,
                    insightId: props.insightId,
                    recordingId: props.recordingId,
                    notebookShortId: props.notebookShortId,
                })
                return (response && (response as any).share_passwords) || []
            },
        },
    })),

    reducers({
        newPasswordModalOpen: [
            false,
            {
                setNewPasswordModalOpen: (_, { open }) => open,
            },
        ],
        isCreatingPassword: [
            false as boolean,
            {
                createPassword: () => true,
                createPasswordSuccess: () => false,
                createPasswordFailure: () => false,
            },
        ],
        createdPasswordResult: [
            null as { id: string; password: string; note: string; created_at: string; created_by_email: string } | null,
            {
                createPasswordSuccess: (_, { response }) => response,
                createPassword: () => null,
                createPasswordFailure: () => null,
                clearCreatedPasswordResult: () => null,
            },
        ],
    }),

    listeners(({ actions, props }) => ({
        createPassword: async ({ password, note }) => {
            try {
                const response = await api.sharing.createPassword(
                    {
                        dashboardId: props.dashboardId,
                        insightId: props.insightId,
                        recordingId: props.recordingId,
                        notebookShortId: props.notebookShortId,
                    },
                    {
                        raw_password: password,
                        note: note,
                    }
                )

                actions.createPasswordSuccess(response)
                lemonToast.success('Password created successfully')

                // Reload passwords list (but don't let this failure affect the success)
                try {
                    actions.loadSharePasswords()
                } catch (loadError) {
                    console.warn('Failed to reload passwords after creation:', loadError)
                }

                // Return void - response is already stored in createPasswordSuccess
            } catch (error: any) {
                console.error('Password creation error:', error)
                actions.createPasswordFailure(error)
                lemonToast.error(error.detail || error.message || 'Failed to create password')
            }
        },

        deletePassword: async ({ passwordId }) => {
            try {
                await api.sharing.deletePassword(
                    {
                        dashboardId: props.dashboardId,
                        insightId: props.insightId,
                        recordingId: props.recordingId,
                        notebookShortId: props.notebookShortId,
                    },
                    passwordId
                )

                actions.loadSharePasswords()
                lemonToast.success('Password deleted')
            } catch (error: any) {
                lemonToast.error(error.detail || 'Failed to delete password')
            }
        },
    })),
])