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.8 KB
Raw Blame
import { MakeLogicType, kea, key, path, props, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { combineUrl } from 'kea-router'

import { colonDelimitedDuration, reverseColonDelimitedDuration } from 'lib/utils/durations'
import { urls } from 'scenes/urls'

export interface FormWithTime {
    includeTime: boolean
    time: string | null
}

function makePrivateLinkQueryParams(formWithTime: FormWithTime): Record<string, string | undefined> {
    return {
        t: formWithTime.includeTime ? `${reverseColonDelimitedDuration(formWithTime.time) || 0}` : undefined,
    }
}

export function makePrivateLink(id: string, formWithTime: FormWithTime): string {
    return combineUrl(
        urls.absolute(urls.currentProject(urls.replaySingle(id))),
        makePrivateLinkQueryParams(formWithTime)
    ).url
}

export type PlayerShareLogicProps = {
    seconds: number | null
    id: string
    shareType?: 'private' | 'public'
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerShareLogicValues {
    isPrivateLinkFormSubmitting: boolean
    isPrivateLinkFormValid: boolean
    privateLinkForm: FormWithTime
    privateLinkFormAllErrors: Record<string, any>
    privateLinkFormChanged: boolean
    privateLinkFormErrors: DeepPartialMap<FormWithTime, ValidationErrorType>
    privateLinkFormHasErrors: boolean
    privateLinkFormManualErrors: Record<string, any>
    privateLinkFormTouched: boolean
    privateLinkFormTouches: Record<string, boolean>
    privateLinkFormValidationErrors: DeepPartialMap<FormWithTime, ValidationErrorType>
    privateLinkUrl: string
    privateLinkUrlQueryParams: Record<string, string | undefined>
    showPrivateLinkFormErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerShareLogicActions {
    resetPrivateLinkForm: (values?: FormWithTime) => {
        values?: FormWithTime
    }
    setPrivateLinkFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setPrivateLinkFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setPrivateLinkFormValues: (values: DeepPartial<FormWithTime>) => {
        values: DeepPartial<FormWithTime>
    }
    submitPrivateLinkForm: () => {
        value: boolean
    }
    submitPrivateLinkFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitPrivateLinkFormRequest: (privateLinkForm: FormWithTime) => {
        privateLinkForm: FormWithTime
    }
    submitPrivateLinkFormSuccess: (privateLinkForm: FormWithTime) => {
        privateLinkForm: FormWithTime
    }
    touchPrivateLinkFormField: (key: string) => {
        key: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerShareLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        privateLinkUrlQueryParams: (privateLinkForm: FormWithTime) => Record<string, string | undefined>
        privateLinkUrl: (privateLinkForm: FormWithTime) => string
    }
}

export type playerShareLogicType = MakeLogicType<
    playerShareLogicValues,
    playerShareLogicActions,
    PlayerShareLogicProps,
    playerShareLogicMeta
>

export const playerShareLogic = kea<playerShareLogicType>([
    path(() => ['scenes', 'session-recordings', 'player', 'playerShareLogic']),
    props({} as PlayerShareLogicProps),
    key((props: PlayerShareLogicProps) => `${props.id}-${props.seconds}`),

    forms(({ props }) => ({
        privateLinkForm: {
            defaults: { includeTime: true, time: colonDelimitedDuration(props.seconds, null) } as FormWithTime,
            errors: ({ time, includeTime }) => ({
                time:
                    time && includeTime && reverseColonDelimitedDuration(time || undefined) === null
                        ? 'Set a valid time like 02:30 (minutes:seconds)'
                        : undefined,
            }),
            options: {
                // whether we show errors after touch (true) or submit (false)
                showErrorsOnTouch: true,

                // show errors even without submitting first
                alwaysShowErrors: true,
            },
        },
    })),

    selectors(({ props }) => ({
        privateLinkUrlQueryParams: [
            (s) => [s.privateLinkForm],
            (privateLinkForm: FormWithTime) => {
                return makePrivateLinkQueryParams(privateLinkForm)
            },
        ],
        privateLinkUrl: [
            (s) => [s.privateLinkForm],
            (privateLinkForm: FormWithTime) => {
                return makePrivateLink(props.id, privateLinkForm)
            },
        ],
    })),
])