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.

3.9 KB
Raw Blame
import { MakeLogicType, actions, kea, key, path, props, reducers, selectors } from 'kea'
import { combineUrl } from 'kea-router'

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

export interface ObservationShareLogicProps {
    observationId: string
    /** Where the recording is paused when the dialog opens, in whole seconds. */
    seconds: number
}

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

/**
 * Absolute link to an observation page. `t` is the same seconds param the player already reads, so the
 * embedded recording starts where the sharer was watching instead of at the beginning.
 */
export function observationShareUrl(observationId: string, form: ObservationShareForm): string {
    // A cleared or unreadable field names no moment, so the param is dropped rather than sent as `t=0`,
    // which would force the recording open at the very start on the reader's page. A typed `00:00` parses
    // to 0 rather than null, so sharing the first second still works.
    const seconds = form.includeTime ? reverseColonDelimitedDuration(form.time) : null
    return combineUrl(urls.absolute(urls.currentProject(urls.replayVisionObservation(observationId))), {
        t: seconds === null ? undefined : `${seconds}`,
    }).url
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationShareLogicValues {
    includeTime: boolean
    shareUrl: string
    time: string | null
    timeError: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationShareLogicActions {
    setIncludeTime: (includeTime: boolean) => {
        includeTime: boolean
    }
    setTime: (time: string | null) => {
        time: string | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationShareLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        timeError: (includeTime: boolean, time: string | null) => string | null
        shareUrl: (includeTime: boolean, time: string | null) => string
    }
}

export type observationShareLogicType = MakeLogicType<
    observationShareLogicValues,
    observationShareLogicActions,
    ObservationShareLogicProps,
    observationShareLogicMeta
>

export const observationShareLogic = kea<observationShareLogicType>([
    path(['products', 'replay_vision', 'frontend', 'observations', 'observationShareLogic']),
    props({} as ObservationShareLogicProps),
    key((props: ObservationShareLogicProps) => `${props.observationId}-${props.seconds}`),

    actions({
        setIncludeTime: (includeTime: boolean) => ({ includeTime }),
        setTime: (time: string | null) => ({ time }),
    }),

    reducers(({ props }) => ({
        includeTime: [
            true,
            {
                setIncludeTime: (_, { includeTime }: { includeTime: boolean }) => includeTime,
                // Typing a time means you want it, so tick the box rather than making people click twice.
                setTime: () => true,
            },
        ],
        time: [
            colonDelimitedDuration(props.seconds, null) as string | null,
            {
                setTime: (_, { time }: { time: string | null }) => time,
            },
        ],
    })),

    selectors(({ props }) => ({
        timeError: [
            (s) => [s.includeTime, s.time],
            (includeTime: boolean, time: string | null): string | null =>
                includeTime && time && reverseColonDelimitedDuration(time) === null
                    ? 'Set a valid time like 02:30 (minutes:seconds)'
                    : null,
        ],
        shareUrl: [
            (s) => [s.includeTime, s.time],
            (includeTime: boolean, time: string | null): string =>
                observationShareUrl(props.observationId, { includeTime, time }),
        ],
    })),
])