22import  {  useContext ,  useEffect ,  useState ,  useMemo  }  from  "react" ; 
33
44import  {  EditmodeContext  }  from  "./EditmodeContext" ; 
5- import  {  api ,  computeContentKey  }  from  './utils ' 
6- import  {  renderChunk ,  getCachedData ,  storeCache   }  from  './utils/native ' 
5+ import  {  api ,  computeContentKey  }  from  './utilities ' 
6+ import  {  renderChunk ,  getCachedData ,  storeCache ,   sanitizeContent   }  from  './utilities ' 
77
8- export  function  useChunk ( defaultContent  =  "" ,  {  identifier,  type,  contentKey } )  { 
8+ export  function  useChunk ( defaultContent  =  "" ,  {  identifier,  type,  contentKey,  variables  } )  { 
99  const  {  projectId,  defaultChunks }  =  useContext ( EditmodeContext ) ; 
1010  const  [ chunk ,  setChunk ]  =  useState ( undefined ) ; 
1111
@@ -34,11 +34,20 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey })
3434  useEffect ( ( )  =>  { 
3535    // Render content 
3636    ( async  ( )  => { 
37-       const  cachedChunk  =  await  getCachedData ( cacheId ) ; 
38-       const  newChunk  =  cachedChunk  ? JSON . parse ( cachedChunk )  : fallbackChunk  ||  { 
39-         chunk_type : type  ||  "single_line_text" , 
40-         content : defaultContent , 
41-         content_key : contentKey 
37+       let  cachedChunk  =  await  getCachedData ( cacheId ) 
38+       let  newChunk ; 
39+       if  ( cachedChunk )  { 
40+         let  dataChunk  =  JSON . parse ( cachedChunk ) ; 
41+         const  parsedChunk  =  sanitizeContent ( dataChunk ,  variables ,  fallbackChunk ) 
42+         newChunk  =  parsedChunk ; 
43+       }  else  if  ( fallbackChunk )  { 
44+         newChunk  =  fallbackChunk ; 
45+       }  else  { 
46+         newChunk  =  { 
47+           chunk_type : type  ||  "single_line_text" , 
48+           content : defaultContent , 
49+           content_key : contentKey 
50+         } 
4251      } 
4352
4453      if  ( newChunk )  setChunk ( newChunk ) 
@@ -49,7 +58,10 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey })
4958        . get ( url ) 
5059        . then ( ( res )  =>  { 
5160          storeCache ( cacheId ,  res . data ) 
52-           if  ( ! cachedChunk )  setChunk ( res . data ) 
61+           if  ( ! cachedChunk )  { 
62+             const  parsedChunk  =  sanitizeContent ( res . data ,  variables ,  fallbackChunk ) 
63+             setChunk ( parsedChunk ) 
64+           } 
5365        } )  // Store chunk to localstorage 
5466        . catch ( ( error )  =>  console . log ( error ) ) ;  // Set error state 
5567
0 commit comments