This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add dependency graph viewer to live code preview
- Loading branch information
Showing
20 changed files
with
538 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-unforget/babel-plugin": patch | ||
--- | ||
|
||
Expose mermaid dependency graph generator API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client"; | ||
|
||
import { useEffect, useRef, useState } from "react"; | ||
|
||
interface DependencyGraphViewerProps { | ||
mermaidSyntax: string; | ||
} | ||
|
||
function createNodeFromHTML(svgString: string) { | ||
var div = document.createElement("div"); | ||
div.innerHTML = svgString.trim(); | ||
|
||
return div.firstElementChild as SVGElement; | ||
} | ||
|
||
type MermaidDefault = (typeof import("mermaid"))["default"]; | ||
type SvgPanZoomDefault = typeof import("svg-pan-zoom"); | ||
|
||
const DependencyGraphViewer = ({ | ||
mermaidSyntax, | ||
}: DependencyGraphViewerProps) => { | ||
const container = useRef<HTMLDivElement>(null); | ||
|
||
const [modules, setModules] = useState< | ||
[MermaidDefault, SvgPanZoomDefault] | null | ||
>(null); | ||
|
||
useEffect(() => { | ||
Promise.all([import("mermaid"), import("svg-pan-zoom")]).then( | ||
([mermaidModule, svgPanZoomModule]) => { | ||
mermaidModule.default.initialize({ | ||
startOnLoad: true, | ||
theme: "dark", | ||
}); | ||
|
||
setModules([mermaidModule.default, svgPanZoomModule.default]); | ||
}, | ||
); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (mermaidSyntax && container.current && modules) { | ||
const [mermaid, svgPanZoom] = modules; | ||
(async () => { | ||
const { svg } = await mermaid.render("mermaidGraph", mermaidSyntax); | ||
|
||
while (container.current.hasChildNodes()) { | ||
container.current.removeChild(container.current.lastChild); | ||
} | ||
|
||
const svgNode = createNodeFromHTML(svg); | ||
svgNode.setAttribute("height", "100%"); | ||
container.current.appendChild(svgNode); | ||
|
||
svgPanZoom(svgNode, { | ||
controlIconsEnabled: true, | ||
}); | ||
})(); | ||
} | ||
}, [mermaidSyntax, modules]); | ||
|
||
return ( | ||
<div> | ||
<p className="mb-6 text-sm"> | ||
Below is a visual representation of the dependency graph that React | ||
Unforget's compiler sees to ultimately optimize your code. | ||
</p> | ||
<div ref={container} className="w-full h-[600px]" /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DependencyGraphViewer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import dynamic from "next/dynamic"; | ||
|
||
const DynamicDependencyGraphViewer = dynamic( | ||
() => import("./DependencyGraphViewer"), | ||
{ | ||
loading: () => ( | ||
<div className="flex items-center justify-center h-[300px]"> | ||
<span className="loading loading-spinner loading-lg" /> | ||
</div> | ||
), | ||
}, | ||
); | ||
|
||
export { DynamicDependencyGraphViewer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use client"; | ||
import React from "react"; | ||
import { | ||
TextRevealCard, | ||
TextRevealCardDescription, | ||
TextRevealCardTitle, | ||
} from "./ui/text-reveal-card"; | ||
import { encode } from "html-entities"; | ||
|
||
const oldCode = `const UglyComponent = memo(() => { | ||
const data = useData(); | ||
const filteredData = useMemo(() => { | ||
const items = []; | ||
for (let i = 0; i < 1000000000; i++) { | ||
items.push(data[i]); | ||
} | ||
}, [data]); | ||
const someComplexJsx = useMemo(() => ( | ||
<> | ||
<DependentComponent1 data={filteredData} /> | ||
{/* ... */} | ||
</> | ||
), [dependency1, dependency2, filteredData]); | ||
return <div>{someComplexJsx}</div>; | ||
}); | ||
`; | ||
|
||
/* | ||
const NiceComponent = () => { | ||
const data = useData(); | ||
const filteredData = []; | ||
for (let i = 0; i < 1000000000; i++) { | ||
filteredData.push(data[i]); | ||
} | ||
return ( | ||
<div> | ||
<DependentComponent1 data={filteredData} /> | ||
{/* ... *} | ||
</div> | ||
); | ||
} | ||
*/ | ||
const newCode = `<span class="text-blue-400">const</span> NiceComponent = () => { | ||
<span class="text-blue-400">const</span> data = <span class="text-pink-400">useData</span>(); | ||
<span class="text-blue-400">const</span> filteredData = []; | ||
<span class="text-green-400">for</span> (<span class="text-blue-400">let</span> i = <span class="text-yellow-400">0</span>; i < <span class="text-yellow-400">1000000000</span>; i++) { | ||
filteredData.<span class="text-purple-400">push</span>(data[i]); | ||
} | ||
<span class="text-green-400">return</span> ( | ||
<span class="text-red-400"><div></span> | ||
<span class="text-red-400"><DependentComponent1</span> <span class="text-orange-400">data=</span><span class="text-yellow-400">{filteredData}</span> <span class="text-red-400">/></span> | ||
<span class="text-gray-400">{/* ... */}</span> | ||
<span class="text-red-400"></div></span> | ||
); | ||
}\n\n | ||
`; | ||
|
||
export const OldAndNewCodeReveal = () => { | ||
return ( | ||
<div className="flex items-center justify-center my-10 rounded-2xl w-full"> | ||
<TextRevealCard text={encode(oldCode)} revealText={newCode}> | ||
<TextRevealCardTitle> | ||
You don't need all that clutter! | ||
</TextRevealCardTitle> | ||
<TextRevealCardDescription> | ||
👋 Wave goodbye to clutter! Shed the excess and use React Unforget | ||
instead. | ||
</TextRevealCardDescription> | ||
</TextRevealCard> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.