import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import LoadingSpinner from "@/components/ui/spinner";
import { cn } from "@/lib/utils";
import { Check, ChevronsUpDown } from "lucide-react";
import { useBookmarkLists } from "@karakeep/shared-react/hooks/lists";
import { ZBookmarkList } from "@karakeep/shared/types/lists";
export function BookmarkListSelector({
value,
onChange,
hideSubtreeOf,
hideBookmarkIds = [],
placeholder = "Select a list",
className,
listTypes = ["manual", "smart"],
}: {
className?: string;
value?: string | null;
onChange: (value: string) => void;
placeholder?: string;
hideSubtreeOf?: string;
hideBookmarkIds?: string[];
listTypes?: ZBookmarkList["type"][];
}) {
const [open, setOpen] = useState(false);
const { data, isPending: isFetchingListsPending } = useBookmarkLists();
let { allPaths } = data ?? {};
if (isFetchingListsPending) {
return ;
}
allPaths = allPaths?.filter((path) => {
const lastItem = path[path.length - 1];
if (hideBookmarkIds.includes(lastItem.id)) {
return false;
}
if (!listTypes.includes(lastItem.type)) {
return false;
}
// Hide lists where user is a viewer (can't add/remove bookmarks)
if (lastItem.userRole === "viewer") {
return false;
}
if (!hideSubtreeOf) {
return true;
}
return !path.map((p) => p.id).includes(hideSubtreeOf);
});
// Find the selected list's display name
const selectedListPath = allPaths?.find(
(path) => path[path.length - 1].id === value,
);
const selectedListName = selectedListPath
? selectedListPath.map((p) => `${p.icon} ${p.name}`).join(" / ")
: null;
return (
{allPaths && allPaths.length === 0
? "You don't currently have any lists."
: "No lists found."}
{allPaths?.map((path) => {
const l = path[path.length - 1];
const name = path.map((p) => `${p.icon} ${p.name}`).join(" / ");
return (
{
onChange(currentValue);
setOpen(false);
}}
className="cursor-pointer"
>
{name}
);
})}
);
}