11import { useCallback } from "react"
22import { useStore } from "./store"
33import { CircuitJsonPreview } from "@tscircuit/runframe"
4+ import type { AnyCircuitElement } from "circuit-json"
5+ import type { SimpleRouteJson } from "@tscircuit/core"
46
57export const App = ( ) => {
68 const circuitJson = useStore ( ( s ) => s . circuitJson )
79 const setCircuitJson = useStore ( ( s ) => s . setCircuitJson )
810 const reset = useStore ( ( s ) => s . reset )
911
12+ const convertSimpleRouteJsonToCircuitJson = ( simpleRouteJson : SimpleRouteJson ) : AnyCircuitElement [ ] => {
13+ const circuitJson : AnyCircuitElement [ ] = [ ]
14+
15+ for ( const connection of simpleRouteJson . connections ) {
16+ const trace : AnyCircuitElement = {
17+ type : "pcb_trace" ,
18+ pcb_trace_id : connection . name ,
19+ route : connection . pointsToConnect . map ( point => ( {
20+ route_type : "wire" ,
21+ x : point . x ,
22+ y : point . y ,
23+ layer : point . layer as "top" | "bottom" | "inner1" | "inner2" | "inner3" | "inner4" | "inner5" | "inner6" ,
24+ width : simpleRouteJson . minTraceWidth ,
25+ } ) ) ,
26+ }
27+ circuitJson . push ( trace )
28+ }
29+
30+ return circuitJson
31+ }
32+
1033 const handleDrop = useCallback (
1134 ( e : React . DragEvent ) => {
1235 e . preventDefault ( )
@@ -16,7 +39,12 @@ export const App = () => {
1639 reader . onload = ( e ) => {
1740 try {
1841 const json = JSON . parse ( e . target ?. result as string )
19- setCircuitJson ( json )
42+ if ( json . connections && json . minTraceWidth !== undefined ) {
43+ const circuitJson = convertSimpleRouteJsonToCircuitJson ( json )
44+ setCircuitJson ( circuitJson )
45+ } else {
46+ setCircuitJson ( json )
47+ }
2048 } catch ( err ) {
2149 console . error ( "Failed to parse JSON:" , err )
2250 }
@@ -35,7 +63,12 @@ export const App = () => {
3563 reader . onload = ( e ) => {
3664 try {
3765 const json = JSON . parse ( e . target ?. result as string )
38- setCircuitJson ( json )
66+ if ( json . connections && json . minTraceWidth !== undefined ) {
67+ const circuitJson = convertSimpleRouteJsonToCircuitJson ( json )
68+ setCircuitJson ( circuitJson )
69+ } else {
70+ setCircuitJson ( json )
71+ }
3972 } catch ( err ) {
4073 console . error ( "Failed to parse JSON:" , err )
4174 }
0 commit comments