-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added nomnoml * began drafting NomnomlPreview.tsx * began adding handleCopy and useEffect based on MermaidPreview component * mirrored imports based on MermaidPreview component * drafted fetchNomnoml() and modeled Card component off of MermaidPreview.tsx * added error handling to catch block * added elif block for handling nomnoml codeblocks * added width autofitting to nomnoml svg * removed nanoid, commented code, console.log statements * added nomnoml to pnpm lock file * updated Instructions blurb * moved try/catch syntax inside fetchNomnoml to properly catch any errors from renderSvg() * updated comments * updated comments * updated default system prompt to include nomnoml responses * added error message to UI for when nomnoml rendering fails * updated error message * updated visual topics rule typo
- Loading branch information
Showing
6 changed files
with
108 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,85 @@ | ||
import { memo, useCallback, useEffect, useRef, type ReactNode } from "react"; | ||
import { Card, CardBody, IconButton, useClipboard } from "@chakra-ui/react"; | ||
import { TbCopy } from "react-icons/tb"; | ||
import { useAlert } from "../hooks/use-alert"; | ||
|
||
type NomnomlPreviewProps = { | ||
children: ReactNode & ReactNode[]; | ||
}; | ||
|
||
const NomnomlPreview = ({ children }: NomnomlPreviewProps) => { | ||
const { onCopy, value, setValue } = useClipboard(""); | ||
const { info } = useAlert(); | ||
const diagramRef = useRef<HTMLDivElement | null>(null); | ||
const code = String(children); | ||
|
||
const handleCopy = useCallback(() => { | ||
onCopy(); | ||
info({ | ||
title: "Copied to Clipboard", | ||
message: "Nomnoml SVG diagram was copied to your clipboard.", | ||
}); | ||
}, [onCopy, info]); | ||
|
||
// Render the diagram as an SVG into our card's body | ||
useEffect(() => { | ||
const diagramDiv = diagramRef.current; | ||
if (!diagramDiv) { | ||
return; | ||
} | ||
|
||
const fetchNomnoml = async () => { | ||
try { | ||
// Load nomnoml dynamically at runtime if needed | ||
const nomnoml = await import("nomnoml"); | ||
const svg = await nomnoml.renderSvg(code); | ||
|
||
setValue(svg); | ||
|
||
// Render nomnoml svg if successful | ||
diagramDiv.innerHTML = svg; | ||
|
||
// Adjust the width of the SVG to fit the content inside the CardBody | ||
const svgElement = diagramDiv.querySelector("svg"); | ||
if (svgElement) { | ||
svgElement.style.width = "100%"; | ||
} | ||
} catch (err: any) { | ||
// When the diagram fails, use the error vs. diagram for copying (to debug) | ||
setValue(err); | ||
|
||
// Render custom error message instead of error or blank content | ||
const errMessage = `Error rendering Nomnoml diagram`; | ||
diagramDiv.innerHTML = errMessage; | ||
console.warn(`Error rendering nomnoml diagram`, err); | ||
} | ||
}; | ||
fetchNomnoml(); | ||
}, [diagramRef, code, setValue]); | ||
|
||
return ( | ||
<Card variant="outline" position="relative" mt={2} minHeight="12em" resize="vertical"> | ||
<IconButton | ||
position="absolute" | ||
right={1} | ||
top={1} | ||
zIndex={50} | ||
aria-label="Copy Diagram to Clipboard" | ||
title="Copy Diagram to Clipboard" | ||
icon={<TbCopy />} | ||
color="gray.600" | ||
_dark={{ color: "gray.300" }} | ||
variant="ghost" | ||
onClick={() => handleCopy()} | ||
isDisabled={!value} | ||
/> | ||
|
||
<CardBody p={2}> | ||
<div ref={diagramRef} /> | ||
</CardBody> | ||
</Card> | ||
); | ||
}; | ||
|
||
// Memoize to reduce re-renders/flickering when content hasn't changed | ||
export default memo(NomnomlPreview); |
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