"use client";
import {
forwardRef,
useEffect,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
import { createPortal } from "react-dom";
import {
Download,
Eye,
EyeOff,
FolderMinus,
Hash,
History,
Pencil,
Trash2,
Upload,
} from "lucide-react";
import { SubfolderSvgIcon } from "@/app/components/shared/FolderSvgIcon";
import {
CLOSE_ROW_ACTIONS_EVENT,
closeRowActionMenus,
} from "@/app/components/shared/TablePrimitive";
import {
LiquidDropdownButton,
LiquidDropdownSurface,
} from "@/app/components/ui/liquid-dropdown";
import { cn } from "@/app/lib/utils";
export { CLOSE_ROW_ACTIONS_EVENT, closeRowActionMenus };
export type RowActionMenuSurfaceProps = ComponentPropsWithoutRef<"div">;
interface Props {
onDelete?: () => void;
onHide?: () => void;
onUnhide?: () => void;
onDownload?: () => void;
onRemoveFromFolder?: () => void;
onShowAllVersions?: () => void;
onUploadNewVersion?: () => void;
onNewSubfolder?: () => void;
deleting?: boolean;
deleteDisabled?: boolean;
onEditDetails?: () => void;
onRename?: () => void;
onUpdateCmNumber?: () => void;
newSubfolderLabel?: string;
renameLabel?: string;
deleteLabel?: string;
}
type RowActionMenuItemsProps = Props & {
onClose: () => void;
surfaceProps?: RowActionMenuSurfaceProps;
};
const ROW_ACTION_ITEM_CLASS =
"flex items-center gap-2 w-full px-3 py-2 text-gray-600";
const ROW_ACTION_LEFT_ITEM_CLASS = `text-left ${ROW_ACTION_ITEM_CLASS}`;
export const RowActionMenuItems = forwardRef<
HTMLDivElement,
RowActionMenuItemsProps
>(function RowActionMenuItems({
onDelete,
onHide,
onUnhide,
onDownload,
onRemoveFromFolder,
onShowAllVersions,
onUploadNewVersion,
onNewSubfolder,
deleting,
deleteDisabled = false,
onEditDetails,
onRename,
onUpdateCmNumber,
newSubfolderLabel = "New subfolder",
renameLabel = "Rename",
deleteLabel = "Delete",
onClose,
surfaceProps,
}, ref) {
const { className: surfaceClassName, ...restSurfaceProps } =
surfaceProps ?? {};
return (
{onNewSubfolder && (
{ onClose(); onNewSubfolder(); }}
className={ROW_ACTION_LEFT_ITEM_CLASS}
>
{newSubfolderLabel}
)}
{onRename && (
{ onClose(); onRename(); }}
className={ROW_ACTION_ITEM_CLASS}
>
{renameLabel}
)}
{onEditDetails && (
{ onClose(); onEditDetails(); }}
className={ROW_ACTION_ITEM_CLASS}
>
Edit details
)}
{onUpdateCmNumber && (
{ onClose(); onUpdateCmNumber(); }}
className={ROW_ACTION_ITEM_CLASS}
>
Edit CM No.
)}
{onDownload && (
{ onClose(); onDownload(); }}
className={ROW_ACTION_ITEM_CLASS}
>
Download
)}
{onShowAllVersions && (
{ onClose(); onShowAllVersions(); }}
className={ROW_ACTION_LEFT_ITEM_CLASS}
>
Show all versions
)}
{onUploadNewVersion && (
{ onClose(); onUploadNewVersion(); }}
className={ROW_ACTION_LEFT_ITEM_CLASS}
>
Upload new version
)}
{onRemoveFromFolder && (
{ onClose(); onRemoveFromFolder(); }}
className={ROW_ACTION_LEFT_ITEM_CLASS}
>
Remove from subfolder
)}
{onUnhide && (
{ onClose(); onUnhide(); }}
className={ROW_ACTION_ITEM_CLASS}
>
Activate
)}
{onHide && (
{ onClose(); onHide(); }}
className={ROW_ACTION_ITEM_CLASS}
>
Deactivate
)}
{onDelete && (
)}
);
});
export function RowActions(props: Props) {
const [open, setOpen] = useState(false);
const [coords, setCoords] = useState({ top: 0, right: 0 });
const btnRef = useRef(null);
useEffect(() => {
if (!open) return;
function handleClick() {
setOpen(false);
}
document.addEventListener("click", handleClick);
return () => document.removeEventListener("click", handleClick);
}, [open]);
useEffect(() => {
function handleCloseRowActions() {
setOpen(false);
}
document.addEventListener(CLOSE_ROW_ACTIONS_EVENT, handleCloseRowActions);
return () =>
document.removeEventListener(
CLOSE_ROW_ACTIONS_EVENT,
handleCloseRowActions,
);
}, []);
function handleToggle(e: React.MouseEvent) {
e.stopPropagation();
if (open) {
setOpen(false);
return;
}
closeRowActionMenus();
if (btnRef.current) {
const rect = btnRef.current.getBoundingClientRect();
setCoords({
top: rect.bottom + 4,
right: window.innerWidth - rect.right,
});
}
setOpen(true);
}
return (
<>
{open &&
createPortal(
setOpen(false)}
surfaceProps={{
style: {
position: "fixed",
top: coords.top,
right: coords.right,
},
className: "z-[120]",
onClick: (e) => e.stopPropagation(),
}}
/>,
document.body,
)}
>
);
}