Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 62x 62x 62x 9028x 9028x 9028x 9028x 9028x 9028x 9028x 9028x 9028x 1x 1x 1x 9028x | import { defineCommand } from "just-search-it";
import { Package, PackageOpen, PackageX } from "lucide-react";
import { toast } from "sonner";
import { FlowsheetTemplateTypeEnum } from "@/api/apiStore.gen";
import { RegisterCommand } from "@/commands/CommandProvider";
import { useProject, useProjectId } from "@/hooks/project";
import { useCreateTemplate, useRevertTemplate } from "@/hooks/useTemplate";
import { useUserInfo } from "@/hooks/useUserInfo";
import { Button } from "../../../ahuora-design-system/ui/button";
import { ToolTipCover } from "../../../ahuora-design-system/ui/tooltip";
export const MakeTemplatePublicCommand = defineCommand<[], void>(
"makeTemplatePublic",
);
export const MakeTemplatePrivateCommand = defineCommand<[], void>(
"makeTemplatePrivate",
);
export const RevertToFlowsheetCommand = defineCommand<[], void>(
"revertToFlowsheet",
);
function getErrorMessage(e: unknown, defaultMessage: string) {
const maybe = e as { data?: { error?: string }; message?: string };
Iif (maybe?.data?.error) return maybe.data.error;
Iif (maybe?.message) return maybe.message;
Iif (typeof e === "string") return e;
return defaultMessage;
}
export function TemplateCommandsProvider() {
const flowsheetId = useProjectId();
const project = useProject();
const { data: userInfo } = useUserInfo();
const { createTemplate } = useCreateTemplate();
const { revertTemplate } = useRevertTemplate();
const templateType = project?.flowsheet_template_type;
const isAdmin = !!userInfo?.is_admin;
const canMakePublic =
(templateType === FlowsheetTemplateTypeEnum.NotTemplate && isAdmin) ||
(templateType === FlowsheetTemplateTypeEnum.PrivateTemplate && isAdmin);
const canMakePrivate =
templateType === FlowsheetTemplateTypeEnum.NotTemplate ||
(templateType === FlowsheetTemplateTypeEnum.PublicTemplate && isAdmin);
async function makePublic() {
Iif (!flowsheetId) return;
if (!canMakePublic) {
toast.error("You don't have permission to make this template public.");
return;
}
try {
await createTemplate(
flowsheetId,
FlowsheetTemplateTypeEnum.PublicTemplate,
);
toast.success("Successfully converted to public template!");
} catch (e) {
toast.error(getErrorMessage(e, "Failed to create public template"));
}
}
async function makePrivate() {
Iif (!flowsheetId) return;
if (!canMakePrivate) {
toast.error("You don't have permission to make this template private.");
return;
}
try {
await createTemplate(
flowsheetId,
FlowsheetTemplateTypeEnum.PrivateTemplate,
);
toast.success("Successfully converted to private template!");
} catch (e) {
toast.error(getErrorMessage(e, "Failed to create private template"));
}
}
async function revertToFlowsheet() {
Iif (!flowsheetId) return;
try {
await revertTemplate(flowsheetId);
toast.success("Successfully reverted to flowsheet!");
} catch (e) {
toast.error(getErrorMessage(e, "Failed to revert to flowsheet"));
}
}
return (
<>
{canMakePublic && (
<RegisterCommand
command={MakeTemplatePublicCommand}
args={[]}
name="Make Public Template"
description="Convert current flowsheet into a public template"
group="Export"
action={makePublic}
icon={<PackageOpen />}
/>
)}
{canMakePrivate && (
<>
<RegisterCommand
command={MakeTemplatePrivateCommand}
args={[]}
name="Make Private Template"
description="Convert current flowsheet into a private template"
group="Export"
action={makePrivate}
icon={<Package />}
/>
<ToolTipCover
asChild
content="Convert to Template (accessible in other flowsheets)"
>
<Button
variant="ghost"
size="icon"
className="w-fit"
onClick={makePrivate}
>
<Package />
</Button>
</ToolTipCover>
</>
)}
{(templateType === FlowsheetTemplateTypeEnum.PrivateTemplate ||
templateType === FlowsheetTemplateTypeEnum.PublicTemplate) && (
<>
<RegisterCommand
command={RevertToFlowsheetCommand}
args={[]}
name="Revert to Flowsheet"
description="Revert this template back to a regular flowsheet"
group="Export"
action={revertToFlowsheet}
icon={<PackageX />}
/>
<ToolTipCover asChild content="Revert to Flowsheet">
<Button
variant="ghost"
size="icon"
className="w-fit"
onClick={revertToFlowsheet}
>
<PackageX />
</Button>
</ToolTipCover>
</>
)}
</>
);
}
export default TemplateCommandsProvider;
|