|
| 1 | +import { DownloadSimple } from "@phosphor-icons/react"; |
| 2 | +import { |
| 3 | + SESSION_SERVICE, |
| 4 | + type SessionService, |
| 5 | +} from "@posthog/core/sessions/sessionService"; |
| 6 | +import { useService } from "@posthog/di/react"; |
| 7 | +import { Button } from "@posthog/quill"; |
| 8 | +import type { TaskRunArtifact } from "@posthog/shared"; |
| 9 | +import { isTerminalStatus, type Task } from "@posthog/shared/domain-types"; |
| 10 | +import { |
| 11 | + getAuthIdentity, |
| 12 | + useAuthStateValue, |
| 13 | +} from "@posthog/ui/features/auth/store"; |
| 14 | +import { useSessionSelector } from "@posthog/ui/features/sessions/sessionStore"; |
| 15 | +import { FileIcon } from "@posthog/ui/primitives/FileIcon"; |
| 16 | +import { toast } from "@posthog/ui/primitives/toast"; |
| 17 | +import { Box, Flex, Text } from "@radix-ui/themes"; |
| 18 | +import { useQuery } from "@tanstack/react-query"; |
| 19 | +import { useCallback, useMemo, useState } from "react"; |
| 20 | + |
| 21 | +function formatFileSize(size: number | undefined): string | null { |
| 22 | + if (size === undefined) return null; |
| 23 | + if (size < 1_000) return `${size} B`; |
| 24 | + if (size < 1_000_000) return `${Math.round(size / 1_000)} KB`; |
| 25 | + return `${(size / 1_000_000).toFixed(1)} MB`; |
| 26 | +} |
| 27 | + |
| 28 | +export function CloudArtifactDownloads({ |
| 29 | + taskId, |
| 30 | + task, |
| 31 | +}: { |
| 32 | + taskId: string | undefined; |
| 33 | + task: Task | undefined; |
| 34 | +}) { |
| 35 | + const sessionService = useService<SessionService>(SESSION_SERVICE); |
| 36 | + const sessionArtifacts = useSessionSelector( |
| 37 | + taskId, |
| 38 | + (session) => session?.cloudArtifacts, |
| 39 | + ); |
| 40 | + const cloudStatus = useSessionSelector( |
| 41 | + taskId, |
| 42 | + (session) => session?.cloudStatus, |
| 43 | + ); |
| 44 | + const authIdentity = useAuthStateValue(getAuthIdentity); |
| 45 | + const [downloadingId, setDownloadingId] = useState<string | null>(null); |
| 46 | + const runId = task?.latest_run?.id; |
| 47 | + const { data: fetchedArtifacts } = useQuery({ |
| 48 | + queryKey: ["cloudRunArtifacts", authIdentity, taskId, runId], |
| 49 | + queryFn: () => |
| 50 | + sessionService.getCloudRunArtifacts(taskId ?? "", runId ?? ""), |
| 51 | + enabled: |
| 52 | + authIdentity !== null && |
| 53 | + taskId !== undefined && |
| 54 | + runId !== undefined && |
| 55 | + isTerminalStatus(cloudStatus ?? task?.latest_run?.status), |
| 56 | + retry: false, |
| 57 | + staleTime: Infinity, |
| 58 | + }); |
| 59 | + const artifacts = useMemo( |
| 60 | + () => |
| 61 | + ( |
| 62 | + fetchedArtifacts ?? |
| 63 | + sessionArtifacts ?? |
| 64 | + task?.latest_run?.artifacts ?? |
| 65 | + [] |
| 66 | + ).filter((artifact) => artifact.type === "output"), |
| 67 | + [fetchedArtifacts, sessionArtifacts, task?.latest_run?.artifacts], |
| 68 | + ); |
| 69 | + |
| 70 | + const downloadArtifact = useCallback( |
| 71 | + async (artifact: TaskRunArtifact): Promise<void> => { |
| 72 | + if (!taskId || !runId || !artifact.id) return; |
| 73 | + setDownloadingId(artifact.id); |
| 74 | + try { |
| 75 | + const url = await sessionService.getCloudAttachmentPreviewUrl( |
| 76 | + taskId, |
| 77 | + runId, |
| 78 | + artifact.id, |
| 79 | + ); |
| 80 | + if (!url) { |
| 81 | + toast.error("This file is no longer available"); |
| 82 | + return; |
| 83 | + } |
| 84 | + window.open(url, "_blank", "noopener,noreferrer"); |
| 85 | + } catch { |
| 86 | + toast.error("Couldn't download file"); |
| 87 | + } finally { |
| 88 | + setDownloadingId(null); |
| 89 | + } |
| 90 | + }, |
| 91 | + [runId, sessionService, taskId], |
| 92 | + ); |
| 93 | + |
| 94 | + if (!runId || artifacts.length === 0) return null; |
| 95 | + |
| 96 | + return ( |
| 97 | + <Box className="mb-3 rounded-lg border border-gray-4 bg-gray-2 p-3"> |
| 98 | + <Text className="mb-2 block font-medium text-[13px]">Files</Text> |
| 99 | + <Flex direction="column" gap="1"> |
| 100 | + {artifacts.map((artifact) => { |
| 101 | + const size = formatFileSize(artifact.size); |
| 102 | + const canDownload = Boolean(artifact.id); |
| 103 | + return ( |
| 104 | + <Flex |
| 105 | + key={artifact.id ?? artifact.storage_path ?? artifact.name} |
| 106 | + align="center" |
| 107 | + justify="between" |
| 108 | + gap="3" |
| 109 | + className="min-w-0 rounded-md bg-background px-2 py-1.5" |
| 110 | + > |
| 111 | + <Flex align="center" gap="2" className="min-w-0"> |
| 112 | + <FileIcon filename={artifact.name} size={16} /> |
| 113 | + <Text className="truncate text-[13px]">{artifact.name}</Text> |
| 114 | + {size !== null && ( |
| 115 | + <Text color="gray" className="shrink-0 text-[12px]"> |
| 116 | + {size} |
| 117 | + </Text> |
| 118 | + )} |
| 119 | + </Flex> |
| 120 | + <Button |
| 121 | + size="sm" |
| 122 | + variant="outline" |
| 123 | + disabled={!canDownload || downloadingId === artifact.id} |
| 124 | + onClick={() => void downloadArtifact(artifact)} |
| 125 | + > |
| 126 | + <DownloadSimple size={14} /> |
| 127 | + {downloadingId === artifact.id ? "Opening..." : "Download"} |
| 128 | + </Button> |
| 129 | + </Flex> |
| 130 | + ); |
| 131 | + })} |
| 132 | + </Flex> |
| 133 | + </Box> |
| 134 | + ); |
| 135 | +} |
0 commit comments