Skip to content

Commit

Permalink
Autosaves: Delete local backup after auto-saving to DB (#12578)
Browse files Browse the repository at this point in the history
  • Loading branch information
miina committed Nov 8, 2022
1 parent 52495b7 commit 6b7dd46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function LocalAutoSave() {
state: { hasNewChanges },
} = useHistory();
const isUploading = useIsUploadingToStory();
const { isAutoSavingStory } = useStory(({ state }) => ({
isAutoSavingStory: state.meta.isAutoSavingStory,
}));

const { story, pages, restoreLocalAutoSave } = useStory(
({ state, actions }) => ({
Expand All @@ -65,7 +68,12 @@ function LocalAutoSave() {

// Save the local autosave.
useEffect(() => {
if (!hasNewChanges || !localAutoSaveInterval || isUploading) {
if (
!hasNewChanges ||
!localAutoSaveInterval ||
isUploading ||
isAutoSavingStory
) {
return undefined;
}

Expand All @@ -90,13 +98,26 @@ function LocalAutoSave() {
story,
storyId,
isNew,
isAutoSavingStory,
]);

const onClose = () => {
sessionStore.deleteItemByKey(getSessionStorageKey(storyId, isNew));
setBackup(null);
};

const didAutoSaveTracker = useRef(isAutoSavingStory);
useEffect(() => {
if (isAutoSavingStory) {
didAutoSaveTracker.current = true;
// If we auto-saved to DB before but are not auto-saving anymore, let's delete the local backup.
// No need for both local and DB backup together.
} else if (didAutoSaveTracker.current && !backup) {
sessionStore.deleteItemByKey(getSessionStorageKey(storyId, false));
didAutoSaveTracker.current = false;
}
}, [isAutoSavingStory, backup, storyId]);

const hadNewChangesTracker = useRef(false);
const wasNewTracker = useRef(isNew);
useEffect(() => {
Expand Down Expand Up @@ -131,7 +152,6 @@ function LocalAutoSave() {
JSON.stringify(existingAutoSave.story) !==
JSON.stringify(storyRef.current);

// @todo Should we also check for DB autosave once it's implemented in #1402?
// If we have an autosave and it differs from the current state.
if (autoSaveHasChanges) {
setBackup(existingAutoSave);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function setup({
state: {
story: { status, storyId: 1 },
pages: [{ id: 1 }],
meta: {
isAutoSavingStory: false,
},
},
actions: {
restoreLocalAutoSave: jest.fn(),
Expand Down

0 comments on commit 6b7dd46

Please sign in to comment.