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: only show new versions of spaces if they are newer than the local copies #249

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/common/components/templates/Space.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type SpaceConfig = {
isEditable: boolean;
fidgetTrayContents: FidgetInstanceData[];
theme: UserTheme;
timestamp?: string;
};

export type SpaceConfigSaveDetails = Partial<
Expand Down
21 changes: 20 additions & 1 deletion src/common/data/stores/app/homebase/homebaseStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +16,7 @@ import {
analytics,
AnalyticsEvent,
} from "@/common/providers/AnalyticsProvider";
import moment from "moment";

interface HomeBaseStoreState {
homebaseConfig?: SpaceConfig;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 22 additions & 7 deletions src/common/data/stores/app/space/spaceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: () => {
Expand Down