import React, { useState, useEffect } from "react"; import { __ } from "@wordpress/i18n"; import apiFetch from "@wordpress/api-fetch"; import PostsLoader from './PostsLoader'; const RecentDocs = () => { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchRecentDocs = async () => { try { const response = await apiFetch({ path: "/wp/v2/docs?status=publish,draft,pending,future,private&per_page=5&orderby=date&order=desc" }); setPosts(response); setLoading(false); } catch (err) { setError(err.message); setLoading(false); } }; fetchRecentDocs(); }, []); const getStatusLabel = (status) => { const statusMap = { publish: { label: __('Published', 'betterdocs'), class: 'published' }, future: { label: __('Scheduled', 'betterdocs'), class: 'scheduled' }, draft: { label: __('Drafted', 'betterdocs'), class: 'drafted' }, pending: { label: __('Pending', 'betterdocs'), class: 'pending' }, private: { label: __('Private', 'betterdocs'), class: 'private' } }; return statusMap[status] || { label: status, class: status }; }; const truncateTitle = (title) => { // Check if FAQ component is also displayed const hasFaqs = betterdocs_admin?.count_all_faq > 0; // Decode HTML entities (like ‘ to ') const decodedTitle = document.createElement('textarea'); decodedTitle.innerHTML = title; const cleanTitle = decodedTitle.value; // If no FAQs, display full title without truncation if (!hasFaqs) { return cleanTitle; } // Otherwise, truncate to 35 characters when both components are displayed const maxLength = 35; return cleanTitle.length > maxLength ? cleanTitle.substring(0, maxLength) + '..' : cleanTitle; }; // Don’t render the wrapper if not loading and there are no posts if (!loading && posts.length === 0) { return null; } return (