Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forked component state #358

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 51 additions & 65 deletions src/components/sandbox/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,13 @@ export const Sandbox = ({ onboarding = false }) => {

const setComponentSrc = useCurrentComponentStore((store) => store.setSrc);

const getFileLocalStorage = useCallback(
(file) => {
const path = fileToPath(file);
const jpath = fileToJpath(file);

cache
.asyncLocalStorageGet(StorageDomain, {
path,
type: StorageType.Code,
})
.then(({ code } = {}) => {
setFilesObject((state) => ({
...state,
[jpath]: {
...state[jpath],
codeLocalStorage: code,
codeVisible: code || state[jpath].codeVisible,
},
}));
});
},
[cache],
);

const getFileSocialDB = useCallback(
const getFileData = useCallback(
(file) => {
if (!file.src) {
return;
}

const path = fileToPath(file);
const jpath = fileToJpath(file);
const widgetSrc = `${file.src}/**`;

Expand All @@ -140,29 +117,45 @@ export const Sandbox = ({ onboarding = false }) => {

if (widgetObject && file.new) {
const { codeMain, codeDraft, isDraft } = getWidgetDetails(widgetObject);
const newPath = fileToPath(file);
const code = codeDraft || codeMain;
updateCodeLocalStorage(newPath, codeDraft || codeMain, cache);

const singleFileObject = {
codeMain,
codeDraft,
isDraft,
savedOnChain: true,
new: false,
codeLocalStorage: code,
codeVisible: code,
};

setFilesObject((state) => ({
...state,
[jpath]: {
...state[jpath],
...singleFileObject,
changesMade: checkChangesMade(codeMain, codeDraft, code),
},
}));
const onChainCode = codeDraft || codeMain;

cache
.asyncLocalStorageGet(StorageDomain, {
path,
type: StorageType.Code,
})
.then(({ code } = {}) => {
setFilesObject((state) => ({
...state,
[jpath]: {
...state[jpath],
codeMain,
codeDraft,
isDraft,
savedOnChain: true,
changesMade: code ? checkChangesMade(onChainCode, codeDraft, code) : false,
codeLocalStorage: code || onChainCode,
codeVisible: code || state[jpath].codeVisible || onChainCode,
},
}));
});
}

cache
.asyncLocalStorageGet(StorageDomain, {
path,
type: StorageType.Code,
})
.then(({ code } = {}) => {
setFilesObject((state) => ({
...state,
[jpath]: {
...state[jpath],
codeLocalStorage: code,
codeVisible: code || state[jpath].codeVisible,
},
}));
});
};
fetchCode();
},
Expand Down Expand Up @@ -202,9 +195,9 @@ export const Sandbox = ({ onboarding = false }) => {
addFile(newFile);
setRenderCode(null);
selectFile(path);
getFileSocialDB(newFile);
getFileData(newFile);
},
[accountId, addFile, getFileSocialDB, onboarding],
[accountId, addFile, getFileData, onboarding],
);

useEffect(() => {
Expand All @@ -224,22 +217,13 @@ export const Sandbox = ({ onboarding = false }) => {
setMetadata(undefined);
};

const getAllFileLocalStorage = useCallback(
const collectAllFileData = useCallback(
(filesObject) => {
Object.values(filesObject).map((file) => {
getFileLocalStorage(file);
getFileData(file);
});
},
[getFileLocalStorage],
);

const getAllFileSocialDB = useCallback(
(filesObject) => {
Object.values(filesObject).map((file) => {
getFileSocialDB(file);
});
},
[getFileSocialDB],
[getFileData],
);

const firstLoad = useCallback(() => {
Expand All @@ -262,8 +246,7 @@ export const Sandbox = ({ onboarding = false }) => {

setFilesObject(filesObject);
selectFile(filesObject[fileToJpath(path)]);
getAllFileLocalStorage(filesObject);
getAllFileSocialDB(filesObject);
collectAllFileData(filesObject);

if (onboarding) {
return;
Expand All @@ -278,7 +261,7 @@ export const Sandbox = ({ onboarding = false }) => {

setDefaultWidget(componentSrc.join('/'));
});
}, [cache, currentStep, getAllFileLocalStorage, getAllFileSocialDB, onboarding, router.query, setComponentSrc]);
}, [cache, collectAllFileData, currentStep, onboarding, router.query, setComponentSrc]);

const renameFile = (newName) => {
const pathNew = nameToPath(path.type, newName);
Expand All @@ -301,7 +284,6 @@ export const Sandbox = ({ onboarding = false }) => {
};

const changeCode = (path, code) => {
updateCodeLocalStorage(path, code, cache);
const jpath = JSON.stringify(path);

setFilesObject((state) => ({
Expand All @@ -310,9 +292,11 @@ export const Sandbox = ({ onboarding = false }) => {
...state[jpath],
codeLocalStorage: code,
codeVisible: code,
savedOnChain: false,
changesMade: checkChangesMade(state[jpath].codeMain, state[jpath].codeDraft, code),
},
}));
updateCodeLocalStorage(path, code, cache);
};

const reformat = (path, code) => {
Expand Down Expand Up @@ -440,7 +424,9 @@ export const Sandbox = ({ onboarding = false }) => {
selectFile(newPath);
};

// possibly unused
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this section below is unused @marcinbodnar could you confirm that?

const reloadFile = () => {
console.log('reloadFile which is never called');
const onboardingPath = onboardingComponents.starter;
selectFile(onboardingPath);
setMainLoader(false);
Expand Down