Skip to content

Commit 1825f0b

Browse files
authored
Feature: Chunks Fallback (#18)
* add way to cache chunks * add chunk fallback * rename chunkFallback to defaultChunks
1 parent 9771856 commit 1825f0b

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/Editmode.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { useEffect } from "react";
33
import { EditmodeContext } from "./EditmodeContext";
44
import { Platform } from 'react-native';
55

6-
export function Editmode({ children, projectId }) {
6+
export function Editmode({ children, projectId, defaultChunks }) {
77
if (!projectId) {
88
throw new Error("<Editmode projectId={...}> is missing a valid projectId");
99
}
@@ -25,7 +25,7 @@ export function Editmode({ children, projectId }) {
2525
}
2626

2727
return (
28-
<EditmodeContext.Provider value={{ branch, projectId }}>
28+
<EditmodeContext.Provider value={{ branch, projectId, defaultChunks }}>
2929
{children}
3030
</EditmodeContext.Provider>
3131
);

src/EditmodeContext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { createContext } from "react";
33
export const EditmodeContext = createContext({
44
branch: null,
55
projectId: null,
6+
defaultChunks: null,
67
});

src/useChunk.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { api } from "./api";
55
import { EditmodeContext } from "./EditmodeContext";
66
import { renderChunk } from "./utils/renderChunk.jsx";
77
import { computeContentKey } from "./utils/computeContentKey";
8+
import { Platform, AsyncStorage } from 'react-native';
89

910
export function useChunk(defaultContent, { identifier, type }) {
10-
const { projectId } = useContext(EditmodeContext);
11+
const { projectId, defaultChunks } = useContext(EditmodeContext);
1112
const [[error, chunk], setResponse] = useState([undefined, undefined]);
1213
const contentKey = defaultContent ? computeContentKey(defaultContent) : null;
1314
const url = identifier
@@ -43,13 +44,61 @@ export function useChunk(defaultContent, { identifier, type }) {
4344
};
4445
}
4546

47+
const fallbackChunk = defaultChunks.filter(chunkItem => chunkItem.identifier === identifier)[0];
48+
4649
if (!chunk) {
47-
return {
48-
Component() {
49-
return null;
50-
},
51-
content: defaultContent,
52-
};
50+
if (!defaultContent && !fallbackChunk) {
51+
let cachedChunk;
52+
53+
// Fetch Data
54+
if(Platform.OS === 'web') {
55+
cachedChunk = localStorage.getItem(identifier);
56+
} else {
57+
const fetchChunk = async () => {
58+
try {
59+
cachedChunk = await AsyncStorage.getItem(identifier);
60+
} catch (error) {
61+
console.log('Error in fetching chunk.', error);
62+
}
63+
};
64+
}
65+
return {
66+
Component() {
67+
return null;
68+
},
69+
content: cachedChunk,
70+
};
71+
} else if (fallbackChunk) {
72+
return {
73+
Component() {
74+
return null;
75+
},
76+
content: fallbackChunk,
77+
};
78+
} else if (defaultContent) {
79+
return {
80+
Component() {
81+
return null;
82+
},
83+
content: defaultContent,
84+
};
85+
}
86+
} else {
87+
// Store Data
88+
if(Platform.OS === 'web') {
89+
localStorage.setItem(chunk.identifier, JSON.stringify(chunk));
90+
} else {
91+
const storeChunk = async () => {
92+
try {
93+
await AsyncStorage.setItem(
94+
chunk.identifier,
95+
JSON.stringify(chunk)
96+
);
97+
} catch (error) {
98+
console.log('Error in saving chunk.', error);
99+
}
100+
}
101+
}
53102
}
54103

55104
return {

0 commit comments

Comments
 (0)