Skip to content
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

Feat: Download JSON button added #26

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"easyeda": "^0.0.32",
"embla-carousel-react": "^8.3.0",
"fflate": "^0.8.2",
"file-saver": "^2.0.5",
"immer": "^10.1.1",
"input-otp": "^1.2.4",
"jose": "^5.9.3",
Expand Down Expand Up @@ -92,6 +93,7 @@
"@tscircuit/prompt-benchmarks": "^0.0.14",
"@types/babel__standalone": "^7.1.7",
"@types/bun": "^1.1.10",
"@types/file-saver": "^2.0.7",
"@types/prismjs": "^1.26.4",
"@types/react": "^18.3.9",
"@types/react-dom": "^18.3.0",
Expand Down
26 changes: 16 additions & 10 deletions src/components/DownloadButtonAndMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from "react"
import {
DropdownMenu,
DropdownMenuTrigger,
Expand All @@ -7,8 +6,22 @@ import {
} from "@/components/ui/dropdown-menu"
import { Button } from "@/components/ui/button"
import { Download, ChevronDown } from "lucide-react"
import { saveAs } from "file-saver"
import { createBlobURL } from "@/lib/createBlobURL"

export function DownloadButtonAndMenu({ className }: { className?: string }) {
export function DownloadButtonAndMenu({
className,
circuitJson,
fileName,
}: {
className?: string
circuitJson?: string
fileName?: string
}) {
const handleDownloadClick = () => {
const blob = createBlobURL(circuitJson as string)
saveAs(blob, `${fileName}.json`)
}
return (
<div className={className}>
<DropdownMenu>
Expand All @@ -20,14 +33,7 @@ export function DownloadButtonAndMenu({ className }: { className?: string }) {
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem className="text-xs">
<Download className="mr-1 h-3 w-3" />
<span className="flex-grow mr-6">Download TSX</span>
<span className="text-[0.6rem] opacity-80 bg-blue-500 text-white font-mono rounded-md px-1 text-center py-0.5 mr-1">
tsx
</span>
</DropdownMenuItem>
<DropdownMenuItem className="text-xs">
<DropdownMenuItem className="text-xs" onClick={handleDownloadClick}>
<Download className="mr-1 h-3 w-3" />
<span className="flex-grow mr-6">Download Circuit JSON</span>
<span className="text-[0.6rem] opacity-80 bg-blue-500 text-white font-mono rounded-md px-1 text-center py-0.5 mr-1">
Expand Down
14 changes: 12 additions & 2 deletions src/components/EditorNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { OpenInNewWindowIcon } from "@radix-ui/react-icons"
import { encodeTextToUrlHash } from "@/lib/encodeTextToUrlHash"
import { Snippet } from "fake-snippets-api/lib/db/schema"
import { useState, useEffect } from "react"
import { useRunTsx } from "@/hooks/use-run-tsx"
import { cn } from "@/lib/utils"
import { DownloadButtonAndMenu } from "./DownloadButtonAndMenu"
import { TypeBadge } from "./TypeBadge"
Expand All @@ -51,6 +51,12 @@ export default function EditorNav({
onSave: () => void
}) {
const [, navigate] = useLocation()

const { circuitJson, message } = useRunTsx(
snippet?.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">
Expand Down Expand Up @@ -111,7 +117,11 @@ export default function EditorNav({
<Sparkles className="mr-1 h-3 w-3" />
Edit with AI
</Button>
<DownloadButtonAndMenu className="hidden md:flex" />
<DownloadButtonAndMenu
className="hidden md:flex"
circuitJson={JSON.stringify(circuitJson)}
fileName={snippet?.name}
/>
<Button
variant="ghost"
size="sm"
Expand Down
4 changes: 4 additions & 0 deletions src/lib/createBlobURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const createBlobURL = (content: string) => {
const blob = new Blob([content], { type: "text/plain" })
return URL.createObjectURL(blob)
}
Loading