import React from "react"; import { useState, useEffect } from "@wordpress/element"; import { __, sprintf } from "@wordpress/i18n"; import { decodeEntities } from "@wordpress/html-entities"; import { Sparkle, IconDoc } from "../../sample-docs/icons"; import { AiBadge, GenerateBtn, PopulatedDocs } from "../../sample-docs/Pieces"; import SampleDocsFlow from "../../sample-docs/SampleDocsFlow"; import { sampleDocsEnabled, fetchExistingDocs } from "../../sample-docs/api"; /* Quick Setup "Create Content" field — replaces the old DocCreate.gif. Leads with the AI promo banner ("Skip the blank page"). If the site already has docs/categories (or sample docs were generated this session), it shows them read-only in the All Docs grid instead of the empty prompt. Renders inside the tab content area only; tab header + left nav untouched. */ /* Persist the generated docs for the wizard session so leaving the page (Dashboard / All Docs) and returning keeps the populated view. */ const LS_GEN_KEY = "betterdocs_setup_generated_docs"; const slimCats = (cats) => (cats || []).map((c, i) => ({ id: c.id || "sd_" + i, name: decodeEntities(c.name || ""), icon: c.icon || "book", color: c.color || "#00B884", articles: (c.articles || []).map((a) => decodeEntities(typeof a === "string" ? a : a?.title || "")), })); const readStoredDocs = () => { try { const s = window.localStorage?.getItem(LS_GEN_KEY); const parsed = s ? JSON.parse(s) : null; return Array.isArray(parsed) && parsed.length ? parsed : null; } catch (e) { return null; } }; function SampleDocsGenerator(props) { const [flowOpen, setFlowOpen] = useState(false); const [generated, setGenerated] = useState(readStoredDocs); const [existing, setExisting] = useState(null); const aiEnabled = sampleDocsEnabled(); // Detect content the site already has, so the step reflects reality // instead of always prompting "No categories yet". useEffect(() => { if (generated) return; // a fresh generation takes precedence let alive = true; fetchExistingDocs().then((cats) => { if (alive && cats && cats.length) { setExisting(slimCats(cats)); } }); return () => { alive = false; }; }, []); if (!aiEnabled) { return null; } // What to display in the grid: a fresh generation wins, else existing content. const docs = generated || existing; const totalDocs = docs ? docs.reduce((n, c) => n + (c.articles?.length || 0), 0) : 0; return (
{__("BetterDocs can read your site and generate a tailored starter knowledge base — categories and draft articles — in about a minute.", "betterdocs")}