import { useState } from "react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { useShowArchived } from "@/components/utils/useShowArchived";
import { useTranslation } from "@/lib/i18n/client";
import {
DoorOpen,
FolderInput,
Pencil,
Plus,
Share,
Square,
SquareCheck,
Trash2,
Users,
} from "lucide-react";
import { ZBookmarkList } from "@karakeep/shared/types/lists";
import { EditListModal } from "../lists/EditListModal";
import DeleteListConfirmationDialog from "./DeleteListConfirmationDialog";
import LeaveListConfirmationDialog from "./LeaveListConfirmationDialog";
import { ManageCollaboratorsModal } from "./ManageCollaboratorsModal";
import { MergeListModal } from "./MergeListModal";
import { ShareListModal } from "./ShareListModal";
export function ListOptions({
list,
isOpen,
onOpenChange,
children,
}: {
isOpen?: boolean;
onOpenChange?: (open: boolean) => void;
list: ZBookmarkList;
children?: React.ReactNode;
}) {
const { t } = useTranslation();
const { showArchived, onClickShowArchived } = useShowArchived();
const [deleteListDialogOpen, setDeleteListDialogOpen] = useState(false);
const [leaveListDialogOpen, setLeaveListDialogOpen] = useState(false);
const [newNestedListModalOpen, setNewNestedListModalOpen] = useState(false);
const [mergeListModalOpen, setMergeListModalOpen] = useState(false);
const [editModalOpen, setEditModalOpen] = useState(false);
const [shareModalOpen, setShareModalOpen] = useState(false);
const [collaboratorsModalOpen, setCollaboratorsModalOpen] = useState(false);
// Only owners can manage the list (edit, delete, manage collaborators, etc.)
const isOwner = list.userRole === "owner";
// Collaborators (non-owners) can leave the list
const isCollaborator =
list.userRole === "editor" || list.userRole === "viewer";
// Define action items array
const actionItems = [
{
id: "edit",
title: t("actions.edit"),
icon: ,
visible: isOwner,
disabled: false,
onClick: () => setEditModalOpen(true),
},
{
id: "share",
title: t("lists.share_list"),
icon: ,
visible: isOwner,
disabled: false,
onClick: () => setShareModalOpen(true),
},
{
id: "manage-collaborators",
title: isOwner
? t("lists.collaborators.manage")
: t("lists.collaborators.view"),
icon: ,
visible: list.type === "manual",
disabled: false,
onClick: () => setCollaboratorsModalOpen(true),
},
{
id: "new-nested-list",
title: t("lists.new_nested_list"),
icon: ,
visible: isOwner,
disabled: false,
onClick: () => setNewNestedListModalOpen(true),
},
{
id: "merge-list",
title: t("lists.merge_list"),
icon: ,
visible: isOwner,
disabled: false,
onClick: () => setMergeListModalOpen(true),
},
{
id: "toggle-archived",
title: t("actions.toggle_show_archived"),
icon: showArchived ? (
) : (
),
visible: isOwner,
disabled: false,
onClick: onClickShowArchived,
},
{
id: "leave-list",
title: t("lists.leave_list.action"),
icon: ,
visible: isCollaborator,
disabled: false,
className: "flex gap-2 text-destructive",
onClick: () => setLeaveListDialogOpen(true),
},
{
id: "delete",
title: t("actions.delete"),
icon: ,
visible: isOwner,
disabled: false,
className: "flex gap-2 text-destructive",
onClick: () => setDeleteListDialogOpen(true),
},
];
// Filter visible items
const visibleItems = actionItems.filter((item) => item.visible);
// If no items are visible, don't render the dropdown
if (visibleItems.length === 0) {
return null;
}
return (
{children}
{visibleItems.map((item) => (
{item.icon}
{item.title}
))}
);
}