import type { DirectionFilter, SortFilter as TSortFilter } from "@shared/types";
import {
DisclosureIcon,
SortAscendingIcon,
SortDescendingIcon,
} from "outline-icons";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { hover, s } from "@shared/styles";
import FilterOptions from "~/components/FilterOptions";
type Props = {
/** The selected sort field */
sort?: TSortFilter | null;
/** The selected sort direction */
direction?: DirectionFilter | null;
/** Callback when a sort option is selected */
onSelect: (sort: string, direction: string) => void;
};
export const SortInput = ({ sort, direction, onSelect }: Props) => {
const { t } = useTranslation();
const options = useMemo(
() => [
{
key: "relevance-DESC",
label: t("Relevance"),
icon: ,
},
{
key: "updatedAt-DESC",
label: t("Recently updated"),
icon: ,
},
{
key: "updatedAt-ASC",
label: t("Least recently updated"),
icon: ,
},
{
key: "createdAt-DESC",
label: t("Newest"),
icon: ,
},
{
key: "createdAt-ASC",
label: t("Oldest"),
icon: ,
},
{
key: "title-ASC",
label: t("A → Z"),
icon: ,
},
{
key: "title-DESC",
label: t("Z → A"),
icon: ,
},
],
[t]
);
const selectedKey =
sort && direction ? `${sort}-${direction}` : "relevance-DESC";
const handleSelect = (key: string) => {
const [sortField, sortDirection] = key.split("-");
onSelect(sortField, sortDirection);
};
const selectedLabel =
options.find((option) => option.key === selectedKey)?.label ??
t("Relevance");
return (
{t("Sorted by")}
{selectedLabel}
}
/>
);
};
const Wrapper = styled.div`
display: flex;
align-items: center;
gap: 4px;
`;
const Trigger = styled.button`
display: inline-flex;
align-items: center;
margin: 0;
padding: 0 0 0 2px;
border: 0;
border-radius: 4px;
background: none;
font: inherit;
font-weight: 500;
color: ${s("text")};
cursor: var(--pointer);
&:${hover},
&[aria-expanded="true"] {
text-decoration: underline;
}
`;
const Disclosure = styled(DisclosureIcon)`
color: ${s("textTertiary")};
`;