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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 76x 76x 76x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1038x 1x 1x 1506x 1038x 1x 1x 1506x 1038x 1x 1x 1196x 158x 156x 1350x 156x 1350x 83x 1204x 158x 2144x 1670x | import { defineCommand } from "just-search-it";
import { Globe, Lock, Package, PackageOpen, PackageX } from "lucide-react";
import { toast } from "sonner";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/ahuora-design-system/ui/dropdown-menu";
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);
const canRevert =
templateType === FlowsheetTemplateTypeEnum.PrivateTemplate ||
templateType === FlowsheetTemplateTypeEnum.PublicTemplate;
const showTemplateOptions = canMakePublic || canMakePrivate || canRevert;
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 />}
/>
)}
{canRevert && (
<>
<RegisterCommand
command={RevertToFlowsheetCommand}
args={[]}
name="Revert to Flowsheet"
description="Revert this template back to a regular flowsheet"
group="Export"
action={revertToFlowsheet}
icon={<PackageX />}
/>
</>
)}
{showTemplateOptions && (
<DropdownMenu>
<ToolTipCover asChild content="Template Options">
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="icon"
className="w-fit"
aria-label="Template Options"
>
<Package />
</Button>
</DropdownMenuTrigger>
</ToolTipCover>
<DropdownMenuContent align="end">
{canMakePublic && (
<DropdownMenuItem
onClick={makePublic}
className="flex items-center gap-2"
>
<Globe size={16} />
Convert to Public Template
</DropdownMenuItem>
)}
{canMakePrivate && (
<DropdownMenuItem
onClick={makePrivate}
className="flex items-center gap-2"
>
<Lock size={16} />
Convert to Private Template
</DropdownMenuItem>
)}
{canRevert && (
<DropdownMenuItem
onClick={revertToFlowsheet}
className="flex items-center gap-2"
>
<PackageX size={16} />
Revert to Flowsheet
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
)}
</>
);
}
export default TemplateCommandsProvider;
|