import { Route, createBrowserRouter, createRoutesFromElements, RouterProvider, redirect, } from "react-router-dom"; import MainLayout from "./MainLayout"; import Home from "./Home"; import DocsLayout from "./DocsLayout"; import ErrorBoundary from "./ErrorHandler"; import DynamicLayout from "./DynamicLayout"; import { Code } from "@/components/LoadedCode"; import DocsIntroduction, { tableOfContents as docsIntroductionToc, } from "./docs/introduction.mdx"; import DocsInstallation, { tableOfContents as docsInstallationToc, } from "./docs/installation.mdx"; import DocsConfiguration, { tableOfContents as docsConfigurationToc, } from "./docs/configuration.mdx"; import { HeadingContext } from "./HeadingContext"; import React, { ForwardedRef, forwardRef, useContext, useEffect, useRef, useState, } from "react"; import { Tooltip, TooltipContent } from "@pswui/Tooltip"; function buildThresholdList() { const thresholds: number[] = []; const numSteps = 20; for (let i = 1.0; i <= numSteps; i++) { const ratio = i / numSteps; thresholds.push(ratio); } thresholds.push(0); return thresholds; } function HashedHeaders(Level: `h${1 | 2 | 3 | 4 | 5 | 6}`) { return (prop: any, ref: ForwardedRef) => { const internalRef = useRef(null); const [_, setActiveHeadings] = useContext(HeadingContext); const { children, ...restProp } = prop; useEffect(() => { const observer = new IntersectionObserver( ([{ target, intersectionRatio }]) => { if (intersectionRatio > 0.5) { setActiveHeadings((prev) => [...prev, target.id]); } else { setActiveHeadings((prev) => prev.filter((id) => id !== target.id)); } }, { root: null, rootMargin: "0px", threshold: buildThresholdList(), }, ); if (internalRef.current) { observer.observe(internalRef.current); } return () => { observer.disconnect(); }; }, [internalRef.current]); const [status, setStatus] = useState<"normal" | "error" | "success">( "normal", ); const messages = { normal: "Click to copy link", success: "Copied link!", error: "Failed to copy..", }; useEffect(() => { if (status !== "normal") { const timeout = setTimeout(() => { setStatus("normal"); }, 3000); return () => { clearTimeout(timeout); }; } }, [status]); return ( { internalRef.current = el; if (typeof ref === "function") { ref(el); } else if (el && ref) { ref.current = el; } }} className={`${prop.className} cursor-pointer select-none`} onClick={async (e) => { try { await navigator.clipboard.writeText( window.location.href.split("#")[0] + "#" + e.currentTarget.id, ); setStatus("success"); } catch (e) { setStatus("error"); } }} {...restProp} > {children}

{messages[status]}

); }; } const overrideComponents = { pre: (props: any) => { const { props: { children, className }, } = React.cloneElement(React.Children.only(props.children)); const language = (!className || !className.includes("language-") ? "typescript" : /language-([a-z]+)/.exec(className)![1]) ?? "typescript"; return {children as string}; }, code: forwardRef((props: any, ref) => ( )), table: forwardRef((props: any, ref) => (
)), h1: forwardRef(HashedHeaders("h1")), h2: forwardRef(HashedHeaders("h2")), h3: forwardRef(HashedHeaders("h3")), h4: forwardRef(HashedHeaders("h4")), h5: forwardRef(HashedHeaders("h5")), h6: forwardRef(HashedHeaders("h6")), }; const docsModules = import.meta.glob("./docs/components/*.mdx"); const routes = Object.keys(docsModules).map((path) => { const sfPath = path.split("/").pop()?.replace(".mdx", ""); return ( { const { default: C, tableOfContents } = await import( `./docs/components/${sfPath}.mdx` ); return { Component: () => ( ), }; }} /> ); }); const REDIRECTED_404 = /^\?(\/([a-zA-Z0-9\-_]+\/?)+)(&.*)*$/; const router = createBrowserRouter( createRoutesFromElements( } errorElement={}> REDIRECTED_404.test(window.location.search) ? redirect(REDIRECTED_404.exec(window.location.search)?.[1] ?? "/") : true } element={} /> }> redirect("/docs/introduction")} /> } /> } /> } /> redirect( `/docs/components/${Object.keys(docsModules)[0] .split("/") .pop() ?.replace(".mdx", "")}`, ) } /> {routes} , ), ); function App() { return ; } export default App;