11import  {  CSSProto  }  from  './types' 
22
3- const  getItem  =  async  < T > ( key : string ) : Promise < T  |  null >  =>  { 
3+ export   const  getItem  =  async  < T > ( key : string ) : Promise < T  |  null >  =>  { 
44  const  item  =  await  chrome . storage . local . get ( key ) 
55  return  item [ key ]  ? ( JSON . parse ( item [ key ] )  as  T )  : null 
66} 
77
8- const  setItem  =  ( key : string ,  value : any )  =>  chrome . storage . local . set ( {  [ key ] : JSON . stringify ( value )  } ) 
8+ export   const  setItem  =  ( key : string ,  value : any )  =>  chrome . storage . local . set ( {  [ key ] : JSON . stringify ( value )  } ) 
99
1010export  const  getSettings  =  ( )  =>  getItem ( 'settings' ) 
1111export  const  getSites  =  ( )  =>  getItem ( 'sites' ) 
1212
1313export  const  setSettings  =  ( settings : any )  =>  setItem ( 'settings' ,  settings ) 
1414
1515export  const  getAllCssProtos  =  ( )  =>  getItem < CSSProto [ ] > ( 'css-protos' ) . then ( ( res )  =>  res  ||  [ ] ) 
16+ export  const  getAllCssProtosById  =  async  ( )  =>  { 
17+   const  cssProtos  =  await  getAllCssProtos ( ) 
18+   return  cssProtos . reduce ( ( acc : {  [ id : string ] : CSSProto  } ,  cssProto )  =>  ( {  ...acc ,  [ cssProto . id ] : cssProto  } ) ,  { } ) 
19+ } 
20+ 
1621export  const  addCSSProto  =  async  ( cssProto : CSSProto )  =>  setItem ( 'css-protos' ,  [ ...( await  getAllCssProtos ( ) ) ,  cssProto ] ) 
1722export  const  deleteCSSProto  =  async  ( id : string )  => 
1823  setItem ( 
@@ -34,30 +39,52 @@ export const updateCSSProto = async (id: string, partialCssProto: Partial<CSSPro
3439export  const  setActive  =  async  ( id : string ,  active : boolean )  =>  { 
3540  const  cssProtos  =  await  getAllCssProtos ( ) 
3641  const  index  =  cssProtos . findIndex ( ( cssProto )  =>  cssProto . id  ==  id ) 
37-   cssProtos [ index ] . options . active  =  active 
42+   cssProtos [ index ] . isActive  =  active 
3843  return  setItem ( 'css-protos' ,  cssProtos ) 
3944} 
4045
46+ export  const  bulkUpdateOrCreate  =  async  ( cssProtos : CSSProto [ ] )  =>  { 
47+   const  allProtos  =  await  getAllCssProtosById ( ) 
48+   for  ( const  proto  of  cssProtos )  { 
49+     if  ( allProtos [ proto . id ] )  await  updateCSSProto ( proto . id ,  proto ) 
50+     else  await  addCSSProto ( proto ) 
51+   } 
52+ } 
53+ 
54+ export  const  importFromUrl  =  async  ( url : string )  =>  { 
55+   // fetch the gist content 
56+   const  rawGist  =  await  fetch ( url ) . then ( ( res )  =>  res . text ( ) ) 
57+   const  cssProtos  =  transformGist ( rawGist ) 
58+   return  bulkUpdateOrCreate ( cssProtos ) 
59+ } 
60+ 
61+ export  const  transformGist  =  ( rawGist : string )  => 
62+   rawGist 
63+     . split ( / \/ \* { 3 , } / ) 
64+     . map ( ( blocks )  =>  blocks . trim ( ) ) 
65+     . filter ( ( blocks )  =>  blocks . length  >  0 ) 
66+     . map ( ( blocks )  =>  blocks . split ( '\n' ) . map ( ( line )  =>  line . trim ( ) ) ) 
67+     . reduce ( ( acc : CSSProto [ ] ,  blockLines )  =>  { 
68+       const  [ id ,  name ,  urlMatch ,  _ ,  maybeLF ,  ...cssRaw ]  =  blockLines 
69+       return  [ ...acc ,  {  id,  name,  urlMatch,  cssRaw : ( maybeLF  ==  '\n'  ? ''  : maybeLF )  +  cssRaw . join ( '\n' )  } ] 
70+     } ,  [ ] ) 
71+ 
4172const  cssProtos : CSSProto [ ]  =  [ 
4273  { 
4374    id : '1' , 
4475    urlMatch : 'https://www.google.com/' , 
4576    name : 'Google' , 
4677    cssRaw : 'body { background-color: red; }' , 
4778    cssCompiled : 'body { background-color: red; }' , 
48-     options : { 
49-       active : true , 
50-     } , 
79+     isActive : true , 
5180  } , 
5281  { 
5382    id : '2' , 
5483    urlMatch : 'https://luca.gg/' , 
5584    name : 'Google 2' , 
5685    cssRaw : 'body { background-color: blue; }' , 
5786    cssCompiled : 'body { background-color: blue; }' , 
58-     options : { 
59-       active : true , 
60-     } , 
87+     isActive : true , 
6188  } , 
6289] 
6390
0 commit comments