|
1 |
| -import { useState } from "react" |
| 1 | +import { useState, useCallback } from "react" |
| 2 | +import { PCBViewer } from "@tscircuit/pcb-viewer" |
| 3 | +import { parseDsnToCircuitJson } from "dsn-converter" |
2 | 4 |
|
3 | 5 | function App() {
|
4 |
| - return <h1 className="text-3xl font-bold underline">Hello world!</h1> |
| 6 | + const [circuitJson, setCircuitJson] = useState<any>(null) |
| 7 | + |
| 8 | + const handleDrop = useCallback((e: React.DragEvent) => { |
| 9 | + e.preventDefault() |
| 10 | + const file = e.dataTransfer.files[0] |
| 11 | + if (file) { |
| 12 | + const reader = new FileReader() |
| 13 | + reader.onload = async (e) => { |
| 14 | + const dsnContent = e.target?.result as string |
| 15 | + try { |
| 16 | + const json = parseDsnToCircuitJson(dsnContent) |
| 17 | + setCircuitJson(json) |
| 18 | + } catch (err) { |
| 19 | + console.error("Failed to parse DSN file:", err) |
| 20 | + alert("Failed to parse DSN file. Please check the format.") |
| 21 | + } |
| 22 | + } |
| 23 | + reader.readAsText(file) |
| 24 | + } |
| 25 | + }, []) |
| 26 | + |
| 27 | + const handlePaste = useCallback((e: React.ClipboardEvent) => { |
| 28 | + const dsnContent = e.clipboardData.getData("text") |
| 29 | + if (dsnContent) { |
| 30 | + try { |
| 31 | + const json = parseDsnToCircuitJson(dsnContent) |
| 32 | + setCircuitJson(json) |
| 33 | + } catch (err) { |
| 34 | + console.error("Failed to parse DSN content:", err) |
| 35 | + alert("Failed to parse DSN content. Please check the format.") |
| 36 | + } |
| 37 | + } |
| 38 | + }, []) |
| 39 | + |
| 40 | + return ( |
| 41 | + <div |
| 42 | + className="min-h-screen bg-gray-900 text-white p-4" |
| 43 | + onDrop={handleDrop} |
| 44 | + onDragOver={(e) => e.preventDefault()} |
| 45 | + onPaste={handlePaste} |
| 46 | + tabIndex={0} |
| 47 | + > |
| 48 | + {circuitJson ? ( |
| 49 | + <PCBViewer circuitJson={circuitJson} height={800} /> |
| 50 | + ) : ( |
| 51 | + <div className="flex flex-col items-center justify-center min-h-screen"> |
| 52 | + <div className="text-center"> |
| 53 | + <h1 className="text-3xl font-bold mb-4"> |
| 54 | + Specctra DSN File Viewer |
| 55 | + </h1> |
| 56 | + <p className="text-gray-400 mb-2">Drag and drop a DSN file here</p> |
| 57 | + <p className="text-gray-400"> |
| 58 | + or paste DSN content with Ctrl/CMD+V |
| 59 | + </p> |
| 60 | + </div> |
| 61 | + <div className="text-gray-400 text-sm mt-16"> |
| 62 | + Unofficial Specctra DSN Parser/Viewer created by{" "} |
| 63 | + <a |
| 64 | + className="underline" |
| 65 | + href="https://github.com/tscircuit/tscircuit" |
| 66 | + > |
| 67 | + tscircuit |
| 68 | + </a> |
| 69 | + , get the{" "} |
| 70 | + <a |
| 71 | + className="underline" |
| 72 | + href="https://github.com/tscircuit/dsn-viewer" |
| 73 | + > |
| 74 | + source code here |
| 75 | + </a> |
| 76 | + . |
| 77 | + </div> |
| 78 | + <a className="mt-4" href="https://github.com/tscircuit/tscircuit"> |
| 79 | + <img |
| 80 | + src="https://img.shields.io/github/stars/tscircuit/tscircuit?style=social" |
| 81 | + alt="GitHub stars" |
| 82 | + /> |
| 83 | + </a> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ) |
5 | 88 | }
|
6 | 89 |
|
7 | 90 | export default App
|
0 commit comments