|
| 1 | +import { Cube } from "@phosphor-icons/react"; |
| 2 | +import type { Task } from "@posthog/shared/domain-types"; |
| 3 | +import { Badge } from "../../../primitives/Badge"; |
| 4 | +import { Tooltip } from "../../../primitives/Tooltip"; |
| 5 | +import { openSettings } from "../../settings/hooks/useOpenSettings"; |
| 6 | +import { useSandboxCustomImages } from "../../settings/sections/environments/useSandboxCustomImages"; |
| 7 | +import { useSandboxEnvironments } from "../../settings/sections/environments/useSandboxEnvironments"; |
| 8 | + |
| 9 | +export function CustomImageBadge({ task }: { task: Task }) { |
| 10 | + const run = task.latest_run; |
| 11 | + const state = run?.state as |
| 12 | + | { custom_image_id?: unknown; sandbox_environment_id?: unknown } |
| 13 | + | undefined; |
| 14 | + const customImageId = |
| 15 | + typeof state?.custom_image_id === "string" ? state.custom_image_id : null; |
| 16 | + const sandboxEnvironmentId = |
| 17 | + typeof state?.sandbox_environment_id === "string" |
| 18 | + ? state.sandbox_environment_id |
| 19 | + : null; |
| 20 | + |
| 21 | + // Only mount the data-fetching part when the run could have used a custom image. |
| 22 | + if ( |
| 23 | + run?.environment !== "cloud" || |
| 24 | + (!customImageId && !sandboxEnvironmentId) |
| 25 | + ) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + return ( |
| 29 | + <ResolvedCustomImageBadge |
| 30 | + customImageId={customImageId} |
| 31 | + sandboxEnvironmentId={sandboxEnvironmentId} |
| 32 | + /> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function ResolvedCustomImageBadge({ |
| 37 | + customImageId, |
| 38 | + sandboxEnvironmentId, |
| 39 | +}: { |
| 40 | + customImageId: string | null; |
| 41 | + sandboxEnvironmentId: string | null; |
| 42 | +}) { |
| 43 | + const { images } = useSandboxCustomImages(); |
| 44 | + const { environments } = useSandboxEnvironments(); |
| 45 | + |
| 46 | + const environment = sandboxEnvironmentId |
| 47 | + ? environments.find((env) => env.id === sandboxEnvironmentId) |
| 48 | + : undefined; |
| 49 | + const imageId = customImageId ?? environment?.custom_image_id ?? null; |
| 50 | + if (!imageId) return null; |
| 51 | + |
| 52 | + const imageName = |
| 53 | + images.find((image) => image.id === imageId)?.name ?? |
| 54 | + environment?.custom_image_name ?? |
| 55 | + null; |
| 56 | + |
| 57 | + return ( |
| 58 | + <Tooltip |
| 59 | + content={`Runs on custom base image${imageName ? ` "${imageName}"` : ""}. Click to manage custom images.`} |
| 60 | + side="bottom" |
| 61 | + delayDuration={300} |
| 62 | + > |
| 63 | + <button |
| 64 | + type="button" |
| 65 | + className="no-drag flex shrink-0 cursor-pointer items-center" |
| 66 | + aria-label="Manage custom images" |
| 67 | + onClick={() => openSettings("cloud-environments")} |
| 68 | + > |
| 69 | + <Badge color="violet" className="flex items-center gap-1"> |
| 70 | + <Cube size={10} weight="fill" /> |
| 71 | + {imageName ? `Custom VM · ${imageName}` : "Custom VM"} |
| 72 | + </Badge> |
| 73 | + </button> |
| 74 | + </Tooltip> |
| 75 | + ); |
| 76 | +} |
0 commit comments