Skip to content

feat: add template export functionality to UI #18214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,31 @@ class ApiMethods {
return response.data;
};

/**
* Downloads a template version as a tar or zip archive
* @param fileId The file ID from the template version's job
* @param format Optional format: "zip" for zip archive, empty/undefined for tar
* @returns Promise that resolves to a Blob containing the archive
*/
downloadTemplateVersion = async (
fileId: string,
format?: "zip",
): Promise<Blob> => {
const params = new URLSearchParams();
if (format) {
params.set("format", format);
}

const response = await this.axios.get(
`/api/v2/files/${fileId}?${params.toString()}`,
{
responseType: "blob",
},
);

return response.data;
};

updateTemplateMeta = async (
templateId: string,
data: TypesGen.UpdateTemplateMeta,
Expand Down
34 changes: 33 additions & 1 deletion site/src/pages/TemplatePage/TemplatePageHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EditIcon from "@mui/icons-material/EditOutlined";
import Button from "@mui/material/Button";
import { API } from "api/api";
import { workspaces } from "api/queries/workspaces";
import type {
AuthorizationResponse,
Expand All @@ -26,7 +27,7 @@ import {
} from "components/PageHeader/PageHeader";
import { Pill } from "components/Pill/Pill";
import { Stack } from "components/Stack/Stack";
import { CopyIcon } from "lucide-react";
import { CopyIcon, DownloadIcon } from "lucide-react";
import {
EllipsisVertical,
PlusIcon,
Expand All @@ -46,6 +47,7 @@ type TemplateMenuProps = {
templateName: string;
templateVersion: string;
templateId: string;
fileId: string;
onDelete: () => void;
};

Expand All @@ -54,6 +56,7 @@ const TemplateMenu: FC<TemplateMenuProps> = ({
templateName,
templateVersion,
templateId,
fileId,
onDelete,
}) => {
const dialogState = useDeletionDialogState(templateId, onDelete);
Expand All @@ -68,6 +71,24 @@ const TemplateMenu: FC<TemplateMenuProps> = ({

const templateLink = getLink(linkToTemplate(organizationName, templateName));

const handleExport = async (format?: "zip") => {
try {
const blob = await API.downloadTemplateVersion(fileId, format);
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
const extension = format === "zip" ? "zip" : "tar";
link.download = `${templateName}-${templateVersion}.${extension}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
} catch (error) {
console.error("Failed to export template:", error);
// TODO: Show user-friendly error message
}
};

return (
<>
<DropdownMenu>
Expand Down Expand Up @@ -102,6 +123,16 @@ const TemplateMenu: FC<TemplateMenuProps> = ({
<CopyIcon className="size-icon-sm" />
Duplicate&hellip;
</DropdownMenuItem>

<DropdownMenuItem onClick={() => handleExport()}>
<DownloadIcon className="size-icon-sm" />
Export as TAR
</DropdownMenuItem>

<DropdownMenuItem onClick={() => handleExport("zip")}>
<DownloadIcon className="size-icon-sm" />
Export as ZIP
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
className="text-content-destructive focus:text-content-destructive"
Expand Down Expand Up @@ -206,6 +237,7 @@ export const TemplatePageHeader: FC<TemplatePageHeaderProps> = ({
templateId={template.id}
templateName={template.name}
templateVersion={activeVersion.name}
fileId={activeVersion.job.file_id}
onDelete={onDeleteTemplate}
/>
)}
Expand Down
Loading