web3.news/app/components/SitesCombobox.tsx

85 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-12-18 03:39:09 +00:00
import * as React from "react";
2023-12-08 18:29:17 +00:00
2023-12-18 03:39:09 +00:00
import { Button } from "@/components/ui/button";
2023-12-08 18:29:17 +00:00
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
2023-12-18 03:39:09 +00:00
CommandList,
} from "@/components/ui/command";
2023-12-08 18:29:17 +00:00
import {
Popover,
PopoverContent,
2023-12-18 03:39:09 +00:00
PopoverTrigger,
} from "@/components/ui/popover";
import { ChevronDownIcon } from "@heroicons/react/24/solid";
2023-12-22 09:11:03 +00:00
import { SelectOptions, SiteList } from "@/types.js";
import slugify from "slugify";
2023-12-23 11:42:46 +00:00
import { useEffect } from "react";
2023-12-08 18:29:17 +00:00
2023-12-23 11:42:46 +00:00
export function SitesCombobox({
siteList,
onSiteSelect,
}: {
siteList: SiteList;
onSiteSelect: React.Dispatch<React.SetStateAction<any>>;
}) {
2023-12-22 09:11:03 +00:00
const sites = Object.entries(siteList).map((item) => {
return {
label: item[1].name,
value: slugify(item[0]),
};
});
2023-12-18 03:39:09 +00:00
const [open, setOpen] = React.useState(false);
2023-12-22 09:11:03 +00:00
const [selectedSite, setSelectedSite] = React.useState<SelectOptions | null>(
null
);
2023-12-08 18:29:17 +00:00
2023-12-23 11:42:46 +00:00
useEffect(() => {
onSiteSelect(selectedSite?.value as any);
}, [selectedSite]);
2023-12-08 18:29:17 +00:00
return (
<div className="flex flex- items-center space-x-4">
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
2023-12-18 03:39:09 +00:00
<Button
variant={"ghost"}
className="max-w-[120px] focus:ring-2 focus:ring-ring px-2 font-bold items-center w-full flex justify-between text-white text-xs uppercase"
>
2023-12-22 09:11:03 +00:00
{selectedSite ? <>{selectedSite.label}</> : <>All Sites</>}
2023-12-18 03:39:09 +00:00
<ChevronDownIcon className="ml-3 w-5 h-5" />
2023-12-08 18:29:17 +00:00
</Button>
</PopoverTrigger>
<PopoverContent className="p-0" side="right" align="start">
<Command>
2023-12-22 09:11:03 +00:00
<CommandInput placeholder="Select site..." />
2023-12-08 18:29:17 +00:00
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup>
2023-12-22 09:11:03 +00:00
{sites?.map((status) => (
2023-12-08 18:29:17 +00:00
<CommandItem
className="text-white"
key={status.value}
value={status.value}
onSelect={(value) => {
2023-12-22 09:11:03 +00:00
setSelectedSite(
2023-12-23 11:42:46 +00:00
sites.find((site) => site.value === value) || null
2023-12-18 03:39:09 +00:00
);
setOpen(false);
2023-12-08 18:29:17 +00:00
}}
>
{status.label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
2023-12-18 03:39:09 +00:00
);
2023-12-08 18:29:17 +00:00
}