: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, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { router } from 'kea-router'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { projectLogic } from 'scenes/projectLogic'
import { urls } from 'scenes/urls'
import {
sandboxCustomImagesCreate,
sandboxCustomImagesDestroy,
sandboxCustomImagesList,
} from 'products/tasks/frontend/generated/api'
import type { SandboxCustomImageDTOApi } from 'products/tasks/frontend/generated/api.schemas'
export interface CloudImageDraft {
name: string
description: string
repository: string
private: boolean
}
const NEW_IMAGE: CloudImageDraft = { name: '', description: '', repository: '', private: true }
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cloudImagesLogicValues {
currentProjectId: number | null // projectLogic
draft: CloudImageDraft | null
draftError: string | null
draftSaving: boolean
images: SandboxCustomImageDTOApi[] | null
imagesFailed: boolean
imagesLoading: boolean
readyImages: SandboxCustomImageDTOApi[]
unavailable: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cloudImagesLogicActions {
closeDraft: () => {
value: true
}
createImage: () => {
value: true
}
createImageDone: () => {
value: true
}
deleteImage: (image: SandboxCustomImageDTOApi) => {
image: SandboxCustomImageDTOApi
}
imagesUnavailable: () => {
value: true
}
loadImages: () => any
loadImagesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadImagesSuccess: (
images: SandboxCustomImageDTOApi[] | null,
payload?: any
) => {
images: SandboxCustomImageDTOApi[] | null
payload?: any
}
startNewImage: () => {
value: true
}
updateDraft: (changes: Partial<CloudImageDraft>) => {
changes: Partial<CloudImageDraft>
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cloudImagesLogicMeta {
__keaTypeGenInternalSelectorTypes: {
readyImages: (images: SandboxCustomImageDTOApi[] | null) => SandboxCustomImageDTOApi[]
draftError: (draft: CloudImageDraft | null) => string | null
}
}
export type cloudImagesLogicType = MakeLogicType<
cloudImagesLogicValues,
cloudImagesLogicActions,
Record<string, any>,
cloudImagesLogicMeta
>
export const cloudImagesLogic = kea<cloudImagesLogicType>([
path(['scenes', 'settings', 'environment', 'cloudImagesLogic']),
connect(() => ({
values: [projectLogic, ['currentProjectId']],
})),
actions({
imagesUnavailable: true,
startNewImage: true,
updateDraft: (changes: Partial<CloudImageDraft>) => ({ changes }),
closeDraft: true,
createImage: true,
createImageDone: true,
deleteImage: (image: SandboxCustomImageDTOApi) => ({ image }),
}),
loaders(({ values, actions }) => ({
images: [
null as SandboxCustomImageDTOApi[] | null,
{
loadImages: async (): Promise<SandboxCustomImageDTOApi[] | null> => {
if (values.currentProjectId == null) {
return null
}
try {
const page = await sandboxCustomImagesList(String(values.currentProjectId))
return page.results.filter((image) => image.status !== 'archived')
} catch (error: any) {
if (error?.status === 403) {
actions.imagesUnavailable()
return null
}
throw error
}
},
},
],
})),
reducers({
unavailable: [false, { imagesUnavailable: () => true }],
imagesFailed: [
false,
{
loadImages: () => false,
loadImagesSuccess: () => false,
loadImagesFailure: () => true,
},
],
draft: [
null as CloudImageDraft | null,
{
startNewImage: () => NEW_IMAGE,
updateDraft: (draft, { changes }) => (draft ? { ...draft, ...changes } : draft),
closeDraft: () => null,
},
],
draftSaving: [false, { createImage: () => true, createImageDone: () => false, closeDraft: () => false }],
}),
selectors({
readyImages: [
(s) => [s.images],
(images: SandboxCustomImageDTOApi[] | null): SandboxCustomImageDTOApi[] =>
(images ?? []).filter((image) => image.status === 'ready'),
],
draftError: [
(s) => [s.draft],
(draft: CloudImageDraft | null): string | null => {
if (!draft) {
return null
}
if (!draft.name.trim()) {
return 'Enter a name'
}
if (!draft.description.trim()) {
return 'Describe what the image needs'
}
if (draft.repository.trim() && !/^[\w.-]+\/[\w.-]+$/.test(draft.repository.trim())) {
return 'Enter the repository as org/repo'
}
return null
},
],
}),
listeners(({ actions, values }) => ({
createImage: async () => {
const { draft, currentProjectId } = values
if (!draft || currentProjectId == null || values.draftError) {
actions.createImageDone()
return
}
try {
const image = await sandboxCustomImagesCreate(String(currentProjectId), {
name: draft.name.trim(),
description: draft.description.trim(),
repository: draft.repository.trim(),
private: draft.private,
})
actions.closeDraft()
actions.loadImages()
if (image.builder_task_id) {
lemonToast.success('Image started. The builder agent sets it up in this task.')
router.actions.push(urls.taskDetail(image.builder_task_id))
} else {
lemonToast.success('Image created')
}
} catch (error: any) {
lemonToast.error(error?.detail ?? 'The image was not created. Try again.')
actions.createImageDone()
}
},
deleteImage: async ({ image }) => {
if (values.currentProjectId == null) {
return
}
try {
await sandboxCustomImagesDestroy(String(values.currentProjectId), image.id)
lemonToast.success('Image deleted')
actions.loadImages()
} catch (error: any) {
lemonToast.error(error?.detail ?? 'The image was not deleted. Try again.')
}
},
})),
afterMount(({ actions }) => {
actions.loadImages()
}),
])