: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, LogicWrapper, actions, connect, kea, key, listeners, path, props, reducers } from 'kea'
import { teamLogic } from 'scenes/teamLogic'
import { canvasesViewRetrieve } from 'products/canvas/frontend/generated/api'
import type { CanvasApi, CanvasViewResponseApi } from 'products/canvas/frontend/generated/api.schemas'
export const SPACE_CANVAS_PREVIEWS_MAX_LOADING = 3
export type SpaceCanvasPreviewStatus = 'queued' | 'loading' | 'ready' | 'unavailable'
export interface SpaceCanvasPreview {
canvasId: string
status: SpaceCanvasPreviewStatus
artifactUrl: string | null
}
export interface SpaceCanvasPreviewsLogicProps {
spaceId: string
}
/** Null when the canvas has nothing a preview can render, so the card never asks for one. */
export function spaceCanvasPreviewKey(canvas: CanvasApi): string | null {
if (canvas.kind === 'grid' || !canvas.published_build_id) {
return null
}
return `${canvas.id}:${canvas.published_build_id}`
}
export function spaceCanvasPreviewUrl(view: CanvasViewResponseApi): string | null {
const build = view.published_build
return build?.build_status === 'ready' ? (build.artifact_url ?? null) : null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spaceCanvasPreviewsLogicValues {
currentTeamId: number | null // teamLogic
previews: Record<string, SpaceCanvasPreview>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spaceCanvasPreviewsLogicActions {
previewSettled: (
previewKey: string,
artifactUrl: string | null
) => {
artifactUrl: string | null
previewKey: string
}
previewStarted: (
previewKey: string,
canvasId: string
) => {
canvasId: string
previewKey: string
}
requestPreview: (
previewKey: string,
canvasId: string
) => {
canvasId: string
previewKey: string
}
startQueuedPreviews: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spaceCanvasPreviewsLogicMeta {
key: string
}
export type spaceCanvasPreviewsLogicType = MakeLogicType<
spaceCanvasPreviewsLogicValues,
spaceCanvasPreviewsLogicActions,
SpaceCanvasPreviewsLogicProps,
spaceCanvasPreviewsLogicMeta
>
export const spaceCanvasPreviewsLogic: LogicWrapper<spaceCanvasPreviewsLogicType> = kea<spaceCanvasPreviewsLogicType>([
props({} as SpaceCanvasPreviewsLogicProps),
key((props) => props.spaceId),
path((spaceId) => ['products', 'tasks', 'spaces', 'spaceCanvasPreviewsLogic', spaceId]),
connect(() => ({ values: [teamLogic, ['currentTeamId']] })),
actions({
requestPreview: (previewKey: string, canvasId: string) => ({ previewKey, canvasId }),
previewStarted: (previewKey: string, canvasId: string) => ({ previewKey, canvasId }),
previewSettled: (previewKey: string, artifactUrl: string | null) => ({ previewKey, artifactUrl }),
startQueuedPreviews: true,
}),
reducers({
previews: [
{} as Record<string, SpaceCanvasPreview>,
{
requestPreview: (state, { previewKey, canvasId }) =>
state[previewKey]
? state
: { ...state, [previewKey]: { canvasId, status: 'queued', artifactUrl: null } },
previewStarted: (state, { previewKey, canvasId }) => ({
...state,
[previewKey]: { canvasId, status: 'loading', artifactUrl: null },
}),
previewSettled: (state, { previewKey, artifactUrl }) =>
state[previewKey]
? {
...state,
[previewKey]: {
...state[previewKey],
status: artifactUrl ? 'ready' : 'unavailable',
artifactUrl,
},
}
: state,
},
],
}),
listeners(({ actions, values }) => ({
requestPreview: () => actions.startQueuedPreviews(),
previewSettled: () => actions.startQueuedPreviews(),
startQueuedPreviews: () => {
const entries = Object.entries(values.previews)
let free =
SPACE_CANVAS_PREVIEWS_MAX_LOADING - entries.filter(([, preview]) => preview.status === 'loading').length
for (const [previewKey, preview] of entries) {
if (free <= 0) {
return
}
if (preview.status === 'queued') {
free -= 1
actions.previewStarted(previewKey, preview.canvasId)
}
}
},
previewStarted: async ({ previewKey, canvasId }) => {
let artifactUrl: string | null = null
try {
if (values.currentTeamId) {
artifactUrl = spaceCanvasPreviewUrl(
await canvasesViewRetrieve(String(values.currentTeamId), canvasId)
)
}
} catch {
artifactUrl = null
}
actions.previewSettled(previewKey, artifactUrl)
},
})),
])