import React, { useRef, useEffect } from "react";
import { __ } from "@wordpress/i18n";
import { applyFilters } from "@wordpress/hooks";
import { decodeEntities } from "@wordpress/html-entities";
import ConnectorColumn from "./ConnectorColumn";
// Right-pointing chevron from Claude design — rotates 90° when expanded
const ChevronIcon = () => (
);
const GripIcon = () => (
);
const PlusIcon = () => (
);
const EditIcon = () => (
);
// Delete glyph shared with the AI Chatbot Logs row actions, so the control
// reads the same in every admin table. Drawn on a 24 viewBox at 16px
// (effective stroke ~1.33px) rather than the 13px/1.3 glyphs beside it.
const TrashIcon = () => (
);
const TreeRow = ({
rowData,
isSelected,
isHighlighted,
onSelect,
onToggle,
onEdit,
onAddChild,
onDelete,
dragOver,
onDragStart,
onDragOver,
onDragLeave,
onDrop,
}) => {
const { node, depth, isLast, parentLastChain, hasChildren, isOpen } = rowData;
const rowRef = useRef(null);
const dropZone = dragOver?.id === node.id ? dragOver.pos : null;
const childCount = node.children?.length || 0;
// Bring a freshly created row into view after the page jumps to it.
useEffect(() => {
if (isHighlighted && rowRef.current) {
rowRef.current.scrollIntoView({ block: "nearest", behavior: "smooth" });
}
}, [isHighlighted]);
return (
onDragStart(e, node)}
onDragOver={(e) => onDragOver(e, node, rowRef)}
onDragLeave={onDragLeave}
onDrop={(e) => onDrop(e, node)}
data-id={node.id}
>
{/* Grip */}
{/* Checkbox */}
onSelect(node.id)}
aria-label={__("Select", "betterdocs")}
/>
{/* Connector lines */}
{/* Chevron expand/collapse — always green when node has children */}
{hasChildren ? (
) : (
)}
{/* Category icon thumbnail */}
{node.thumbnail && (
)}
{/* Name & meta */}
onToggle(node.id)}
role="button"
tabIndex={0}
onKeyDown={(e) => e.key === "Enter" && onToggle(node.id)}
>
{decodeEntities(node.name)}
{node.slug && (
/{node.slug}
)}
{hasChildren && childCount > 0 && (
· {childCount}{" "}
{childCount === 1
? __("child", "betterdocs")
: __("children", "betterdocs")}
)}
{node.description
? decodeEntities(node.description)
: __("No description", "betterdocs")}
{/* Extra table column cell slot (e.g. Pro Knowledge Base column). */}
{applyFilters("betterdocs_doc_category_table_row_column", null, node)}
{/* Count badge */}
{node.count}
{/* Row actions — icon-only buttons, visible on row hover */}
);
};
export default TreeRow;