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.

5.1 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { teamLogic } from 'scenes/teamLogic'

import { visualReviewReposList, visualReviewReposRetrieve } from '../generated/api'
import type { RepoApi } from '../generated/api.schemas'
import { isNotFoundError, resolveRepoId } from '../lib/repoIds'
import { visualReviewPreferencesLogic } from './visualReviewPreferencesLogic'

export interface VisualReviewRepoLogicProps {
    repoFullName: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewRepoLogicValues {
    currentProjectId: number | string // teamLogic
    allRepos: RepoApi[]
    allReposLoading: boolean
    otherRepos: RepoApi[]
    repo: RepoApi | null
    repoLoading: boolean
    repoNotFound: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewRepoLogicActions {
    rememberRepo: (
        teamId: number,
        repoFullName: string
    ) => {
        repoFullName: string
        teamId: number
    } // visualReviewPreferencesLogic
    loadAllRepos: () => any
    loadAllReposFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAllReposSuccess: (
        allRepos: RepoApi[],
        payload?: any
    ) => {
        allRepos: RepoApi[]
        payload?: any
    }
    loadRepo: () => any
    loadRepoFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRepoSuccess: (
        repo: RepoApi | null,
        payload?: any
    ) => {
        repo: RepoApi | null
        payload?: any
    }
    setRepoNotFound: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewRepoLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        otherRepos: (allRepos: RepoApi[], arg: any) => RepoApi[]
    }
}

export type visualReviewRepoLogicType = MakeLogicType<
    visualReviewRepoLogicValues,
    visualReviewRepoLogicActions,
    VisualReviewRepoLogicProps,
    visualReviewRepoLogicMeta
>

// Shared repo state for the Runs, Snapshots and Flakiness scenes. It is keyed by the repo's
// full name from the URL so the data only loads once per workspace and survives tab navigation.
// Without this, each scene's own loadRepo re-fetches on mount and the title/tab
// strip flicker as the response comes back.
export const visualReviewRepoLogic = kea<visualReviewRepoLogicType>([
    path(['products', 'visual_review', 'frontend', 'scenes', 'visualReviewRepoLogic']),
    props({} as VisualReviewRepoLogicProps),
    key((props) => props.repoFullName),
    connect(() => ({
        values: [teamLogic, ['currentProjectId']],
        actions: [visualReviewPreferencesLogic, ['rememberRepo']],
    })),
    actions({
        setRepoNotFound: true,
    }),
    loaders(({ actions, props, values }) => ({
        repo: [
            null as RepoApi | null,
            {
                loadRepo: async () => {
                    const projectId = String(values.currentProjectId)
                    try {
                        return await visualReviewReposRetrieve(
                            projectId,
                            await resolveRepoId(projectId, props.repoFullName)
                        )
                    } catch (error) {
                        if (isNotFoundError(error)) {
                            actions.setRepoNotFound()
                            return null
                        }
                        throw error
                    }
                },
            },
        ],
        // Sibling repos surface in the title-bar dropdown so a multi-repo
        // workspace can switch with a single click. Loaded lazily — single-
        // repo workspaces never render the dropdown, but the request is cheap.
        allRepos: [
            [] as RepoApi[],
            {
                loadAllRepos: async () => {
                    const response = await visualReviewReposList(String(values.currentProjectId))
                    return response.results ?? []
                },
            },
        ],
    })),
    reducers({
        repoNotFound: [
            false,
            {
                loadRepo: () => false,
                setRepoNotFound: () => true,
            },
        ],
    }),
    selectors({
        otherRepos: [
            (s) => [s.allRepos, (_, p) => p.repoFullName],
            (allRepos: RepoApi[], repoFullName: string): RepoApi[] =>
                allRepos.filter((r) => r.repo_full_name.toLowerCase() !== repoFullName.toLowerCase()),
        ],
    }),
    listeners(({ actions }) => ({
        loadRepoSuccess: ({ repo }) => {
            if (repo) {
                actions.rememberRepo(repo.team_id, repo.repo_full_name)
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadRepo()
        actions.loadAllRepos()
    }),
])