Skip to content

Commit

Permalink
Update Spaces using merge instead of keeping Grid Local state (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiporox authored Jul 7, 2024
1 parent 55aeafe commit fcda7a4
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 196 deletions.
4 changes: 2 additions & 2 deletions src/common/components/pages/SpacePage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { ReactNode, useRef, useState } from "react";
import Sidebar from "../organisms/Sidebar";
import Space, { SpaceConfig } from "../templates/Space";
import Space, { SpaceConfig, SpaceConfigSaveDetails } from "../templates/Space";
import { isUndefined } from "lodash";
import SpaceLoading from "../templates/SpaceLoading";

type SpacePageArgs = {
config?: SpaceConfig;
saveConfig?: (config: SpaceConfig) => Promise<void>;
saveConfig?: (config: SpaceConfigSaveDetails) => Promise<void>;
commitConfig?: () => Promise<void>;
resetConfig?: () => Promise<void>;
profile?: ReactNode;
Expand Down
43 changes: 16 additions & 27 deletions src/common/components/pages/UserDefinedSpace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useAuthenticatorManager } from "@/authenticators/AuthenticatorManager";
import { useAppStore } from "@/common/data/stores/app";
import createIntialPersonSpaceConfigForFid from "@/constants/initialPersonSpace";
import { SpaceConfig } from "../templates/Space";
import { UpdatableSpaceConfig } from "@/common/data/stores/app/space/spaceStore";
import { SpaceConfigSaveDetails } from "../templates/Space";
import Profile from "@/fidgets/ui/profile";
import SpacePage from "./SpacePage";

Expand Down Expand Up @@ -38,18 +37,17 @@ export default function UserDefinedSpace({
saveLocalCopy: state.space.saveLocalSpace,
commitSpaceToDb: state.space.commitSpaceToDatabase,
registerSpace: state.space.registerSpace,
currentSpaceId: state.currentSpace.currentSpaceId,
getCurrentSpaceConfig: state.currentSpace.getCurrentSpaceConfig,
setCurrentSpaceId: state.currentSpace.setCurrentSpaceId,
}));
const [loading, setLoading] = useState(!isNil(providedSpaceId));
const [loadSuccess, setLoadSuccesss] = useState(false);

useEffect(() => {
setCurrentSpaceId(providedSpaceId);
if (!isNil(providedSpaceId)) {
setLoading(true);
loadSpace(providedSpaceId).then((res) => {
setLoadSuccesss(res !== null);
loadSpace(providedSpaceId, fid).then(() => {
setSpaceId(providedSpaceId);
setLoading(false);
});
Expand Down Expand Up @@ -94,42 +92,33 @@ export default function UserDefinedSpace({
[fid],
);

const config: SpaceConfig | undefined = useMemo(() => {
if (!isNil(spaceId)) {
if (loading) {
return undefined;
}
const currentSpaceConfig = getCurrentSpaceConfig();
if (loadSuccess && currentSpaceConfig) {
return {
...currentSpaceConfig,
isEditable,
};
}
}
return {
...INITIAL_PERSONAL_SPACE_CONFIG,
const currentConfig = getCurrentSpaceConfig();

const config = useMemo(
() => ({
...(currentConfig ? currentConfig : INITIAL_PERSONAL_SPACE_CONFIG),
isEditable,
};
}, [spaceId, isEditable, loading, loadSuccess, fid]);
}),
[currentConfig, isEditable],
);

useEffect(() => {
if (isEditable && isNil(spaceId) && !isNil(currentUserFid)) {
registerSpace(currentUserFid, "profile").then((newSpaceId) =>
setSpaceId(newSpaceId || null),
);
registerSpace(currentUserFid, "profile").then((newSpaceId) => {
setSpaceId(newSpaceId || null);
});
}
}, [isEditable, spaceId, currentUserFid]);

const saveConfig = useCallback(
async (spaceConfig: SpaceConfig) => {
async (spaceConfig: SpaceConfigSaveDetails) => {
if (isNil(currentUserFid)) {
throw new Error("Attempted to save config when user is not signed in!");
}
if (isNil(spaceId)) {
throw new Error("Cannot save config until space is registered");
}
const saveableConfig: UpdatableSpaceConfig = {
const saveableConfig = {
...spaceConfig,
fidgetInstanceDatums: mapValues(
spaceConfig.fidgetInstanceDatums,
Expand Down
20 changes: 13 additions & 7 deletions src/common/components/templates/Space.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ export type SpaceConfig = {
theme: UserTheme;
};

export type SpaceConfigSaveDetails = Partial<
Omit<SpaceConfig, "layoutDetails">
> & {
layoutDetails?: Partial<LayoutFidgetDetails<LayoutFidgetConfig<any>>>;
};

type SpaceArgs = {
config: SpaceConfig;
saveConfig: (config: SpaceConfig) => Promise<void>;
saveConfig: (config: SpaceConfigSaveDetails) => Promise<void>;
commitConfig: () => Promise<void>;
resetConfig: () => Promise<void>;
profile?: ReactNode;
Expand Down Expand Up @@ -71,13 +77,13 @@ export default function Space({
layoutConfig,
fidgetInstanceDatums,
fidgetTrayContents,
}: LayoutFidgetSaveableConfig<LayoutFidgetConfig<any>>) {
}: Partial<LayoutFidgetSaveableConfig<LayoutFidgetConfig<any>>>) {
return saveConfig({
...config,
layoutDetails: {
...config.layoutDetails,
layoutConfig,
},
layoutDetails: layoutConfig
? {
layoutConfig,
}
: undefined,
theme,
fidgetInstanceDatums,
fidgetTrayContents,
Expand Down
8 changes: 4 additions & 4 deletions src/common/data/stores/app/accounts/identityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const identityStore = (
setCurrentIdentity: (publicKey: string) => {
set((draft) => {
draft.account.currentSpaceIdentityPublicKey = publicKey;
});
}, "setCurrentIdentity");
},
loadIdentitiesForWallet: async (wallet: Wallet) => {
// Load Identity + Nonce + Wallet address info from DB
Expand All @@ -190,7 +190,7 @@ export const identityStore = (
: [];
set((draft) => {
draft.account.walletIdentities[wallet.address] = walletIdentities;
});
}, "loadIdentitiesForWallet");
return walletIdentities;
},
getIdentitiesForWallet: (wallet: Wallet) => {
Expand Down Expand Up @@ -255,7 +255,7 @@ export const identityStore = (
preKeys: [],
associatedFids: [],
});
});
}, "decryptIdentityKeys");
},
createIdentityForWallet: async (
signMessage: SignMessageFunctionSignature,
Expand Down Expand Up @@ -307,7 +307,7 @@ export const identityStore = (
preKeys: [],
associatedFids: [],
});
});
}, "createIdentityForWallet");
return identityKeys.publicKey;
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/stores/app/accounts/prekeyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const prekeyStore = (
),
["timestamp"],
);
});
}, "addPreKeysToIdentity");
},
loadPreKeys: async () => {
const { data } = await axiosBackend.get<PreKeyResponse>(
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/stores/app/accounts/privyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const privyStore = (set: StoreSet<AppStore>): PrivyStore => ({
setPrivyUser: (user: PrivyUser) => {
set((draft) => {
draft.account.privyUser = user;
});
}, "setPrivyUser");
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import axios from "axios";
import { createClient } from "../../../database/supabase/clients/component";
import { homebasePath } from "@/constants/supabase";
import { SignedFile } from "@/common/lib/signedFiles";
import { debounce } from "lodash";
import { cloneDeep, debounce, isArray, mergeWith } from "lodash";
import stringify from "fast-json-stable-stringify";
import axiosBackend from "../../../api/backend";
import { SpaceConfig } from "@/common/components/templates/Space";
import {
SpaceConfig,
SpaceConfigSaveDetails,
} from "@/common/components/templates/Space";
import INITIAL_HOMEBASE_CONFIG from "@/constants/intialHomebase";
import {
analytics,
Expand All @@ -22,7 +25,7 @@ interface HomeBaseStoreState {
interface HomeBaseStoreActions {
loadHomebase: () => Promise<SpaceConfig>;
commitHomebaseToDatabase: () => Promise<void>;
saveHomebaseConfig: (config: SpaceConfig) => Promise<void>;
saveHomebaseConfig: (config: SpaceConfigSaveDetails) => Promise<void>;
resetHomebaseConfig: () => Promise<void>;
clearHomebase: () => void;
}
Expand Down Expand Up @@ -57,21 +60,23 @@ export const createHomeBaseStoreFunc = (
await get().account.decryptEncryptedSignedFile(fileData),
) as SpaceConfig;
set((draft) => {
draft.homebase.homebaseConfig = spaceConfig;
draft.homebase.remoteHomebaseConfig = spaceConfig;
});
draft.homebase.homebaseConfig = cloneDeep(spaceConfig);
draft.homebase.remoteHomebaseConfig = cloneDeep(spaceConfig);
}, "loadHomebase-found");
return spaceConfig;
} catch (e) {
set((draft) => {
draft.homebase.homebaseConfig = INITIAL_HOMEBASE_CONFIG;
draft.homebase.remoteHomebaseConfig = INITIAL_HOMEBASE_CONFIG;
});
return INITIAL_HOMEBASE_CONFIG;
draft.homebase.homebaseConfig = cloneDeep(INITIAL_HOMEBASE_CONFIG);
draft.homebase.remoteHomebaseConfig = cloneDeep(
INITIAL_HOMEBASE_CONFIG,
);
}, "loadHomebase-default");
return cloneDeep(INITIAL_HOMEBASE_CONFIG);
}
},
commitHomebaseToDatabase: async () => {
debounce(async () => {
const localCopy = get().homebase.homebaseConfig;
const localCopy = cloneDeep(get().homebase.homebaseConfig);
if (localCopy) {
const file = await get().account.createEncryptedSignedFile(
stringify(localCopy),
Expand All @@ -83,7 +88,7 @@ export const createHomeBaseStoreFunc = (
await axiosBackend.post(`/api/space/homebase/`, file);
set((draft) => {
draft.homebase.remoteHomebaseConfig = localCopy;
});
}, "commitHomebaseToDatabase");
analytics.track(AnalyticsEvent.SAVE_HOMEBASE_THEME);
} catch (e) {
console.error(e);
Expand All @@ -93,14 +98,23 @@ export const createHomeBaseStoreFunc = (
}, 1000)();
},
saveHomebaseConfig: async (config) => {
set((draft) => {
draft.homebase.homebaseConfig = config;
const localCopy = cloneDeep(get().homebase.homebaseConfig) as SpaceConfig;
mergeWith(localCopy, config, (_, newItem) => {
if (isArray(newItem)) return newItem;
});
set(
(draft) => {
draft.homebase.homebaseConfig = localCopy;
},
"saveHomebaseConfig",
false,
);
},
resetHomebaseConfig: async () => {
const remote = cloneDeep(get().homebase.remoteHomebaseConfig);
set((draft) => {
draft.homebase.homebaseConfig = draft.homebase.remoteHomebaseConfig;
});
draft.homebase.homebaseConfig = remote;
}, "resetHomebaseConfig");
},
clearHomebase: () => {
set(
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/stores/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
HomeBaseStore,
createHomeBaseStoreFunc,
partializedHomebaseStore,
} from "./homebase";
} from "./homebase/homebaseStore";
import {
createSpaceStoreFunc,
partializedSpaceStore,
Expand Down
Loading

0 comments on commit fcda7a4

Please sign in to comment.