diff --git a/src/common/components/templates/Space.tsx b/src/common/components/templates/Space.tsx index 23b81940..37f5bcc7 100644 --- a/src/common/components/templates/Space.tsx +++ b/src/common/components/templates/Space.tsx @@ -27,6 +27,7 @@ export type SpaceConfig = { isEditable: boolean; fidgetTrayContents: FidgetInstanceData[]; theme: UserTheme; + timestamp?: string; }; export type SpaceConfigSaveDetails = Partial< diff --git a/src/common/data/stores/app/homebase/homebaseStore.ts b/src/common/data/stores/app/homebase/homebaseStore.ts index c819d834..7f160d79 100644 --- a/src/common/data/stores/app/homebase/homebaseStore.ts +++ b/src/common/data/stores/app/homebase/homebaseStore.ts @@ -4,7 +4,7 @@ import axios from "axios"; import { createClient } from "../../../database/supabase/clients/component"; import { homebasePath } from "@/constants/supabase"; import { SignedFile } from "@/common/lib/signedFiles"; -import { cloneDeep, debounce, isArray, mergeWith } from "lodash"; +import { cloneDeep, debounce, isArray, isUndefined, mergeWith } from "lodash"; import stringify from "fast-json-stable-stringify"; import axiosBackend from "../../../api/backend"; import { @@ -16,6 +16,7 @@ import { analytics, AnalyticsEvent, } from "@/common/providers/AnalyticsProvider"; +import moment from "moment"; interface HomeBaseStoreState { homebaseConfig?: SpaceConfig; @@ -59,6 +60,23 @@ export const createHomeBaseStoreFunc = ( const spaceConfig = JSON.parse( await get().account.decryptEncryptedSignedFile(fileData), ) as SpaceConfig; + const currentHomebase = get().homebase.homebaseConfig; + if ( + (spaceConfig && + spaceConfig.timestamp && + currentHomebase && + currentHomebase.timestamp && + moment(spaceConfig.timestamp).isAfter( + moment(currentHomebase.timestamp), + )) || + (spaceConfig && + isUndefined(spaceConfig.timestamp) && + currentHomebase && + currentHomebase.timestamp) + ) { + console.debug("local homebase config is more recent"); + return cloneDeep(currentHomebase); + } set((draft) => { draft.homebase.homebaseConfig = cloneDeep(spaceConfig); draft.homebase.remoteHomebaseConfig = cloneDeep(spaceConfig); @@ -102,6 +120,7 @@ export const createHomeBaseStoreFunc = ( mergeWith(localCopy, config, (_, newItem) => { if (isArray(newItem)) return newItem; }); + localCopy.timestamp = moment().toISOString(); set( (draft) => { draft.homebase.homebaseConfig = localCopy; diff --git a/src/common/data/stores/app/space/spaceStore.ts b/src/common/data/stores/app/space/spaceStore.ts index af62b39d..f6a71c00 100644 --- a/src/common/data/stores/app/space/spaceStore.ts +++ b/src/common/data/stores/app/space/spaceStore.ts @@ -129,6 +129,23 @@ export const createSpaceStoreFunc = ( const spaceConfig = JSON.parse( await get().account.decryptEncryptedSignedFile(fileData), ) as DatabaseWritableSpaceConfig; + const currentLocalCopy = get().space.localSpaces[spaceId]; + if ( + (spaceConfig && + spaceConfig.timestamp && + currentLocalCopy && + currentLocalCopy.timestamp && + moment(currentLocalCopy.timestamp).isAfter( + moment(spaceConfig.timestamp), + )) || + (spaceConfig && + isUndefined(spaceConfig.timestamp) && + currentLocalCopy && + currentLocalCopy.timestamp) + ) { + console.debug(`local copy of space ${spaceId} config is more recent`); + return; + } const updatableSpaceConfig = { ...spaceConfig, isPrivate: fileData.isEncrypted, @@ -268,14 +285,12 @@ export const createSpaceStoreFunc = ( }, saveLocalSpace: async (spaceId, changedConfig) => { const localCopy = cloneDeep(get().space.localSpaces[spaceId]); + mergeWith(localCopy, changedConfig, (_, newItem) => { + if (isArray(newItem)) return newItem; + }); + localCopy.timestamp = moment().toISOString(); set((draft) => { - draft.space.localSpaces[spaceId] = mergeWith( - localCopy, - changedConfig, - (_, newItem) => { - if (isArray(newItem)) return newItem; - }, - ); + draft.space.localSpaces[spaceId] = localCopy; }, "saveLocalSpace"); }, clear: () => {