@@ -20,9 +20,14 @@ import {
2020 Examples ,
2121 CSourceRow ,
2222 StoredLogs ,
23+ StoredCLines ,
2324} from "./components" ;
2425import { getCLineId , ParsedLine , ParsedLineType } from "./parser" ;
2526
27+ // Increment the version if there is a breaking change
28+ // in what we store
29+ const STORED_LOG_KEY = "logs-v2" ;
30+
2631function getEmptyVisualLogState ( ) : VisualLogState {
2732 return {
2833 verifierLogState : getEmptyVerifierState ( ) ,
@@ -62,8 +67,7 @@ function getVisibleLogLines(
6267
6368function getVisibleCLines (
6469 verifierLogState : VerifierLogState ,
65- fileName : string = "" ,
66- pastedLines : string [ ] = [ ] ,
70+ storedCLines : StoredCLines = { } ,
6771) : [ CSourceRow [ ] , Map < string , number > ] {
6872 const cLineIdToVisualIdx : Map < string , number > = new Map ( ) ;
6973 const cLines : CSourceRow [ ] = [ ] ;
@@ -76,8 +80,8 @@ function getVisibleCLines(
7680 } ) ;
7781 ++ j ;
7882
79- if ( file === fileName && pastedLines . length > 0 ) {
80- pastedLines . forEach ( ( lineText , i ) => {
83+ if ( file in storedCLines ) {
84+ storedCLines [ file ] . forEach ( ( lineText , i ) => {
8185 const lineNum = i + 1 ;
8286 const sourceId = getCLineId ( file , lineNum ) ;
8387 cLines . push ( {
@@ -232,7 +236,7 @@ function App({ testListHeight }: { testListHeight?: number }) {
232236 logLineIdxToVisualIdx . get ( hoveredState . line ) || 0 ;
233237
234238 useEffect ( ( ) => {
235- ldb . get ( "logs" , function ( value ) {
239+ ldb . get ( STORED_LOG_KEY , function ( value ) {
236240 try {
237241 const logs : StoredLogs = JSON . parse ( value ) ;
238242 if ( logs ) {
@@ -268,19 +272,23 @@ function App({ testListHeight }: { testListHeight?: number }) {
268272 } ) ;
269273 } , [ verifierLogState ] ) ;
270274
271- const updateStoredLogs = useCallback (
275+ const updateStoredLogs = useCallback ( ( nextStoredLogs : StoredLogs ) => {
276+ ldb . set ( STORED_LOG_KEY , JSON . stringify ( nextStoredLogs ) ) ;
277+ setStoredLogs ( nextStoredLogs ) ;
278+ } , [ ] ) ;
279+
280+ const addStoredLog = useCallback (
272281 ( lines : string [ ] , name : string = "" ) => {
273282 const now = new Date ( ) ;
274283 const nextName = name
275284 ? name
276285 : `pasted log (${ now . toDateString ( ) . toLowerCase ( ) } - ${ now . toLocaleTimeString ( ) . toLocaleLowerCase ( ) } )` ;
277286 // Only keep 5 stored logs for now
278287 const nextStoredLogs : StoredLogs = [
279- [ nextName , lines ] ,
288+ [ nextName , { rawLogLines : lines , pastedCLines : { } } ] ,
280289 ...storedLogs . slice ( 0 , 5 ) ,
281290 ] ;
282- ldb . set ( "logs" , JSON . stringify ( nextStoredLogs ) ) ;
283- setStoredLogs ( nextStoredLogs ) ;
291+ updateStoredLogs ( nextStoredLogs ) ;
284292 } ,
285293 [ storedLogs ] ,
286294 ) ;
@@ -293,29 +301,60 @@ function App({ testListHeight }: { testListHeight?: number }) {
293301
294302 const addPastedCSourceFile = useCallback (
295303 ( fileName : string , pastedLines : string [ ] ) => {
296- setVisualLogState ( ( prevState ) => {
297- const [ cLines , cLineIdToVisualIdx ] = getVisibleCLines (
298- verifierLogState ,
299- fileName ,
300- pastedLines ,
301- ) ;
302- return {
303- ...prevState ,
304- cLines,
305- cLineIdToVisualIdx,
306- } ;
304+ const nextStoredLogs : StoredLogs = [ ] ;
305+ storedLogs . forEach ( ( storedLog , idx ) => {
306+ // The first stored log is the current one
307+ if ( idx !== 0 ) {
308+ nextStoredLogs . push ( storedLog ) ;
309+ return ;
310+ }
311+
312+ const nextPastedCLines : StoredCLines = { } ;
313+ let found = false ;
314+ for ( const [ storedFileName , lines ] of Object . entries (
315+ storedLog [ 1 ] . pastedCLines ,
316+ ) ) {
317+ if ( storedFileName !== fileName ) {
318+ nextPastedCLines [ storedFileName ] = lines ;
319+ return ;
320+ }
321+ found = true ;
322+ nextPastedCLines [ storedFileName ] = pastedLines ;
323+ }
324+ if ( ! found ) {
325+ nextPastedCLines [ fileName ] = pastedLines ;
326+ }
327+ nextStoredLogs . push ( [
328+ storedLog [ 0 ] ,
329+ {
330+ rawLogLines : storedLog [ 1 ] . rawLogLines ,
331+ pastedCLines : nextPastedCLines ,
332+ } ,
333+ ] ) ;
334+ setVisualLogState ( ( prevState ) => {
335+ const [ cLines , cLineIdToVisualIdx ] = getVisibleCLines (
336+ verifierLogState ,
337+ nextPastedCLines ,
338+ ) ;
339+ return {
340+ ...prevState ,
341+ cLines,
342+ cLineIdToVisualIdx,
343+ } ;
344+ } ) ;
307345 } ) ;
346+ updateStoredLogs ( nextStoredLogs ) ;
308347 } ,
309- [ verifierLogState ] ,
348+ [ verifierLogState , storedLogs ] ,
310349 ) ;
311350
312351 const loadInputText = useCallback (
313352 ( text : string ) => {
314353 const rawLines = text . split ( "\n" ) ;
315354 loadLog ( rawLines ) ;
316- updateStoredLogs ( rawLines ) ;
355+ addStoredLog ( rawLines ) ;
317356 } ,
318- [ updateStoredLogs ] ,
357+ [ addStoredLog ] ,
319358 ) ;
320359
321360 const prepareNewLog = useCallback ( ( ) => {
@@ -352,7 +391,30 @@ function App({ testListHeight }: { testListHeight?: number }) {
352391 if ( ! found ) {
353392 console . error ( "Couldn't load previous log" , example ) ;
354393 } else {
355- loadLog ( found [ 1 ] ) ;
394+ loadLog ( found [ 1 ] . rawLogLines ) ;
395+ // Move this to the most recent stored log
396+ const nextStoredLogs : StoredLogs = [ [ found [ 0 ] , found [ 1 ] ] ] ;
397+ storedLogs . forEach ( ( storedLog ) => {
398+ if ( storedLog [ 0 ] !== example ) {
399+ nextStoredLogs . push ( storedLog ) ;
400+ }
401+ } ) ;
402+ updateStoredLogs ( nextStoredLogs ) ;
403+ const newVerifierLogState = processRawLines ( found [ 1 ] . rawLogLines ) ;
404+ const nextVisualLogState = getVisualLogState (
405+ newVerifierLogState ,
406+ false ,
407+ ) ;
408+ const [ cLines , cLineIdToVisualIdx ] = getVisibleCLines (
409+ newVerifierLogState ,
410+ found [ 1 ] . pastedCLines ,
411+ ) ;
412+ setVisualLogState ( {
413+ ...nextVisualLogState ,
414+ cLines,
415+ cLineIdToVisualIdx,
416+ } ) ;
417+ setIsLoading ( false ) ;
356418 }
357419 }
358420 } ,
@@ -437,7 +499,7 @@ function App({ testListHeight }: { testListHeight?: number }) {
437499 }
438500 rawLines = rawLines . concat ( lines ) ;
439501 }
440- updateStoredLogs ( rawLines , fileBlob . name ) ;
502+ addStoredLog ( rawLines , fileBlob . name ) ;
441503 const newVerifierLogState = processRawLines ( rawLines ) ;
442504 setVisualLogState (
443505 getVisualLogState ( newVerifierLogState , visualLogState . showFullLog ) ,
0 commit comments