@@ -247,26 +247,93 @@ function HerbieUIInner() {
247247 const loadSpecFromGist = async ( gistId : string ) => {
248248 const octokit = new Octokit ( ) ;
249249
250- try {
250+ const response = await octokit . request ( "GET /gists/{gist_id}" , {
251+ gist_id : gistId ,
252+ headers : {
253+ "X-GitHub-Api-Version" : "2022-11-28" ,
254+ "accept" : "application/vnd.github+json" ,
255+ } ,
256+ } ) ;
251257
252- const response = await octokit . request ( "GET /gists/{gist_id}" , {
253- gist_id : gistId ,
254- headers : {
255- "X-GitHub-Api-Version" : "2022-11-28" ,
256- "accept" : "application/vnd.github+json" ,
257- } ,
258- } ) ;
259-
260- const fileKey = Object . keys ( response . data . files ) [ 0 ] ;
258+ const fileKey = Object . keys ( response . data . files ) [ 0 ] ;
261259
262- if ( fileKey . length === 0 ) throw new Error ( "No files found in Gist." ) ;
260+ if ( fileKey . length === 0 ) throw new Error ( "No files found in Gist." ) ;
263261
264- const rawUrl = response . data . files [ fileKey ] . raw_url ;
265-
266- const rawResponse = await fetch ( rawUrl ) ;
267- const jsonState = await rawResponse . json ( ) ;
262+ const rawUrl = response . data . files [ fileKey ] . raw_url ;
263+
264+ const rawResponse = await fetch ( rawUrl ) ;
265+ const jsonState = await rawResponse . json ( ) ;
266+
267+ console . log ( "gist jsonState from gist:" , gistId , jsonState ) ;
268+
269+ // LOAD THE STATE FROM JSON
270+ // ------------------------
271+
272+ setServerUrl ( jsonState . serverUrl ) ;
273+ setFPTaylorServerUrl ( jsonState . fptaylorServerUrl ) ;
274+ setFPBenchServerUrl ( jsonState . fpbenchServerUrl ) ;
275+
276+ const newSpecId = spec . id + 1 ;
277+ const inputRangeId = nextId ( inputRangesTable ) ;
278+
279+ const inputRanges = jsonState . specRanges
280+ ? new HerbieTypes . InputRanges ( jsonState . specRanges , newSpecId , inputRangeId )
281+ : new HerbieTypes . RangeInSpecFPCore ( newSpecId , inputRangeId ) ;
282+
283+ setArchivedExpressions ( expressions . map ( e => e . id ) ) ;
284+ setInputRangesTable ( [ ...inputRangesTable , inputRanges ] ) ;
285+ setSpec ( { expression : jsonState . spec . expression , id : newSpecId , fpcore : jsonState . spec . fpcore } ) ;
286+
287+ const oldIdToNew : Map < number , HerbieTypes . Expression > = new Map ( ) ;
288+ const newExpressions = [ ] ;
289+ const newDerivations = [ ] ;
290+
291+ for ( let i = 0 ; i < jsonState . expressions . length ; i ++ ) {
292+ const expr = jsonState . expressions [ i ] ;
293+ const newId = nextId ( expressions ) + i ;
294+ const newExpr = new HerbieTypes . Expression ( expr . text , newId , newSpecId , expr . tex ) ;
295+ oldIdToNew . set ( expr . id , newExpr ) ;
296+ newExpressions . push ( newExpr ) ;
297+ }
298+
299+ for ( const deriv of jsonState . derivations ) {
300+ const newExpr = oldIdToNew . get ( deriv . id ) ;
301+ const newParent = deriv . origExpId ? oldIdToNew . get ( deriv . origExpId ) : undefined ;
302+ if ( newExpr ) {
303+ newDerivations . push ( new HerbieTypes . Derivation ( deriv . history , newExpr . id , newParent ?. id ) ) ;
304+ }
305+ }
306+
307+ setExpressions ( [ ...newExpressions , ...expressions ] ) ;
308+ setDerivations ( [ ...newDerivations , ...derivations ] ) ;
309+ setSelectedExprId ( oldIdToNew . get ( jsonState . selectedExprId ) ?. id ?? - 1 ) ;
310+ setExpandedExpressions ( jsonState . expandedExpressions . map ( ( id : number ) => oldIdToNew . get ( id ) ?. id ?? - 1 ) ) ;
311+ setCompareExprIds ( jsonState . compareExprIds . map ( ( id : number ) => oldIdToNew . get ( id ) ?. id ?? - 1 ) ) ;
312+
313+ // ------------------------
314+
315+ setShowSpecEntry ( false ) ;
268316
269- console . log ( "gist jsonState from gist:" , gistId , jsonState ) ;
317+ } ;
318+
319+ // -----------------
320+ const queryParams = new URLSearchParams ( window . location . search ) ;
321+
322+ const gistId = queryParams . get ( 'gist' ) ;
323+
324+ if ( gistId != null ) {
325+ loadSpecFromGist ( gistId ) ;
326+ }
327+ }
328+
329+
330+ // Load in data from URL with json_url
331+ useEffect ( loadStateFromURL , [ ] )
332+ function loadStateFromURL ( ) {
333+ // -----------------
334+ const loadStateFromURLInner = async ( json_url : string ) => {
335+ const rawResponse = await fetch ( json_url ) ;
336+ const jsonState = await rawResponse . json ( ) ;
270337
271338 // LOAD THE STATE FROM JSON
272339 // ------------------------
@@ -315,20 +382,15 @@ function HerbieUIInner() {
315382 // ------------------------
316383
317384 setShowSpecEntry ( false ) ;
318-
319- } catch ( err ) {
320- console . error ( "Failed to import state from Gist:" , err ) ;
321- }
322-
323385 } ;
324386
325387 // -----------------
326388 const queryParams = new URLSearchParams ( window . location . search ) ;
327389
328- const gistId = queryParams . get ( 'gist ' ) ;
390+ const json_url = queryParams . get ( 'json_url ' ) ;
329391
330- if ( gistId != null ) {
331- loadSpecFromGist ( gistId ) ;
392+ if ( json_url != null ) {
393+ loadStateFromURLInner ( json_url ) ;
332394 }
333395 }
334396
0 commit comments