All files / src/apps/home/pages/docs DocArticle.tsx

72.41% Statements 42/58
77.96% Branches 46/59
66.66% Functions 16/24
78.72% Lines 37/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                                  2x             53x                     8x 8x 50x 50x   8x     2x       2x       181x                             152x                   44x                                                                                                         2x 26x 26x 26x 26x   26x   26x     181x 136x       26x   26x 26x 26x 136x 136x 26x         26x                       327x 26x   26x       26x                                     26x                                                                                             181x                                                                                                                     133x 133x                                                
import { DocNode } from "@/components/docs/DocTree/DocTree";
import { usePageMeta } from "@/contexts/PageContext";
import { APP_ROUTES } from "@/routes/const";
import { ArrowLeft, ArrowRight, ChevronRight } from "lucide-react";
import React, { useEffect, useMemo, useState } from "react";
import { Link, useLocation } from "react-router-dom";
import {
  AppPath,
  Callout,
  CodeBlock,
  ConceptCards,
  DiagramCard,
  DocTable,
  Rich,
  StepList,
  slugify,
} from "./components/DocBlocks";
import { docsContent } from "./docsContent";
import type { DocSection } from "./docsTypes";
import { docsData } from "./docsData";
import { Reveal } from "@/components/reveal";
import { ACCENTS } from "@/components/theme/accents";
 
// ─── Category accents ───────────────────────────────────────────────────────
 
const CATEGORIES = [
  { prefix: "/docs/getting-started", color: ACCENTS.blue, label: "Getting Started" },
  { prefix: "/docs/guides", color: ACCENTS.violet, label: "Platform Guide" },
  { prefix: "/docs/reference", color: ACCENTS.cyan, label: "Reference" },
];
 
function getCategory(path: string) {
  return CATEGORIES.find((c) => path.startsWith(c.prefix)) ?? CATEGORIES[0];
}
 
// ─── Flattened doc order (for prev/next navigation) ────────────────────────
 
interface FlatDoc {
  title: string;
  url: string;
}
 
function flattenAll(nodes: DocNode[]): FlatDoc[] {
  const out: FlatDoc[] = [];
  for (const n of nodes) {
    out.push({ title: n.title, url: n.url });
    if (n.children?.length) out.push(...flattenAll(n.children));
  }
  return out;
}
 
const FLAT_DOCS = flattenAll(docsData);
 
// ─── Section renderer ───────────────────────────────────────────────────────
 
const DocSectionView: React.FC<{ section: DocSection; accent: string }> = ({
  section,
  accent,
}) => (
  <section>
    {section.heading && (
      <h2
        id={slugify(section.heading)}
        className="text-xl font-bold mb-3 scroll-mt-24"
      >
        {section.heading}
      </h2>
    )}
    {section.appPath && (
      <div className="mb-3">
        <AppPath segments={section.appPath} accent={accent} />
      </div>
    )}
    {section.paragraphs?.map((p, j) => (
      <p
        key={j}
        className="text-gray-600 dark:text-gray-300 leading-relaxed mb-3 last:mb-0"
      >
        <Rich text={p} />
      </p>
    ))}
    {section.list && (
      <ul className="list-disc pl-6 space-y-1.5 text-gray-600 dark:text-gray-300 mb-3 last:mb-0">
        {section.list.map((item, k) => (
          <li key={k}>
            <Rich text={item} />
          </li>
        ))}
      </ul>
    )}
    {section.cards && (
      <div className="mt-4">
        <ConceptCards cards={section.cards} accent={accent} />
      </div>
    )}
    {section.callout && (
      <div className="mt-4">
        <Callout
          tone={section.callout.tone}
          title={section.callout.title}
          text={section.callout.text}
        />
      </div>
    )}
    {section.steps && (
      <div className="mt-2">
        <StepList steps={section.steps} accent={accent} />
      </div>
    )}
    {section.table && (
      <div className="mt-4">
        <DocTable
          headers={section.table.headers}
          rows={section.table.rows}
          plainFirstColumn={section.table.plainFirstColumn}
        />
      </div>
    )}
    {section.diagram && (
      <div className="mt-4">
        <DiagramCard
          diagram={section.diagram}
          accent={accent}
          caption={section.diagramCaption}
        />
      </div>
    )}
    {section.code && (
      <div className="mt-4">
        <CodeBlock code={section.code} label={section.codeLabel} />
      </div>
    )}
  </section>
);
 
// ─── Page ───────────────────────────────────────────────────────────────────
 
const DocArticle: React.FC = () => {
  const location = useLocation();
  const path = location.pathname.replace(/\/+$/, "");
  const doc = docsContent[path];
  const category = getCategory(path);
 
  usePageMeta(doc ? doc.title : "Doc Not Found", false);
 
  const headings = useMemo(
    () =>
      (doc?.sections ?? [])
        .filter((s): s is DocSection & { heading: string } => !!s.heading)
        .map((s) => ({ text: s.heading, id: slugify(s.heading) })),
    [doc],
  );
 
  const [activeHeading, setActiveHeading] = useState<string | null>(null);
 
  useEffect(() => {
    Iif (headings.length === 0) return;
    const els = headings
      .map((h) => document.getElementById(h.id))
      .filter((el): el is HTMLElement => !!el);
    Iif (els.length === 0) return;
    // No IntersectionObserver (jsdom, older browsers): the table of contents
    // still renders and links, it just does not track the reader's position.
    // The same guard `useReveal` carries — a browser without it would
    // otherwise throw on every article.
    Eif (typeof IntersectionObserver === "undefined") return;
    const observer = new IntersectionObserver(
      (entries) => {
        const visible = entries.filter((e) => e.isIntersecting);
        if (visible.length > 0) setActiveHeading(visible[0].target.id);
      },
      { rootMargin: "-88px 0px -70% 0px", threshold: 0 },
    );
    els.forEach((el) => observer.observe(el));
    return () => observer.disconnect();
  }, [path, headings]);
 
  const flatIndex = FLAT_DOCS.findIndex((d) => d.url === path);
  const prev = flatIndex > 0 ? FLAT_DOCS[flatIndex - 1] : undefined;
  const next =
    flatIndex >= 0 && flatIndex < FLAT_DOCS.length - 1
      ? FLAT_DOCS[flatIndex + 1]
      : undefined;
 
  Iif (!doc) {
    return (
      <div className="pt-8 pb-16">
        <h1 className="text-2xl font-bold mb-4">Doc Not Found</h1>
        <p className="text-gray-500 dark:text-gray-400 mb-6">
          We couldn't find the page you were looking for. It may have moved
          or no longer exists.
        </p>
        <Link
          to={APP_ROUTES.HOME.docs}
          className="text-sm font-semibold"
          style={{ color: ACCENTS.blue }}
        >
          ← Back to Docs
        </Link>
      </div>
    );
  }
 
  return (
    <div className="pt-8 pb-20">
      {/* Breadcrumb */}
      <div className="flex items-center gap-1.5 text-xs font-semibold text-gray-400 mb-5">
        <Link
          to={APP_ROUTES.HOME.docs}
          className="hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
        >
          Docs
        </Link>
        <ChevronRight className="size-3" />
        <span style={{ color: category.color }}>{category.label}</span>
      </div>
 
      <div className="flex gap-12 items-start">
        {/* Main content */}
        <div className="flex-1 min-w-0 max-w-3xl">
          <h1
            className="text-3xl md:text-4xl font-black mb-3 leading-tight break-words"
            style={{
              background: `linear-gradient(135deg, ${category.color} 0%, #06b6d4 140%)`,
              WebkitBackgroundClip: "text",
              WebkitTextFillColor: "transparent",
              backgroundClip: "text",
            }}
          >
            {doc.title}
          </h1>
          <p className="text-base md:text-lg text-gray-500 dark:text-gray-400 mb-8 leading-relaxed">
            {doc.description}
          </p>
 
          {doc.image && (
            <div
              className="rounded-2xl overflow-hidden mb-10 border"
              style={{ borderColor: `${category.color}22` }}
            >
              <img
                src={doc.image.src}
                alt={doc.image.alt}
                className="w-full h-48 md:h-64 object-cover"
              />
            </div>
          )}
 
          <div className="flex flex-col gap-10">
            {doc.sections.map((section, i) => (
              <Reveal size="sm" key={i} delay={Math.min(i, 4) * 0.05}>
                <DocSectionView section={section} accent={category.color} />
              </Reveal>
            ))}
          </div>
 
          {/* Prev / Next */}
          {(prev || next) && (
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-16 pt-8 border-t border-gray-200 dark:border-gray-800">
              {prev ? (
                <Link
                  to={prev.url}
                  className="group rounded-xl p-4 border border-gray-200 dark:border-gray-700 transition-colors"
                  onMouseEnter={(e) => {
                    e.currentTarget.style.borderColor = `${category.color}55`;
                  }}
                  onMouseLeave={(e) => {
                    e.currentTarget.style.borderColor = "";
                  }}
                >
                  <div className="flex items-center gap-1.5 text-xs text-gray-400 mb-1">
                    <ArrowLeft className="size-3" /> Previous
                  </div>
                  <div className="font-semibold text-sm">{prev.title}</div>
                </Link>
              ) : (
                <div />
              )}
              {next ? (
                <Link
                  to={next.url}
                  className="group rounded-xl p-4 border border-gray-200 dark:border-gray-700 text-right transition-colors"
                  onMouseEnter={(e) => {
                    e.currentTarget.style.borderColor = `${category.color}55`;
                  }}
                  onMouseLeave={(e) => {
                    e.currentTarget.style.borderColor = "";
                  }}
                >
                  <div className="flex items-center justify-end gap-1.5 text-xs text-gray-400 mb-1">
                    Next <ArrowRight className="size-3" />
                  </div>
                  <div className="font-semibold text-sm">{next.title}</div>
                </Link>
              ) : (
                <div />
              )}
            </div>
          )}
        </div>
 
        {/* On this page */}
        {headings.length > 1 && (
          <aside className="hidden xl:block w-56 shrink-0 sticky top-8">
            <p className="text-xs font-semibold uppercase tracking-wide text-gray-400 mb-3">
              On this page
            </p>
            <nav className="flex flex-col gap-2.5 border-l border-gray-200 dark:border-gray-700">
              {headings.map((h) => {
                const active = activeHeading === h.id;
                return (
                  <a
                    key={h.id}
                    href={`#${h.id}`}
                    className="text-sm pl-3 -ml-px border-l-2 leading-snug transition-colors"
                    style={{
                      borderColor: active ? category.color : "transparent",
                      color: active ? category.color : undefined,
                      fontWeight: active ? 600 : 500,
                    }}
                  >
                    {h.text}
                  </a>
                );
              })}
            </nav>
          </aside>
        )}
      </div>
    </div>
  );
};
 
export default DocArticle;