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 249 250 251 252 | 33x 33x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 26x 26x 154x 1x 154x 154x 71x 71x 71x 2x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 462x 71x 71x | import { Button } from "@/ahuora-design-system/ui/button";
import CardGallery, {
FlowsheetCardData,
} from "@/ahuora-design-system/ui/card-gallery";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/ahuora-design-system/ui/dropdown-menu";
import { Input } from "@/ahuora-design-system/ui/input";
import { ScrollArea } from "@/ahuora-design-system/ui/scroll-area";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/ahuora-design-system/ui/select";
import {
FlowsheetRead,
useCoreFlowsheetsCreateMutation,
useCoreFlowsheetsDestroyMutation,
useCoreFlowsheetsListQuery
} from "@/api/apiStore.gen";
import { Plus, Search, Settings2, Package, LogOut } from "lucide-react";
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { toast } from "sonner";
import { useLocalStorage } from "usehooks-ts";
import { useUndoRedoStore } from "@/hooks/useUndoRedoStore";
import {useUserInfo} from "@/hooks/useUserInfo.ts";
type FlowsheetsData = {
timestamp: number; // used to sort the flowsheets
flowsheetCardData: FlowsheetCardData;
createdAt: number; // used to sort the flowsheets by creation date
};
const logoutUrl = import.meta.env.VITE_SIGN_OUT_URL;
const MainPageContent = () => {
const [type, setType] = useState("all");
const { data: flowsheets } = useCoreFlowsheetsListQuery({ type: type });
const { data: user } = useUserInfo();
const [searchFilter, setSearchFilter] = useState("");
const [deleteFlowsheetMutation] = useCoreFlowsheetsDestroyMutation();
const { removeFlowsheet: removeUndoRedoHistoryForFlowsheet } =
useUndoRedoStore();
const [sortOrder, setSortOrder] = useLocalStorage(
"flowsheet-sort-order",
"Recently Edited"
);
const nav = useNavigate();
const [createFlowsheet] = useCoreFlowsheetsCreateMutation();
const StartFromScratchHandler = async () => {
await createFlowsheet({
flowsheet: {},
})
.unwrap()
.then((flowsheet: FlowsheetRead) => {
nav(`/project/${flowsheet.id}/flowsheet`);
});
};
const StartFromTemplateHandler = async () => {
nav("/templates");
};
const LogOutHandler = () =>{
window.location.replace(logoutUrl)
};
const flowsheetsData: FlowsheetsData[] = (flowsheets ?? []).map(
(flowsheet: FlowsheetRead) => {
const timestamp = new Date(flowsheet.savedDate!).getTime();
const displayDate = new Date(timestamp).toLocaleDateString("en-GB", {
year: "numeric",
month: "short",
day: "numeric",
});
return {
timestamp: timestamp,
createdAt: new Date(flowsheet.created_at!).getTime(),
flowsheetCardData: {
id: flowsheet.id,
name: flowsheet.name!,
editDate: timestamp,
description: displayDate,
imgUrl: "/assets/example-flowsheet.png",
onClick: () => {
nav(`project/${flowsheet.id.toString()}/flowsheet`);
},
onDelete: () => {
deleteFlowsheetMutation({
id: flowsheet.id.toString(),
})
.unwrap()
.then(() => {
toast.success("Flowsheet deleted");
removeUndoRedoHistoryForFlowsheet(flowsheet.id.toString());
})
.catch((error) => {
toast.error(
"Failed to delete flowsheet: " +
(error.data?.detail || error.message)
);
});
},
},
};
}
);
const handleSearchFilterChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchFilter(e.target.value);
};
const dropDownData = ["Recently Added", "Recently Edited", "A-Z"];
const getSortedFlowsheets = () => {
const sortedData = [...flowsheetsData];
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.flowsheetCardData.name.localeCompare(b.flowsheetCardData.name)
);
break;
default:
break;
}
return sortedData;
};
const sortedFlowsheetsData = getSortedFlowsheets();
return (
<ScrollArea className="w-full h-full">
<section className="w-full h-full flex flex-col px-[15%] py-5">
<div className="justify-between flex pt-10">
<h1 className="text-2xl pt-2">
Kia ora{user?.name ? `, ${user.name}` : ""}!
</h1>
<Button
variant="outline"
onClick={LogOutHandler}
>
<LogOut />
Sign out
</Button>
</div>
<div className="sticky flex top-0 justify-between w-full bg-background py-5">
<div className="flex gap-3">
<Button onClick={StartFromScratchHandler}>
<Plus />
Create a new Project
</Button>
<Button variant="outline" onClick={StartFromTemplateHandler}>
<Package />
Browse Templates
</Button>
</div>
<div className="flex gap-3">
<Input
divClassName="w-[300px]"
startIcon={Search}
type="text"
placeholder="Search your projects"
onChange={handleSearchFilterChange}
/>
<Select onValueChange={setType} defaultValue="all">
<SelectTrigger className="w-fit" defaultValue="all">
<SelectValue placeholder="All flowsheets" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="all">All flowsheets</SelectItem>
<SelectItem value="shared">Shared flowsheets</SelectItem>
<SelectItem value="owned">Owned flowsheets</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
<DropdownMenu>
<DropdownMenuTrigger asChild aria-label="main-content-sort">
<Button variant="outline" size="icon">
<Settings2 />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="">
<DropdownMenuLabel>Sort by </DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuRadioGroup
value={sortOrder} // Ensure you're binding the correct value here.
onValueChange={(value) => {
setSortOrder(value);
}}
>
{dropDownData?.map((sortData, index) => (
<DropdownMenuRadioItem
key={sortData}
value={sortData}
className=""
>
<span className="capitalize">{sortData}</span>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
<div className="">
{flowsheetsData.length === 0 ? (
<p className="italic text-foreground ml-2">No current projects</p>
) : (
<CardGallery
data={sortedFlowsheetsData
.map((flowsheet) => flowsheet.flowsheetCardData)
.filter((flowsheet) =>
searchFilter.length > 1
? flowsheet.name
.toLocaleLowerCase()
.includes(searchFilter.toLocaleLowerCase())
: true
)}
/>
)}
</div>
</section>
</ScrollArea>
);
};
export default MainPageContent;
|