|
| 1 | +import { Artifact } from "@flanksource-ui/api/types/artifacts"; |
| 2 | +import { |
| 3 | + artifactDownloadURL, |
| 4 | + downloadArtifact |
| 5 | +} from "@flanksource-ui/api/services/artifacts"; |
| 6 | +import { formatBytes } from "@flanksource-ui/utils/common"; |
| 7 | +import { |
| 8 | + Dialog, |
| 9 | + DialogContent, |
| 10 | + DialogFooter, |
| 11 | + DialogHeader, |
| 12 | + DialogTitle |
| 13 | +} from "@flanksource-ui/components/ui/dialog"; |
| 14 | +import { Button } from "@flanksource-ui/ui/Buttons/Button"; |
| 15 | +import CodeBlock from "@flanksource-ui/ui/Code/CodeBlock"; |
| 16 | +import { darkTheme } from "@flanksource-ui/ui/Code/JSONViewerTheme"; |
| 17 | +import { JSONViewer } from "@flanksource-ui/ui/Code/JSONViewer"; |
| 18 | +import { DisplayMarkdown } from "@flanksource-ui/components/Utils/Markdown"; |
| 19 | +import { useQuery } from "@tanstack/react-query"; |
| 20 | +import { IoMdDownload } from "react-icons/io"; |
| 21 | + |
| 22 | +type ArtifactPreviewModalProps = { |
| 23 | + artifact: Artifact | undefined; |
| 24 | + onClose: () => void; |
| 25 | +}; |
| 26 | + |
| 27 | +const MAX_PREVIEW_SIZE = 50 * 1024 * 1024; // 50MB |
| 28 | + |
| 29 | +function isImageContentType(contentType: string) { |
| 30 | + return contentType.startsWith("image/"); |
| 31 | +} |
| 32 | + |
| 33 | +export function ArtifactPreviewModal({ |
| 34 | + artifact, |
| 35 | + onClose |
| 36 | +}: ArtifactPreviewModalProps) { |
| 37 | + const isSmallFile = artifact ? artifact.size < MAX_PREVIEW_SIZE : false; |
| 38 | + |
| 39 | + const isImage = artifact ? isImageContentType(artifact.content_type) : false; |
| 40 | + |
| 41 | + const { data: content, isFetching: isLoading } = useQuery({ |
| 42 | + queryKey: ["artifact", artifact?.id], |
| 43 | + queryFn: () => downloadArtifact(artifact!.id), |
| 44 | + enabled: !!artifact && isSmallFile && !isImage, |
| 45 | + staleTime: Infinity, |
| 46 | + cacheTime: 1000 * 60 * 60 * 24 |
| 47 | + }); |
| 48 | + |
| 49 | + const downloadURL = artifact |
| 50 | + ? artifactDownloadURL(artifact.id, artifact.filename) |
| 51 | + : undefined; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Dialog open={!!artifact} onOpenChange={(open) => !open && onClose()}> |
| 55 | + <DialogContent className="max-h-[80vh] max-w-4xl overflow-hidden"> |
| 56 | + <DialogHeader> |
| 57 | + <DialogTitle className="flex items-center gap-2"> |
| 58 | + <span>{artifact?.filename}</span> |
| 59 | + {artifact && ( |
| 60 | + <span className="text-sm font-normal text-gray-500"> |
| 61 | + ({formatBytes(artifact.size)}) |
| 62 | + </span> |
| 63 | + )} |
| 64 | + </DialogTitle> |
| 65 | + </DialogHeader> |
| 66 | + |
| 67 | + <div className="max-h-[60vh] overflow-auto"> |
| 68 | + {artifact && !isSmallFile && ( |
| 69 | + <div className="py-8 text-center text-gray-500"> |
| 70 | + <p className="mb-4"> |
| 71 | + File is too large to preview. Maximum file size is 50MB. |
| 72 | + </p> |
| 73 | + <a href={downloadURL} target="_blank" rel="noreferrer"> |
| 74 | + <Button text="Download" icon={<IoMdDownload />} /> |
| 75 | + </a> |
| 76 | + </div> |
| 77 | + )} |
| 78 | + |
| 79 | + {isLoading && ( |
| 80 | + <div className="flex items-center justify-center py-8"> |
| 81 | + <div className="h-6 w-6 animate-spin rounded-full border-b-2 border-blue-500" /> |
| 82 | + <span className="ml-2">Loading...</span> |
| 83 | + </div> |
| 84 | + )} |
| 85 | + |
| 86 | + {isImage && artifact && downloadURL && ( |
| 87 | + <img |
| 88 | + src={downloadURL} |
| 89 | + alt={artifact.filename} |
| 90 | + className="max-h-[60vh] max-w-full object-contain" |
| 91 | + /> |
| 92 | + )} |
| 93 | + |
| 94 | + {content && |
| 95 | + artifact && |
| 96 | + !isImage && |
| 97 | + renderContent(artifact.content_type, content)} |
| 98 | + </div> |
| 99 | + |
| 100 | + <DialogFooter className="pt-2"> |
| 101 | + {downloadURL && ( |
| 102 | + <a href={downloadURL} target="_blank" rel="noreferrer"> |
| 103 | + <Button |
| 104 | + text="Download" |
| 105 | + icon={<IoMdDownload />} |
| 106 | + className="btn-secondary" |
| 107 | + /> |
| 108 | + </a> |
| 109 | + )} |
| 110 | + <Button text="Close" onClick={onClose} className="btn-primary" /> |
| 111 | + </DialogFooter> |
| 112 | + </DialogContent> |
| 113 | + </Dialog> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +function renderContent(contentType: string, content: string) { |
| 118 | + switch (contentType) { |
| 119 | + case "text/x-shellscript": |
| 120 | + return ( |
| 121 | + <CodeBlock |
| 122 | + code={content} |
| 123 | + language="bash" |
| 124 | + className="bg-black text-white" |
| 125 | + codeBlockClassName="whitespace-pre" |
| 126 | + theme={darkTheme} |
| 127 | + /> |
| 128 | + ); |
| 129 | + |
| 130 | + case "application/sql": |
| 131 | + return ( |
| 132 | + <CodeBlock |
| 133 | + code={content} |
| 134 | + language="sql" |
| 135 | + className="bg-black text-white" |
| 136 | + codeBlockClassName="whitespace-pre" |
| 137 | + theme={darkTheme} |
| 138 | + /> |
| 139 | + ); |
| 140 | + |
| 141 | + case "text/markdown": |
| 142 | + case "markdown": |
| 143 | + return <DisplayMarkdown md={content} />; |
| 144 | + |
| 145 | + case "application/yaml": |
| 146 | + case "application/json": |
| 147 | + return ( |
| 148 | + <pre> |
| 149 | + <JSONViewer format="json" code={content} convertToYaml /> |
| 150 | + </pre> |
| 151 | + ); |
| 152 | + |
| 153 | + case "text/plain": |
| 154 | + default: |
| 155 | + return ( |
| 156 | + <pre className="whitespace-pre-wrap bg-gray-50 p-4 text-sm"> |
| 157 | + {content} |
| 158 | + </pre> |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments