import LinkCard from "@/components/LinkCard"; import SortLinkDropdown from "@/components/SortLinkDropdown"; import MainLayout from "@/layouts/MainLayout"; import useLinkStore from "@/store/links"; import { Sort } from "@/types/global"; import { faLink, 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(Sort.NameAZ); const [sortedLinks, setSortedLinks] = useState(links); const handleSortChange = (e: Sort) => { setSortBy(e); }; useEffect(() => { const linksArray = [...links]; if (sortBy === Sort.NameAZ) setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name))); else if (sortBy === Sort.TitleAZ) setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title))); else if (sortBy === Sort.NameZA) setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name))); else if (sortBy === Sort.TitleZA) setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title))); else if (sortBy === Sort.DateNewestFirst) setSortedLinks( linksArray.sort( (a, b) => new Date(b.createdAt as string).getTime() - new Date(a.createdAt as string).getTime() ) ); else if (sortBy === Sort.DateOldestFirst) setSortedLinks( linksArray.sort( (a, b) => new Date(a.createdAt as string).getTime() - new Date(b.createdAt as string).getTime() ) ); }, [links, sortBy]); return (

All Links

setSortDropdown(!sortDropdown)} id="sort-dropdown" className="inline-flex rounded-md cursor-pointer hover:bg-slate-200 duration-100 p-1" >
{sortDropdown ? ( setSortDropdown(!sortDropdown)} /> ) : null}
{sortedLinks.map((e, i) => { return ; })}
); }