|
| 1 | +import { useMutation, useQuery } from "@tanstack/react-query" |
| 2 | +import { getTemplateVersions, updateActiveTemplateVersion } from "api/api" |
| 3 | +import { getErrorMessage } from "api/errors" |
| 4 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" |
| 5 | +import { displayError, displaySuccess } from "components/GlobalSnackbar/utils" |
| 6 | +import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout" |
| 7 | +import { VersionsTable } from "components/VersionsTable/VersionsTable" |
| 8 | +import { useState } from "react" |
| 9 | +import { Helmet } from "react-helmet-async" |
| 10 | +import { getTemplatePageTitle } from "../utils" |
| 11 | +import { useDashboard } from "components/Dashboard/DashboardProvider" |
| 12 | + |
| 13 | +const TemplateVersionsPage = () => { |
| 14 | + const dashboard = useDashboard() |
| 15 | + const { template, permissions } = useTemplateLayoutContext() |
| 16 | + const { data } = useQuery({ |
| 17 | + queryKey: ["template", "versions", template.id], |
| 18 | + queryFn: () => getTemplateVersions(template.id), |
| 19 | + }) |
| 20 | + // We use this to update the active version in the UI without having to refetch the template |
| 21 | + const [latestActiveVersion, setLatestActiveVersion] = useState( |
| 22 | + template.active_version_id, |
| 23 | + ) |
| 24 | + const { mutate: promoteVersion, isLoading: isPromoting } = useMutation({ |
| 25 | + mutationFn: (templateVersionId: string) => { |
| 26 | + return updateActiveTemplateVersion(template.id, { |
| 27 | + id: templateVersionId, |
| 28 | + }) |
| 29 | + }, |
| 30 | + onSuccess: async () => { |
| 31 | + setLatestActiveVersion(selectedVersionIdToPromote as string) |
| 32 | + setSelectedVersionIdToPromote(undefined) |
| 33 | + displaySuccess("Version promoted successfully") |
| 34 | + }, |
| 35 | + onError: (error) => { |
| 36 | + displayError(getErrorMessage(error, "Failed to promote version")) |
| 37 | + }, |
| 38 | + }) |
| 39 | + const [selectedVersionIdToPromote, setSelectedVersionIdToPromote] = useState< |
| 40 | + string | undefined |
| 41 | + >() |
| 42 | + const canPromoteVersion = |
| 43 | + dashboard.experiments.includes("template_editor") && |
| 44 | + permissions.canUpdateTemplate |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <Helmet> |
| 49 | + <title>{getTemplatePageTitle("Versions", template)}</title> |
| 50 | + </Helmet> |
| 51 | + <VersionsTable |
| 52 | + versions={data} |
| 53 | + onPromoteClick={ |
| 54 | + canPromoteVersion ? setSelectedVersionIdToPromote : undefined |
| 55 | + } |
| 56 | + activeVersionId={latestActiveVersion} |
| 57 | + /> |
| 58 | + <ConfirmDialog |
| 59 | + type="info" |
| 60 | + hideCancel={false} |
| 61 | + open={selectedVersionIdToPromote !== undefined} |
| 62 | + onConfirm={() => { |
| 63 | + promoteVersion(selectedVersionIdToPromote as string) |
| 64 | + }} |
| 65 | + onClose={() => setSelectedVersionIdToPromote(undefined)} |
| 66 | + title="Promote version" |
| 67 | + confirmLoading={isPromoting} |
| 68 | + confirmText="Promote" |
| 69 | + description="Are you sure you want to promote this version? Workspaces will be prompted to “Update” to this version once promoted." |
| 70 | + /> |
| 71 | + </> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +export default TemplateVersionsPage |
0 commit comments