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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 33x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x 2x 5x 2x 2x 2x 2x 2x 2x 1x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 5x 5x 15x | import { Button } from "@/ahuora-design-system/ui/button";
import CardGallery, {
FlowsheetCardData,
} from "@/ahuora-design-system/ui/card-gallery";
import { Input } from "@/ahuora-design-system/ui/input";
import { ScrollArea } from "@/ahuora-design-system/ui/scroll-area";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/ahuora-design-system/ui/select";
import { ArrowLeft, Search } from "lucide-react";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { toast } from "sonner";
import { useTemplates, useCreateFromTemplate } from "@/hooks/useTemplate";
import { FlowsheetRead, FlowsheetTemplateTypeEnum } from "@/api/apiStore.gen";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/ahuora-design-system/ui/dropdown-menu";
import { Settings2 } from "lucide-react";
import { useLocalStorage } from "usehooks-ts";
import { useUserInfo } from "@/hooks/useUserInfo.ts";
type TemplateFilter = "all" | "public" | "private";
const TemplateGallery = () => {
const [templateFilter, setTemplateFilter] = useState<TemplateFilter>("all");
const [searchFilter, setSearchFilter] = useState("");
const navigate = useNavigate();
// Get templates with filtering
const { templates, isLoading, error } = useTemplates();
const { data: userInfo } = useUserInfo();
// Get the createFromTemplate function from the hook
const { createFromTemplate, isLoading: isCreating } = useCreateFromTemplate();
// Sort order state
const [sortOrder, setSortOrder] = useLocalStorage(
"template-sort-order",
"Recently Added"
);
const handleCreateFromTemplate = (templateId: number) => {
createFromTemplate(templateId)
.unwrap()
.then((result) => {
toast.success("Project created from template successfully!");
navigate(`/project/${result.id}/flowsheet`);
})
.catch((error) => {
toast.error(
"Failed to create project from template: " +
(error.data?.detail || error.message)
);
});
};
// Filter templates by type if necessary
const filteredTemplates =
templates?.filter((t) => {
if (templateFilter === "all") return true;
Iif (templateFilter === "public")
return (
t.flowsheet_template_type === FlowsheetTemplateTypeEnum.PublicTemplate
);
Iif (templateFilter === "private")
return (
t.flowsheet_template_type ===
FlowsheetTemplateTypeEnum.PrivateTemplate
);
return true;
}) || [];
const templatesData = filteredTemplates.map((template: FlowsheetRead) => {
const timestamp = new Date(template.savedDate!).getTime();
const displayDate = new Date(timestamp).toLocaleDateString("en-GB", {
year: "numeric",
month: "short",
day: "numeric",
});
const createdAt = new Date(template.created_at!).getTime();
const isPublic =
template.flowsheet_template_type ===
FlowsheetTemplateTypeEnum.PublicTemplate;
const showEditButton = isPublic ? userInfo?.is_admin : true;
return {
timestamp,
createdAt,
cardData: {
id: template.id,
name: template.name!,
editDate: timestamp,
description: `${displayDate} • ${isPublic ? "Public" : "Private"} Template`,
imgUrl: "/assets/example-flowsheet.png",
onClick: () => handleCreateFromTemplate(template.id),
onEdit: () => navigate(`/project/${template.id}/flowsheet`),
onDelete: undefined,
showCopyButton: false,
showDeleteButton: false,
showEditButton: showEditButton,
},
};
});
// Sort templates based on sort order
const getSortedTemplates = () => {
const sortedData = [...templatesData];
switch (sortOrder) {
case "Recently Added":
sortedData.sort((a, b) => b.createdAt - a.createdAt);
break;
case "Recently Edited":
sortedData.sort((a, b) => b.timestamp - a.timestamp);
break;
case "A-Z":
sortedData.sort((a, b) =>
a.cardData.name.localeCompare(b.cardData.name)
);
break;
default:
break;
}
return sortedData;
};
const sortedTemplates = getSortedTemplates();
// Filter by search text
const searchedTemplates = sortedTemplates
.map((item) => item.cardData)
.filter((template) =>
searchFilter.length > 1
? template.name.toLowerCase().includes(searchFilter.toLowerCase())
: true
);
const dropDownData = ["Recently Added", "Recently Edited", "A-Z"];
return (
<ScrollArea className="w-full h-full">
<section className="w-full h-full flex flex-col px-[15%] py-5">
<h1 className="text-2xl pt-12">Kia ora</h1>
<div className="sticky flex top-0 justify-between w-full bg-background py-5">
<div className="flex gap-3">
<Button
onClick={() => navigate("/")}
className="flex items-center gap-2"
>
<ArrowLeft size={16} />
Back to Projects
</Button>
<span className="text-sm text-muted-foreground flex items-center">
Click on a template to create a new project from it
</span>
</div>
<div className="flex gap-3">
<Input
divClassName="w-[300px]"
startIcon={Search}
type="text"
placeholder="Search templates"
value={searchFilter}
onChange={(e) => setSearchFilter(e.target.value)}
/>
<Select
value={templateFilter}
onValueChange={(value: TemplateFilter) =>
setTemplateFilter(value)
}
>
<SelectTrigger className="w-fit">
<SelectValue placeholder="All templates" />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">All Templates</SelectItem>
<SelectItem value="public">Public Templates</SelectItem>
<SelectItem value="private">Private Templates</SelectItem>
</SelectContent>
</Select>
<DropdownMenu>
<DropdownMenuTrigger asChild aria-label="template-sort">
<Button variant="outline" size="icon">
<Settings2 />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuLabel>Sort by </DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuRadioGroup
value={sortOrder}
onValueChange={(value) => {
setSortOrder(value);
}}
>
{dropDownData.map((sortData) => (
<DropdownMenuRadioItem key={sortData} value={sortData}>
<span className="capitalize">{sortData}</span>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
<div className="">
{isLoading ? (
<p className="italic text-foreground ml-2">Loading templates...</p>
) : error ? (
<p className="text-destructive ml-2">Failed to load templates</p>
) : searchedTemplates.length === 0 ? (
<p className="italic text-foreground ml-2">No templates found</p>
) : (
<CardGallery data={searchedTemplates} />
)}
{isCreating && (
<p className="italic text-foreground ml-2">
Creating flowsheet from template...
</p>
)}
</div>
</section>
</ScrollArea>
);
};
export default TemplateGallery;
|