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.

23.0 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { actionToUrl, urlToAction } from 'kea-router'

import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { Breadcrumb } from '~/types'

import type { FacetBucket, FacetGroups, FacetSelection } from '../components/SnapshotFacetSidebar'
import type { StatPreset } from '../components/SnapshotStatRow'
import { visualReviewReposBaselinesRetrieve } from '../generated/api'
import type { BaselineEntryApi, BaselineOverviewApi } from '../generated/api.schemas'
import type { RepoApi } from '../generated/api.schemas'
import { parseArea, parseTheme, runTypeLabel } from '../lib/parseIdentifier'
import { repoBreadcrumbs } from '../lib/repoBreadcrumbs'
import { resolveRepoIdOrNull } from '../lib/repoIds'
import { repoFullNameFromParams, splitRepoFullName } from '../lib/repoPaths'
import { visualReviewRepoLogic } from './visualReviewRepoLogic'

export interface VisualReviewSnapshotOverviewSceneLogicProps {
    repoFullName: string
}

// We deliberately don't model "show both themes" — for snapshot identifiers
// that ship as light+dark pairs that would double the grid. Picking one side
// dedups by default. Identifiers without a theme suffix are always shown
// regardless of which side is selected.
export type ThemeFilter = 'light' | 'dark'
export type Filters = {
    statPreset: StatPreset
    typeKeys: string[]
    areas: string[]
    stability: string[]
    theme: ThemeFilter
    search: string
}

const EMPTY_FILTERS: Filters = {
    statPreset: 'all',
    typeKeys: [],
    areas: [],
    stability: [],
    theme: 'light',
    search: '',
}

// We collapse run_type + browser into a single "type key" for the facet —
// matches the design's "Storybook 921 / Playwright · chromium 407 / ..." rows.
// Handles two seed conventions in the wild: `run_type=playwright` with browser
// in metadata, or `run_type=playwright-<browser>` baked into the column.
function typeKeyOf(entry: BaselineEntryApi): string {
    const rt = entry.run_type.toLowerCase()
    if (rt === 'playwright' && entry.browser) {
        return `playwright::${entry.browser}`
    }
    if (rt.startsWith('playwright-')) {
        return `playwright::${rt.slice('playwright-'.length)}`
    }
    return entry.run_type
}

function typeLabelOf(key: string): string {
    if (key.startsWith('playwright::')) {
        return runTypeLabel('playwright', key.slice('playwright::'.length))
    }
    return key
}

const STABILITY_KEYS = {
    clean_30d: 'Clean (30d)',
    tolerated_30d: 'Tolerated (30d)',
    quarantined: 'Quarantined',
}

function entryStability(entry: BaselineEntryApi): Array<keyof typeof STABILITY_KEYS> {
    const out: Array<keyof typeof STABILITY_KEYS> = []
    if (entry.is_quarantined) {
        out.push('quarantined')
    }
    if (entry.tolerate_count_30d > 0) {
        out.push('tolerated_30d')
    }
    if (!entry.is_quarantined && entry.tolerate_count_30d === 0) {
        out.push('clean_30d')
    }
    return out
}

function matchesStatPreset(entry: BaselineEntryApi, preset: StatPreset): boolean {
    switch (preset) {
        case 'tolerated_drift':
            return entry.tolerate_count_30d >= 1
        case 'currently_quarantined':
            return entry.is_quarantined
        case 'all':
        default:
            return true
    }
}

function bucketize(values: string[], labelOf: (v: string) => string = (v) => v): FacetBucket[] {
    const counts = new Map<string, number>()
    for (const v of values) {
        counts.set(v, (counts.get(v) ?? 0) + 1)
    }
    return [...counts.entries()]
        .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
        .map(([value, count]) => ({ value, count, label: labelOf(value) }))
}

// Pre-decorated entry shape — the kea selector annotates this on every entry
// once so subsequent filter passes can compare without re-deriving area/theme/etc.
// Exported so the kea-typegen output can resolve it.
export type DecoratedEntry = BaselineEntryApi & {
    _area: string
    _theme: 'light' | 'dark' | null
    _typeKey: string
    _stability: string[]
}

// Filter dimensions a caller can opt out of, plus the "preset" stat row.
type FilterDimension = keyof Filters | 'preset'

// Single source of truth for filter logic. `filteredEntries` calls this with no
// exclusion (full set narrows down); `facetGroups` calls it once per dimension
// excluding that dimension so its own facet rows don't zero each other out.
// Without this, the same six clauses lived twice and any new dimension had to
// be added in both places (the bug greptile flagged).
function applyFilters(
    entries: readonly DecoratedEntry[],
    filters: Filters,
    exclude?: FilterDimension
): DecoratedEntry[] {
    const search = filters.search.trim().toLowerCase()
    return entries.filter((e) => {
        if (exclude !== 'preset' && !matchesStatPreset(e, filters.statPreset)) {
            return false
        }
        if (exclude !== 'typeKeys' && filters.typeKeys.length && !filters.typeKeys.includes(e._typeKey)) {
            return false
        }
        if (exclude !== 'areas' && filters.areas.length && !filters.areas.includes(e._area)) {
            return false
        }
        if (
            exclude !== 'stability' &&
            filters.stability.length &&
            !filters.stability.some((s) => e._stability.includes(s as keyof typeof STABILITY_KEYS))
        ) {
            return false
        }
        if (exclude !== 'theme' && e._theme !== null && e._theme !== filters.theme) {
            return false
        }
        if (exclude !== 'search' && search && !e.identifier.toLowerCase().includes(search)) {
            return false
        }
        return true
    })
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewSnapshotOverviewSceneLogicValues {
    currentProjectId: number | string // teamLogic
    repo: RepoApi | null // visualReviewRepoLogic
    repoNotFound: boolean // visualReviewRepoLogic
    breadcrumbs: Breadcrumb[]
    decoratedEntries: Array<
        BaselineEntryApi & {
            _area: string
            _stability: string[]
            _theme: 'dark' | 'light' | null
            _typeKey: string
        }
    >
    entries: BaselineEntryApi[]
    facetGroups: FacetGroups
    facetSelection: FacetSelection
    filteredEntries: DecoratedEntry[]
    filters: Filters
    frequentlyToleratedCount: number
    overview: BaselineOverviewApi | null
    overviewLoading: boolean
    overviewRepoId: string | null
    repoFullName: string
    statCounts: Record<StatPreset, number>
    thumbnailBasePath: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewSnapshotOverviewSceneLogicActions {
    clearAllFilters: () => {
        value: true
    }
    loadOverview: () => any
    loadOverviewFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOverviewSuccess: (
        overview: BaselineOverviewApi | null,
        payload?: any
    ) => {
        overview: BaselineOverviewApi | null
        payload?: any
    }
    setOverviewRepoId: (repoId: string) => {
        repoId: string
    }
    setSearch: (search: string) => {
        search: string
    }
    setStatPreset: (preset: StatPreset) => {
        preset: StatPreset
    }
    setTheme: (theme: ThemeFilter) => {
        theme: ThemeFilter
    }
    toggleArea: (value: string) => {
        value: string
    }
    toggleStability: (value: string) => {
        value: string
    }
    toggleType: (value: string) => {
        value: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visualReviewSnapshotOverviewSceneLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        repoFullName: (arg: any) => string
        entries: (overview: BaselineOverviewApi | null) => BaselineEntryApi[]
        decoratedEntries: (entries: BaselineEntryApi[]) => Array<
            BaselineEntryApi & {
                _area: string
                _stability: string[]
                _theme: 'dark' | 'light' | null
                _typeKey: string
            }
        >
        filteredEntries: (
            decoratedEntries: (BaselineEntryApi & {
                _area: string
                _stability: string[]
                _theme: 'dark' | 'light' | null
                _typeKey: string
            })[],
            filters: Filters
        ) => DecoratedEntry[]
        frequentlyToleratedCount: (
            decoratedEntries: (BaselineEntryApi & {
                _area: string
                _stability: string[]
                _theme: 'dark' | 'light' | null
                _typeKey: string
            })[],
            overview: BaselineOverviewApi | null
        ) => number
        statCounts: (
            decoratedEntries: (BaselineEntryApi & {
                _area: string
                _stability: string[]
                _theme: 'dark' | 'light' | null
                _typeKey: string
            })[],
            overview: BaselineOverviewApi | null
        ) => Record<StatPreset, number>
        facetGroups: (
            decoratedEntries: (BaselineEntryApi & {
                _area: string
                _stability: string[]
                _theme: 'dark' | 'light' | null
                _typeKey: string
            })[],
            filters: Filters
        ) => FacetGroups
        facetSelection: (filters: Filters) => FacetSelection
        thumbnailBasePath: (currentProjectId: number | string, overviewRepoId: string | null) => string | null
        breadcrumbs: (repo: RepoApi | null, repoFullName: string) => Breadcrumb[]
    }
}

export type visualReviewSnapshotOverviewSceneLogicType = MakeLogicType<
    visualReviewSnapshotOverviewSceneLogicValues,
    visualReviewSnapshotOverviewSceneLogicActions,
    VisualReviewSnapshotOverviewSceneLogicProps,
    visualReviewSnapshotOverviewSceneLogicMeta
>

export const visualReviewSnapshotOverviewSceneLogic = kea<visualReviewSnapshotOverviewSceneLogicType>([
    path(['products', 'visual_review', 'frontend', 'scenes', 'visualReviewSnapshotOverviewSceneLogic']),
    props({} as VisualReviewSnapshotOverviewSceneLogicProps),
    key((props) => props.repoFullName),
    connect((props: VisualReviewSnapshotOverviewSceneLogicProps) => ({
        values: [
            teamLogic,
            ['currentProjectId'],
            visualReviewRepoLogic({ repoFullName: props.repoFullName }),
            ['repo', 'repoNotFound'],
        ],
    })),
    actions({
        setStatPreset: (preset: StatPreset) => ({ preset }),
        toggleType: (value: string) => ({ value }),
        toggleArea: (value: string) => ({ value }),
        toggleStability: (value: string) => ({ value }),
        setTheme: (theme: ThemeFilter) => ({ theme }),
        setSearch: (search: string) => ({ search }),
        clearAllFilters: true,
        setOverviewRepoId: (repoId: string) => ({ repoId }),
    }),
    reducers({
        overviewRepoId: [
            null as string | null,
            {
                setOverviewRepoId: (_, { repoId }) => repoId,
            },
        ],
        filters: [
            EMPTY_FILTERS,
            {
                setStatPreset: (state, { preset }) => ({ ...state, statPreset: preset }),
                toggleType: (state, { value }) => ({
                    ...state,
                    typeKeys: state.typeKeys.includes(value)
                        ? state.typeKeys.filter((v) => v !== value)
                        : [...state.typeKeys, value],
                }),
                toggleArea: (state, { value }) => ({
                    ...state,
                    areas: state.areas.includes(value)
                        ? state.areas.filter((v) => v !== value)
                        : [...state.areas, value],
                }),
                toggleStability: (state, { value }) => ({
                    ...state,
                    stability: state.stability.includes(value)
                        ? state.stability.filter((v) => v !== value)
                        : [...state.stability, value],
                }),
                setTheme: (state, { theme }) => ({ ...state, theme }),
                setSearch: (state, { search }) => ({ ...state, search }),
                clearAllFilters: () => EMPTY_FILTERS,
            },
        ],
    }),
    loaders(({ actions, props, values }) => ({
        overview: [
            null as BaselineOverviewApi | null,
            {
                loadOverview: async () => {
                    const projectId = String(values.currentProjectId)
                    const repoId = await resolveRepoIdOrNull(projectId, props.repoFullName)
                    if (!repoId) {
                        return null
                    }
                    actions.setOverviewRepoId(repoId)
                    return visualReviewReposBaselinesRetrieve(projectId, repoId)
                },
            },
        ],
    })),
    selectors({
        repoFullName: [() => [(_, p) => p.repoFullName], (repoFullName: string): string => repoFullName],
        entries: [
            (s) => [s.overview],
            (overview: BaselineOverviewApi | null): BaselineEntryApi[] => overview?.entries ?? [],
        ],
        // Pre-compute parsed area + theme + typeKey + stability tags for each
        // entry so subsequent filter passes don't re-derive on every keystroke.
        decoratedEntries: [
            (s) => [s.entries],
            (
                entries: BaselineEntryApi[]
            ): Array<
                BaselineEntryApi & {
                    _area: string
                    _theme: 'light' | 'dark' | null
                    _typeKey: string
                    _stability: string[]
                }
            > =>
                entries.map((e) => ({
                    ...e,
                    _area: parseArea(e.identifier),
                    _theme: parseTheme(e.identifier).theme,
                    _typeKey: typeKeyOf(e),
                    _stability: entryStability(e),
                })),
        ],
        // Single sort axis across every preset — drift severity descending
        // with alphabetic tiebreak. Drift is what the visible meta row leads
        // with on each card, so the toolbar indicator now matches what the
        // user sees. Originally we had alpha for "All" and drift for the
        // problem slices, but two axes meant the toolbar label had to keep
        // re-explaining itself; one axis everywhere is calmer.
        filteredEntries: [
            (s) => [s.decoratedEntries, s.filters],
            (
                entries: Array<
                    BaselineEntryApi & {
                        _area: string
                        _stability: string[]
                        _theme: 'dark' | 'light' | null
                        _typeKey: string
                    }
                >,
                filters: Filters
            ) => {
                const filtered = applyFilters(entries, filters)
                filtered.sort(
                    (a, b) =>
                        (b.recent_drift_avg ?? 0) - (a.recent_drift_avg ?? 0) ||
                        b.baseline_change_count - a.baseline_change_count ||
                        b.tolerate_count_30d - a.tolerate_count_30d ||
                        a.identifier.localeCompare(b.identifier)
                )
                return filtered
            },
        ],
        // Trust-debt count surfaced inline on the Tolerated tile. Falls back
        // to recomputing client-side when the server didn't ship totals.
        frequentlyToleratedCount: [
            (s) => [s.decoratedEntries, s.overview],
            (
                entries: Array<
                    BaselineEntryApi & {
                        _area: string
                        _stability: string[]
                        _theme: 'dark' | 'light' | null
                        _typeKey: string
                    }
                >,
                overview: BaselineOverviewApi | null
            ): number =>
                overview?.totals.frequently_tolerated ?? entries.filter((e) => e.tolerate_count_90d >= 3).length,
        ],
        statCounts: [
            (s) => [s.decoratedEntries, s.overview],
            (
                entries: Array<
                    BaselineEntryApi & {
                        _area: string
                        _stability: string[]
                        _theme: 'dark' | 'light' | null
                        _typeKey: string
                    }
                >,
                overview: BaselineOverviewApi | null
            ): Record<StatPreset, number> => {
                const totals = overview?.totals
                // When the filter set is "neutral" (preset=all) we want the
                // stat row to show the full universe counts even if entries
                // is truncated. Use server totals as the source of truth.
                if (totals) {
                    return {
                        all: totals.all_snapshots,
                        tolerated_drift: totals.recently_tolerated,
                        currently_quarantined: totals.currently_quarantined,
                    }
                }
                return {
                    all: entries.length,
                    tolerated_drift: entries.filter((e) => e.tolerate_count_30d >= 1).length,
                    currently_quarantined: entries.filter((e) => e.is_quarantined).length,
                }
            },
        ],
        // Facet buckets recompute over the filtered set so counts narrow as
        // the user picks more filters — except for the dimension being faceted
        // (otherwise picking a TYPE would zero out every other TYPE bucket).
        facetGroups: [
            (s) => [s.decoratedEntries, s.filters],
            (
                entries: Array<
                    BaselineEntryApi & {
                        _area: string
                        _stability: string[]
                        _theme: 'dark' | 'light' | null
                        _typeKey: string
                    }
                >,
                filters: Filters
            ): FacetGroups => {
                const typeBase = applyFilters(entries, filters, 'typeKeys')
                const areaBase = applyFilters(entries, filters, 'areas')
                const stabilityBase = applyFilters(entries, filters, 'stability')
                return {
                    type: bucketize(
                        typeBase.map((e) => e._typeKey),
                        typeLabelOf
                    ),
                    area: bucketize(areaBase.map((e) => e._area)),
                    stability: Object.entries(STABILITY_KEYS).map(([k, label]) => ({
                        value: k,
                        label,
                        count: stabilityBase.filter((e) => e._stability.includes(k as keyof typeof STABILITY_KEYS))
                            .length,
                    })),
                    team: [],
                }
            },
        ],
        facetSelection: [
            (s) => [s.filters],
            (filters: Filters): FacetSelection => ({
                type: new Set(filters.typeKeys),
                area: new Set(filters.areas),
                stability: new Set(filters.stability),
                team: new Set<string>(),
            }),
        ],
        // The overview request already resolved the repo, so a failed repo load does not hide every thumbnail.
        thumbnailBasePath: [
            (s) => [s.currentProjectId, s.overviewRepoId],
            (projectId: number | string, repoId: string | null): string | null =>
                projectId && repoId ? `/api/projects/${projectId}/visual_review/repos/${repoId}/thumbnails` : null,
        ],
        breadcrumbs: [
            (s) => [s.repo, s.repoFullName],
            (repo: RepoApi | null, repoFullName: string): Breadcrumb[] =>
                repoBreadcrumbs(repo?.repo_full_name ?? repoFullName),
        ],
    }),
    actionToUrl(({ values, props }) => {
        const buildHash = (): Record<string, string> => {
            const f = values.filters
            const out: Record<string, string> = {}
            if (f.statPreset !== 'all') {
                out.preset = f.statPreset
            }
            if (f.typeKeys.length) {
                out.types = f.typeKeys.join(',')
            }
            if (f.areas.length) {
                out.areas = f.areas.join(',')
            }
            if (f.stability.length) {
                out.stability = f.stability.join(',')
            }
            if (f.theme !== EMPTY_FILTERS.theme) {
                out.theme = f.theme
            }
            if (f.search) {
                out.q = f.search
            }
            return out
        }
        const path = urls.visualReviewSnapshotOverview(...splitRepoFullName(props.repoFullName))
        return {
            setStatPreset: () => [path, {}, buildHash()],
            toggleType: () => [path, {}, buildHash()],
            toggleArea: () => [path, {}, buildHash()],
            toggleStability: () => [path, {}, buildHash()],
            setTheme: () => [path, {}, buildHash()],
            setSearch: () => [path, {}, buildHash()],
            clearAllFilters: () => [path, {}, {}],
        }
    }),
    urlToAction(({ actions, values, props }) => ({
        '/visual-review/repos/:repoOwner/:repoName/snapshots': (params, _searchParams, hash) => {
            if (repoFullNameFromParams(params.repoOwner, params.repoName) !== props.repoFullName) {
                return
            }
            const f = values.filters
            const next: Filters = {
                statPreset: (hash.preset as StatPreset) ?? 'all',
                typeKeys: hash.types ? hash.types.split(',') : [],
                areas: hash.areas ? hash.areas.split(',') : [],
                stability: hash.stability ? hash.stability.split(',') : [],
                theme: hash.theme === 'dark' ? 'dark' : 'light',
                search: hash.q ?? '',
            }
            if (next.statPreset !== f.statPreset) {
                actions.setStatPreset(next.statPreset)
            }
            if (next.search !== f.search) {
                actions.setSearch(next.search)
            }
            if (next.theme !== f.theme) {
                actions.setTheme(next.theme)
            }
            // For multi-selects, only diff if different.
            if (next.typeKeys.join(',') !== f.typeKeys.join(',')) {
                f.typeKeys.forEach((v) => actions.toggleType(v))
                next.typeKeys.forEach((v) => actions.toggleType(v))
            }
            if (next.areas.join(',') !== f.areas.join(',')) {
                f.areas.forEach((v) => actions.toggleArea(v))
                next.areas.forEach((v) => actions.toggleArea(v))
            }
            if (next.stability.join(',') !== f.stability.join(',')) {
                f.stability.forEach((v) => actions.toggleStability(v))
                next.stability.forEach((v) => actions.toggleStability(v))
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadOverview()
    }),
])