-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented a download function for the circuitJson download button. #49
Changes from 3 commits
bc709f0
06734ee
64f4398
991d679
227f25a
ab9d968
9edba80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,6 +24,7 @@ import { | |||||
DropdownMenuItem, | ||||||
DropdownMenuTrigger, | ||||||
} from "@/components/ui/dropdown-menu" | ||||||
import { useRunTsx } from "@/hooks/use-run-tsx" | ||||||
import { OpenInNewWindowIcon } from "@radix-ui/react-icons" | ||||||
import { encodeTextToUrlHash } from "@/lib/encodeTextToUrlHash" | ||||||
import { Snippet } from "fake-snippets-api/lib/db/schema" | ||||||
|
@@ -51,6 +52,7 @@ export default function EditorNav({ | |||||
onSave: () => void | ||||||
}) { | ||||||
const [, navigate] = useLocation() | ||||||
const { circuitJson } = useRunTsx(code, snippet?.snippet_type) | ||||||
return ( | ||||||
<nav className="flex items-center justify-between px-2 py-3 border-b border-gray-200 bg-white text-sm border-t"> | ||||||
<div className="flex items-center space-x-1"> | ||||||
|
@@ -111,7 +113,11 @@ export default function EditorNav({ | |||||
<Sparkles className="mr-1 h-3 w-3" /> | ||||||
Edit with AI | ||||||
</Button> | ||||||
<DownloadButtonAndMenu className="hidden md:flex" /> | ||||||
<DownloadButtonAndMenu | ||||||
fileName={snippet.unscoped_name} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. important concept: "variable transarency" means you keep the same variable name as it is passed around. Don't rename variables if you don't have to. When I read this code, i was surprised that fileName was the same as the unscoped name, i expected it to have an extension. Naming is hard, renaming makes you have to do it more often
Suggested change
|
||||||
circuitJson={circuitJson} | ||||||
className="hidden md:flex" | ||||||
/> | ||||||
<Button | ||||||
variant="ghost" | ||||||
size="sm" | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const downloadCircuitJson = (content: string, filename: string) => { | ||
const circuitJson = JSON.stringify(content, null, 2) | ||
const blob = new Blob([circuitJson], { type: "text/plain" }) | ||
const url = URL.createObjectURL(blob) | ||
const a = document.createElement("a") | ||
a.href = url | ||
a.download = filename | ||
document.body.appendChild(a) | ||
a.click() | ||
document.body.removeChild(a) | ||
URL.revokeObjectURL(url) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { CodeEditor } from "@/components/CodeEditor" | ||
import { DownloadButtonAndMenu } from "@/components/DownloadButtonAndMenu" | ||
import Header from "@/components/Header" | ||
import { Button } from "@/components/ui/button" | ||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" | ||
|
@@ -45,7 +44,6 @@ export const ViewSnippetPage = () => { | |
<Share className="mr-1 h-3 w-3" /> | ||
Copy URL | ||
</Button> | ||
<DownloadButtonAndMenu /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove from this page? can't we give it the props? |
||
<div className="flex-grow" /> | ||
<TabsList> | ||
<TabsTrigger value="code">Code</TabsTrigger> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you avoid using
useRunTsx
? It's a very expensive hook and the circuitJson should be computed elsewhere