// Copyright (C) 2022-present Daniel31x13 // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. // You should have received a copy of the GNU General Public License along with this program. If not, see . import ClickAwayHandler from "@/components/ClickAwayHandler"; import LinkList from "@/components/LinkList"; import RadioButton from "@/components/RadioButton"; import MainLayout from "@/layouts/MainLayout"; import useLinkStore from "@/store/links"; import { faBookmark, faSort } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { ChangeEvent, useEffect, useState } from "react"; export default function Links() { const { links } = useLinkStore(); const [sortDropdown, setSortDropdown] = useState(false); const [sortBy, setSortBy] = useState("Name (A-Z)"); const [sortedLinks, setSortedLinks] = useState(links); const handleSortChange = (event: ChangeEvent) => { setSortBy(event.target.value); }; useEffect(() => { const linksArray = [...links]; if (sortBy === "Name (A-Z)") setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name))); else if (sortBy === "Title (A-Z)") setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title))); else if (sortBy === "Name (Z-A)") setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name))); else if (sortBy === "Title (Z-A)") setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title))); else if (sortBy === "Date (Newest First)") setSortedLinks( linksArray.sort( (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() ) ); else if (sortBy === "Date (Oldest First)") setSortedLinks( linksArray.sort( (a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime() ) ); }, [links, sortBy]); return (

All Links

setSortDropdown(!sortDropdown)} id="sort-dropdown" className="inline-flex rounded-md cursor-pointer hover:bg-white hover:border-sky-500 border-sky-100 border duration-100 p-1" >
{sortDropdown ? ( { const target = e.target as HTMLInputElement; if (target.id !== "sort-dropdown") setSortDropdown(false); }} className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48" >

Sort by

) : null}
{sortedLinks.map((e, i) => { return ; })}
); }