Skip to content

Commit

Permalink
Fix forked component state
Browse files Browse the repository at this point in the history
  • Loading branch information
shelegdmitriy committed Sep 6, 2023
1 parent 51ffdc4 commit 1c40cfd
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/components/sandbox/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const Sandbox = ({ onboarding = false }) => {
codeVisible: code || state[jpath].codeVisible,
},
}));
});
});
},
[cache],
);
Expand All @@ -142,26 +142,30 @@ export const Sandbox = ({ onboarding = false }) => {
const { codeMain, codeDraft, isDraft } = getWidgetDetails(widgetObject);
const newPath = fileToPath(file);
const code = codeDraft || codeMain;
updateCodeLocalStorage(newPath, codeDraft || codeMain, cache);
const changesMade = checkChangesMade(codeMain, codeDraft, code);

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

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

if (changesMade) {
updateCodeLocalStorage(newPath, code, cache);
}
}
};
fetchCode();
Expand Down Expand Up @@ -224,22 +228,14 @@ export const Sandbox = ({ onboarding = false }) => {
setMetadata(undefined);
};

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

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

const firstLoad = useCallback(() => {
Expand All @@ -262,8 +258,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 +273,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,8 +296,8 @@ export const Sandbox = ({ onboarding = false }) => {
};

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

setFilesObject((state) => ({
...state,
Expand Down

0 comments on commit 1c40cfd

Please sign in to comment.