Skip to content

Commit

Permalink
Merge branch 'main' into fix-frontend-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
agaraman0 authored Aug 4, 2023
2 parents e54cb9a + 7a3264f commit 83a90e7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
26 changes: 26 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-svg": "^16.1.19",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
43 changes: 35 additions & 8 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useState } from 'react';
import React, { useRef, useState } from 'react';
import { renderToString } from 'react-dom/server';
import { ReactSVG } from 'react-svg';
import { Button, TextField, Box, CircularProgress, AppBar, Toolbar, Typography, Container, Grid, Paper, Link, IconButton } from '@mui/material';
import LinkedInIcon from '@mui/icons-material/LinkedIn';
import TwitterIcon from '@mui/icons-material/Twitter';
Expand Down Expand Up @@ -41,17 +43,18 @@ function App() {
const [description, setDescription] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [svgData, setSvgData] = useState(null);
const svgRef = useRef(null);

const handleDesignClick = async () => {
setIsLoading(true);

try {
const response = await axios.post('/call_prompt',
const response = await axios.post('/call_prompt',
{
input: description,
},
{
headers: {"Content-Type": "application/json"},
headers: { "Content-Type": "application/json" },
redirect: 'follow'
}
);
Expand All @@ -66,9 +69,28 @@ function App() {
};

// Creating a Blob from SVG data
const svgBlob = new Blob([svgData], {type: 'image/svg+xml;charset=utf-8'});
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);

const downloadPNG = () => {
const svgString = renderToString(svgRef.current);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();

img.src = `data:image/svg+xml;base64,${btoa(svgString)}`;
img.onload = () => {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const pngUrl = canvas.toDataURL();
const link = document.createElement('a');
link.href = pngUrl;
link.download = 'diagram.png';
link.click();
};
};

return (
<Box
sx={{
Expand All @@ -95,7 +117,7 @@ function App() {
overflow: "auto",
}}
>
{isLoading ? <CircularProgress /> : <div dangerouslySetInnerHTML={{ __html: svgData }} />}
{isLoading ? <CircularProgress /> : <ReactSVG src={svgUrl} ref={svgRef} />}
</Box>
</Grid>

Expand All @@ -121,9 +143,14 @@ function App() {
</Button>

{!isLoading && svgData && (
<Link href={svgUrl} download="diagram.svg" sx={{ mt: 2, display: 'block' }}>
Download Diagram
</Link>
<>
<Link href={svgUrl} download="diagram.svg" sx={{ mt: 2, display: 'block' }}>
Download Diagram (SVG)
</Link>
<Button onClick={downloadPNG} sx={{ mt: 2, display: 'block' }}>
Download Diagram (PNG)
</Button>
</>
)}
</Paper>
</Grid>
Expand Down

0 comments on commit 83a90e7

Please sign in to comment.