Skip to content

Commit

Permalink
feat: swap to use merge to update spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hiporox committed Jul 3, 2024
1 parent da47276 commit 78fe58f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 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
7 changes: 3 additions & 4 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 { SpaceConfig, SpaceConfigSaveDetails } from "../templates/Space";
import Profile from "@/fidgets/ui/profile";
import SpacePage from "./SpacePage";

Expand Down Expand Up @@ -129,14 +128,14 @@ export default function UserDefinedSpace({
}, [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
14 changes: 10 additions & 4 deletions src/common/data/stores/app/homebase/index.ts
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 { debounce, merge } 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 @@ -94,7 +97,10 @@ export const createHomeBaseStoreFunc = (
},
saveHomebaseConfig: async (config) => {
set((draft) => {
draft.homebase.homebaseConfig = config;
draft.homebase.homebaseConfig = merge(
get().homebase.homebaseConfig,
config,
);
});
},
resetHomebaseConfig: async () => {
Expand Down
37 changes: 29 additions & 8 deletions src/common/data/stores/app/space/spaceStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { SpaceConfig } from "@/common/components/templates/Space";
import {
SpaceConfig,
SpaceConfigSaveDetails,
} from "@/common/components/templates/Space";
import { AppStore } from "..";
import { FidgetConfig, FidgetInstanceData } from "@/common/fidgets";
import { StoreGet, StoreSet } from "../../createStore";
Expand All @@ -8,7 +11,7 @@ import {
RegisterNewSpaceResponse,
SpaceRegistration,
} from "@/pages/api/space/registry";
import { debounce, fromPairs, isUndefined, map } from "lodash";
import { debounce, fromPairs, isUndefined, map, merge } from "lodash";
import {
NameChangeRequest,
UpdateSpaceResponse,
Expand All @@ -29,7 +32,7 @@ type SpaceId = string;
// But a space that is saved in the DB doesn't store
// Fidget data or editablity
// So we rebuild the details, but without those fields
export type SaveableSpaceConfig = Omit<
export type DatabaseWritableSpaceConfig = Omit<
SpaceConfig,
"fidgetInstanceDatums" | "isEditable"
> & {
Expand All @@ -40,7 +43,22 @@ export type SaveableSpaceConfig = Omit<
};
};

export type UpdatableSpaceConfig = SaveableSpaceConfig & {
export type DatabaseWritableSpaceSaveConfig = Partial<
Omit<SpaceConfigSaveDetails, "fidgetInstanceDatums" | "isEditable">
> & {
fidgetInstanceDatums: {
[key: string]: Omit<FidgetInstanceData, "config"> & {
config: Omit<FidgetConfig, "data">;
};
};
};

export type UpdatableDatabaseWritableSpaceSaveConfig =
DatabaseWritableSpaceSaveConfig & {
isPrivate?: boolean;
};

export type UpdatableSpaceConfig = DatabaseWritableSpaceConfig & {
isPrivate: boolean;
};

Expand All @@ -65,7 +83,7 @@ interface SpaceActions {
commitSpaceToDatabase: (spaceId: string) => Promise<void>;
saveLocalSpace: (
spaceId: string,
config: UpdatableSpaceConfig,
config: UpdatableDatabaseWritableSpaceSaveConfig,
) => Promise<void>;
clear: () => void;
}
Expand Down Expand Up @@ -101,7 +119,7 @@ export const createSpaceStoreFunc = (
const fileData = JSON.parse(await data.text()) as SignedFile;
const spaceConfig = JSON.parse(
await get().account.decryptEncryptedSignedFile(fileData),
) as SaveableSpaceConfig;
) as DatabaseWritableSpaceConfig;
const updatableSpaceConfig = {
...spaceConfig,
isPrivate: fileData.isEncrypted,
Expand Down Expand Up @@ -234,9 +252,12 @@ export const createSpaceStoreFunc = (
}
}, 1000)();
},
saveLocalSpace: async (spaceId, config) => {
saveLocalSpace: async (spaceId, changedConfig) => {
set((draft) => {
draft.space.localSpaces[spaceId] = config;
draft.space.localSpaces[spaceId] = merge(
get().space.localSpaces[spaceId],
changedConfig,
);
});
},
clear: () => {
Expand Down

0 comments on commit 78fe58f

Please sign in to comment.