Skip to content

Commit

Permalink
Implemented a download function for the circuitJson download button. (#…
Browse files Browse the repository at this point in the history
…49)

* Implemented circuitJson download button and function

* fixed type error

* passed down circuitJson to download componant

* fixed bun.locb conflict

* applied requested changes

* formated

---------

Co-authored-by: Your Name <you@example.com>
  • Loading branch information
Abse2001 and Your Name authored Oct 11, 2024
1 parent 4f9539d commit 6a340a8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
Empty file modified bun.lockb
100755 → 100644
Empty file.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@tscircuit/3d-viewer": "^0.0.32",
"@tscircuit/footprinter": "^0.0.68",
"@tscircuit/pcb-viewer": "^1.10.5",
"@types/file-saver": "^2.0.7",
"@types/ms": "^0.7.34",
"@typescript/ata": "^0.9.7",
"@valtown/codemirror-ts": "^2.2.0",
Expand All @@ -66,6 +67,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
1 change: 1 addition & 0 deletions src/components/CodeAndPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function CodeAndPreview({ snippet }: Props) {
return (
<div className="flex flex-col">
<EditorNav
circuitJson={circuitJson}
snippet={snippet}
code={code}
isSaving={updateSnippetMutation.isLoading}
Expand Down
30 changes: 21 additions & 9 deletions src/components/DownloadButtonAndMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ import {
} from "@/components/ui/dropdown-menu"
import { Button } from "@/components/ui/button"
import { Download, ChevronDown } from "lucide-react"
import { downloadCircuitJson } from "@/lib/download-fns/download-circuit-json-fn"

export function DownloadButtonAndMenu({ className }: { className?: string }) {
interface DownloadButtonAndMenuProps {
className?: string
snippetUnscopedName: string | undefined
circuitJson: string
}

export function DownloadButtonAndMenu({
className,
snippetUnscopedName,
circuitJson,
}: DownloadButtonAndMenuProps) {
return (
<div className={className}>
<DropdownMenu>
Expand All @@ -20,14 +31,15 @@ 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"
onSelect={() =>
downloadCircuitJson(
circuitJson,
snippetUnscopedName || "circuit" + ".json",
)
}
>
<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
9 changes: 8 additions & 1 deletion src/components/EditorNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,6 +35,7 @@ import { TypeBadge } from "./TypeBadge"
import { SnippetLink } from "./SnippetLink"

export default function EditorNav({
circuitJson,
snippet,
code,
hasUnsavedChanges,
Expand All @@ -42,6 +44,7 @@ export default function EditorNav({
onSave,
isSaving,
}: {
circuitJson: any
snippet: Snippet
code: string
hasUnsavedChanges: boolean
Expand Down Expand Up @@ -111,7 +114,11 @@ export default function EditorNav({
<Sparkles className="mr-1 h-3 w-3" />
Edit with AI
</Button>
<DownloadButtonAndMenu className="hidden md:flex" />
<DownloadButtonAndMenu
snippetUnscopedName={snippet.unscoped_name}
circuitJson={circuitJson}
className="hidden md:flex"
/>
<Button
variant="ghost"
size="sm"
Expand Down
4 changes: 4 additions & 0 deletions src/lib/download-fns/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)
}
7 changes: 7 additions & 0 deletions src/lib/download-fns/download-circuit-json-fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { saveAs } from "file-saver"
import { createBlobURL } from "./createBlobURL"
export const downloadCircuitJson = (content: any, fileName: string) => {
const circuitJson = JSON.stringify(content, null, 2)
const blob = new Blob([circuitJson], { type: "application/json" })
saveAs(blob, fileName)
}
6 changes: 5 additions & 1 deletion src/pages/view-snippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export const ViewSnippetPage = () => {
<Share className="mr-1 h-3 w-3" />
Copy URL
</Button>
<DownloadButtonAndMenu />
<DownloadButtonAndMenu
snippetUnscopedName={snippet?.unscoped_name}
circuitJson={circuitJson}
className="hidden md:flex"
/>
<div className="flex-grow" />
<TabsList>
<TabsTrigger value="code">Code</TabsTrigger>
Expand Down

0 comments on commit 6a340a8

Please sign in to comment.