import { useSortable } from '@dnd-kit/sortable'; import { CSS } from '@dnd-kit/utilities'; import { GripVertical } from 'lucide-react'; import React from 'react'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; import { useCanDelete, useCanEdit, useDeletePost, useUpdatePost, } from '../../data/hooks'; import { Post } from '../../types'; import ActionLink from './action-link'; import StatusToggle from './status-toggle'; import { Button } from '@/components/ui/button'; import { StepIndicator } from '@/components/ui/step-indicator'; import { CardTitle } from '@/components/ui/typography'; interface Props { /** * Post object. */ post: Post; /** * Index of the post in the list. */ index: number; /** * Whether sorting is enabled for this row. */ sortable?: boolean; /** * Click handler. */ onClick?: () => void; /** * Edit handler. */ onEdit: () => void; /** * Select handler. If present, a select button will be shown. */ onSelect?: ( post: Post ) => void; } /** * Audience list row component. * * @param {Object} props Component props. * @returns {React.ReactNode} List row component. */ const ListRow = ( props: Props ) => { const { index, post, sortable = false, onClick, onEdit, onSelect, } = props; const isSelectMode = !! onSelect; const isPublished = post.status === 'publish'; const canEdit = useCanEdit( post.id ); const canDelete = useCanDelete( post.id ); const deletePost = useDeletePost(); const updatePost = useUpdatePost(); const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable( { id: post.id, disabled: ! sortable, transition: { duration: 150, easing: 'ease-out', }, } ); const style = { transform: CSS.Transform.toString( transform ), transition, opacity: isDragging ? 0.5 : 1, }; /** * Delete post handler. */ const onDeletePost = () => { // eslint-disable-next-line no-alert if ( window.confirm( __( 'Are you sure you want to delete this audience?', 'altis' ) ) ) { deletePost( post.id ); } }; /** * Audience status change handler. * * @returns {void} */ const onStatusChange = () => updatePost( { id: post.id, status: post.status === 'publish' ? 'draft' : 'publish', } ); const postTitle = decodeEntities( post.title.rendered || __( '(no title)', 'altis' ) ); return (
{ sortable && ( ) }
{ ! canEdit && ( { postTitle } ) } { canEdit && ( { postTitle } ) }
{ canEdit && ( { __( 'Edit' ) } ) } { canDelete && ( <> | { __( 'Trash' ) } ) }
{ canEdit && ( ) } { ! canEdit && ( { post.status === 'publish' ? __( 'Active', 'altis' ) : __( 'Inactive', 'altis' ) } ) }
{ isSelectMode && (
{ isPublished && ( ) }
) }
); }; ListRow.defaultProps = { /** * Default click handler. */ onClick: () => {}, }; export default ListRow;