From 7aabd54ac8eb3d1e48213a6bbe51aea71d8824ca Mon Sep 17 00:00:00 2001 From: Julian Early Date: Thu, 11 Jul 2024 15:03:28 -0700 Subject: [PATCH 1/3] fix: only show new versions of spaces if they are newer than the local copies --- src/common/components/templates/Space.tsx | 1 + .../data/stores/app/homebase/homebaseStore.ts | 12 +++++++++ .../data/stores/app/space/spaceStore.ts | 25 +++++++++++++------ src/pages/api/signerRequests.ts | 14 +++++------ 4 files changed, 38 insertions(+), 14 deletions(-) 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..0d687f51 100644 --- a/src/common/data/stores/app/homebase/homebaseStore.ts +++ b/src/common/data/stores/app/homebase/homebaseStore.ts @@ -16,6 +16,7 @@ import { analytics, AnalyticsEvent, } from "@/common/providers/AnalyticsProvider"; +import moment from "moment"; interface HomeBaseStoreState { homebaseConfig?: SpaceConfig; @@ -59,6 +60,16 @@ export const createHomeBaseStoreFunc = ( const spaceConfig = JSON.parse( await get().account.decryptEncryptedSignedFile(fileData), ) as SpaceConfig; + const currentHomebase = get().homebase.homebaseConfig; + if ( + spaceConfig.timestamp && + currentHomebase && + currentHomebase.timestamp && + moment(spaceConfig.timestamp).isAfter(moment(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 +113,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..ce48e5d3 100644 --- a/src/common/data/stores/app/space/spaceStore.ts +++ b/src/common/data/stores/app/space/spaceStore.ts @@ -129,6 +129,19 @@ 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), + ) + ) { + console.debug(`local copy of space ${spaceId} config is more recent`); + return; + } const updatableSpaceConfig = { ...spaceConfig, isPrivate: fileData.isEncrypted, @@ -268,14 +281,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: () => { diff --git a/src/pages/api/signerRequests.ts b/src/pages/api/signerRequests.ts index 5ab7c111..0d073dfa 100644 --- a/src/pages/api/signerRequests.ts +++ b/src/pages/api/signerRequests.ts @@ -191,13 +191,13 @@ async function handleGet( ); const value: SignedKeyRequestResponse = - process.env.NEXT_PUBLIC_VERCEL_ENV === "development" - ? { - ...data.result.signedKeyRequest, - state: "completed", - userFid: 1, - } - : data.result.signedKeyRequest; + // process.env.NEXT_PUBLIC_VERCEL_ENV === "development" + // ? { + // ...data.result.signedKeyRequest, + // state: "completed", + // userFid: 1, + // } : + data.result.signedKeyRequest; return res.status(200).json({ result: "success", From c437df583634ff2d85785c04b2511677b0214256 Mon Sep 17 00:00:00 2001 From: Julian Early Date: Fri, 12 Jul 2024 13:18:13 -0700 Subject: [PATCH 2/3] fix: roll back back change --- src/pages/api/signerRequests.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pages/api/signerRequests.ts b/src/pages/api/signerRequests.ts index 0d073dfa..5ab7c111 100644 --- a/src/pages/api/signerRequests.ts +++ b/src/pages/api/signerRequests.ts @@ -191,13 +191,13 @@ async function handleGet( ); const value: SignedKeyRequestResponse = - // process.env.NEXT_PUBLIC_VERCEL_ENV === "development" - // ? { - // ...data.result.signedKeyRequest, - // state: "completed", - // userFid: 1, - // } : - data.result.signedKeyRequest; + process.env.NEXT_PUBLIC_VERCEL_ENV === "development" + ? { + ...data.result.signedKeyRequest, + state: "completed", + userFid: 1, + } + : data.result.signedKeyRequest; return res.status(200).json({ result: "success", From b4ff9fd7640cf9281b08b1baa5f879d416746a0e Mon Sep 17 00:00:00 2001 From: Julian Early Date: Fri, 12 Jul 2024 13:20:54 -0700 Subject: [PATCH 3/3] fix: address comments --- .../data/stores/app/homebase/homebaseStore.ts | 17 ++++++++++++----- src/common/data/stores/app/space/spaceStore.ts | 18 +++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/common/data/stores/app/homebase/homebaseStore.ts b/src/common/data/stores/app/homebase/homebaseStore.ts index 0d687f51..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 { @@ -62,10 +62,17 @@ export const createHomeBaseStoreFunc = ( ) as SpaceConfig; const currentHomebase = get().homebase.homebaseConfig; if ( - spaceConfig.timestamp && - currentHomebase && - currentHomebase.timestamp && - moment(spaceConfig.timestamp).isAfter(moment(currentHomebase.timestamp)) + (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); diff --git a/src/common/data/stores/app/space/spaceStore.ts b/src/common/data/stores/app/space/spaceStore.ts index ce48e5d3..f6a71c00 100644 --- a/src/common/data/stores/app/space/spaceStore.ts +++ b/src/common/data/stores/app/space/spaceStore.ts @@ -131,13 +131,17 @@ export const createSpaceStoreFunc = ( ) as DatabaseWritableSpaceConfig; const currentLocalCopy = get().space.localSpaces[spaceId]; if ( - spaceConfig && - spaceConfig.timestamp && - currentLocalCopy && - currentLocalCopy.timestamp && - moment(currentLocalCopy.timestamp).isAfter( - moment(spaceConfig.timestamp), - ) + (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;