el.xwx.moe/components/SortDropdown.tsx

160 lines
5.0 KiB
TypeScript
Raw Normal View History

import React, { Dispatch, SetStateAction, useEffect } from "react";
2023-06-13 21:44:50 -05:00
import { Sort } from "@/types/global";
2024-01-14 09:09:09 -06:00
import { dropdownTriggerer } from "@/lib/client/utils";
import { TFunction } from "i18next";
import useLocalSettingsStore from "@/store/localSettings";
2024-08-16 12:42:55 -05:00
import { resetInfiniteQueryPagination } from "@/hooks/store/links";
import { useQueryClient } from "@tanstack/react-query";
2023-05-28 17:40:28 -05:00
type Props = {
2023-06-13 21:44:50 -05:00
sortBy: Sort;
2023-06-13 23:40:23 -05:00
setSort: Dispatch<SetStateAction<Sort>>;
t: TFunction<"translation", undefined>;
2023-05-28 17:40:28 -05:00
};
export default function SortDropdown({ sortBy, setSort, t }: Props) {
const { updateSettings } = useLocalSettingsStore();
2024-08-16 12:42:55 -05:00
const queryClient = useQueryClient();
useEffect(() => {
updateSettings({ sortBy });
}, [sortBy]);
2023-05-28 17:40:28 -05:00
return (
2023-11-29 14:17:51 -06:00
<div className="dropdown dropdown-bottom dropdown-end">
<div
tabIndex={0}
role="button"
2024-01-14 09:09:09 -06:00
onMouseDown={dropdownTriggerer}
2024-02-10 23:55:00 -06:00
className="btn btn-sm btn-square btn-ghost border-none"
2023-11-29 14:17:51 -06:00
>
2023-12-17 22:32:33 -06:00
<i className="bi-chevron-expand text-neutral text-2xl"></i>
2023-05-28 17:40:28 -05:00
</div>
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-xl mt-1">
2023-11-29 14:17:51 -06:00
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.DateNewestFirst}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.DateNewestFirst);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">
{t("date_newest_first")}
</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.DateOldestFirst}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.DateOldestFirst);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">
{t("date_oldest_first")}
</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.NameAZ}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.NameAZ);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">{t("name_az")}</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.NameZA}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.NameZA);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">{t("name_za")}</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.DescriptionAZ}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.DescriptionAZ);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">
{t("description_az")}
</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
<li>
<label
className="label cursor-pointer flex justify-start"
2023-11-29 14:17:51 -06:00
tabIndex={0}
role="button"
>
<input
type="radio"
name="sort-radio"
className="radio checked:bg-primary"
checked={sortBy === Sort.DescriptionZA}
2024-08-16 12:42:55 -05:00
onChange={() => {
resetInfiniteQueryPagination(queryClient, ["links"]);
setSort(Sort.DescriptionZA);
}}
2023-11-29 14:17:51 -06:00
/>
<span className="label-text whitespace-nowrap">
{t("description_za")}
</span>
2023-11-29 14:17:51 -06:00
</label>
</li>
</ul>
</div>
2023-05-28 17:40:28 -05:00
);
}