import React, { MouseEvent, useCallback } from "react"; import { CollectionSize, Entity, EntityAction, EntityCollection, SelectionController } from "../../types"; import { Checkbox, cn, IconButton, Menu, MenuItem, MoreVertIcon, Skeleton, Tooltip, Typography } from "@firecms/ui"; import { useFireCMSContext, useLargeLayout } from "../../hooks"; /** * * @param entity * @param width * @param frozen * @param isSelected * @param selectionEnabled * @param size * @param toggleEntitySelection * @param hideId * @constructor * * @group Collection components */ export const EntityCollectionRowActions = function EntityCollectionRowActions({ entity, collection, fullPath, width, frozen, isSelected, selectionEnabled, size, highlightEntity, onCollectionChange, unhighlightEntity, actions = [], hideId, selectionController, }: { entity: Entity, collection?: EntityCollection, fullPath?: string, width: number, frozen?: boolean, size: CollectionSize, isSelected?: boolean, selectionEnabled?: boolean, actions?: EntityAction[], hideId?: boolean, onCollectionChange?: () => void, selectionController?: SelectionController; highlightEntity?: (entity: Entity) => void; unhighlightEntity?: (entity: Entity) => void; }) { const largeLayout = useLargeLayout(); const context = useFireCMSContext(); const onCheckedChange = useCallback((checked: boolean) => { selectionController?.toggleEntitySelection(entity); }, [entity, selectionController?.toggleEntitySelection]); const onClick = useCallback((event: MouseEvent) => { event.stopPropagation(); selectionController?.toggleEntitySelection(entity); }, [entity, selectionController?.toggleEntitySelection]); const hasActions = actions.length > 0; const hasCollapsedActions = actions.some(a => a.collapsed || a.collapsed === undefined); const collapsedActions = actions.filter(a => a.collapsed || a.collapsed === undefined); const uncollapsedActions = actions.filter(a => a.collapsed === false); return (
{(hasActions || selectionEnabled) &&
{uncollapsedActions.map((action, index) => ( { event.stopPropagation(); action.onClick({ entity, fullPath, collection, context, selectionController, highlightEntity, unhighlightEntity, onCollectionChange, }); }} size={largeLayout ? "medium" : "small"}> {action.icon} ))} {hasCollapsedActions && }> {collapsedActions.map((action, index) => ( { e.stopPropagation(); action.onClick({ entity, fullPath, collection, context, selectionController, highlightEntity, unhighlightEntity, onCollectionChange, }); }}> {action.icon} {action.name} ))} } {selectionEnabled && }
} {!hideId && size !== "xs" && (
{entity ? { event.stopPropagation(); }} className={"font-mono select-all"} variant={"caption"} color={"secondary"}> {entity.id} : }
)}
); };