Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

use useMount instead of sync once (#1937)

Ivan Starkov97f0cc00d76d
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index 095f597a15..76f2675d35 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -40,12 +40,12 @@ import {
} from "~/shared/nano-states";
import { useDragAndDrop } from "./shared/use-drag-drop";
import { useCopyPaste } from "~/shared/copy-paste";
-import { useSyncInitializeOnce } from "~/shared/hook-utils";
import { setDataCollapsed, subscribeCollapsedToPubSub } from "./collapsed";
import { useWindowResizeDebounced } from "~/shared/dom-hooks";
import { subscribeInstanceSelection } from "./instance-selection";
import { subscribeInstanceHovering } from "./instance-hovering";
import { useHashLinkSync } from "~/shared/pages";
+import { useMount } from "~/shared/hook-utils/use-mount";
registerContainers();
@@ -167,7 +167,7 @@ export const Canvas = ({ params }: CanvasProps): JSX.Element | null => {
const components = new Map(
Object.entries({ ...baseComponents, ...remixComponents })
) as Components;
- useSyncInitializeOnce(() => {
+ useMount(() => {
registerComponentMetas(baseComponentMetas);
registerComponentPropsMetas(baseComponentPropsMetas);
registerComponentMetas(remixComponentMetas);
diff --git a/apps/builder/app/shared/hook-utils/index.ts b/apps/builder/app/shared/hook-utils/index.ts
deleted file mode 100644
index 8241cea1ae..0000000000
--- a/apps/builder/app/shared/hook-utils/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-sync-initialize-once";
diff --git a/apps/builder/app/shared/hook-utils/use-mount.ts b/apps/builder/app/shared/hook-utils/use-mount.ts
new file mode 100644
index 0000000000..ff1eb0a52c
--- /dev/null
+++ b/apps/builder/app/shared/hook-utils/use-mount.ts
@@ -0,0 +1,6 @@
+import { type EffectCallback, useEffect } from "react";
+
+export const useMount = (effect: EffectCallback) => {
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ useEffect(effect, []);
+};
diff --git a/apps/builder/app/shared/hook-utils/use-sync-initialize-once.ts b/apps/builder/app/shared/hook-utils/use-sync-initialize-once.ts
deleted file mode 100644
index fb374ce621..0000000000
--- a/apps/builder/app/shared/hook-utils/use-sync-initialize-once.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { useEffect, useRef } from "react";
-
-// This hook allows to
-// 1. run a function only once on the client
-// Can't use useState initializer because it can be called twice in strict mode and also isn't designed for this.
-export const useSyncInitializeOnce = (fn: () => void) => {
- const ref = useRef(false);
-
- useEffect(() => {
- if (ref.current) {
- return;
- }
- ref.current = true;
-
- fn();
- }, [fn]);
-};
diff --git a/apps/builder/app/shared/nano-states/instances.ts b/apps/builder/app/shared/nano-states/instances.ts
index 78091d1980..95068af79c 100644
--- a/apps/builder/app/shared/nano-states/instances.ts
+++ b/apps/builder/app/shared/nano-states/instances.ts
@@ -1,8 +1,8 @@
import { atom, computed } from "nanostores";
import type { Instance, Instances } from "@webstudio-is/project-build";
import type { InstanceSelector } from "../tree-utils";
-import { useSyncInitializeOnce } from "../hook-utils";
import type { ItemId } from "node_modules/@webstudio-is/design-system/src/components/tree/item-utils";
+import { useMount } from "~/shared/hook-utils/use-mount";
export const isResizingCanvasStore = atom(false);
@@ -18,7 +18,7 @@ export const textEditingInstanceSelectorStore = atom<
export const instancesStore = atom<Instances>(new Map());
export const useSetInstances = (instances: [Instance["id"], Instance][]) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
instancesStore.set(new Map(instances));
});
};
diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.ts
index 246ed4e71b..76dbe1074f 100644
--- a/apps/builder/app/shared/nano-states/nano-states.ts
+++ b/apps/builder/app/shared/nano-states/nano-states.ts
@@ -27,7 +27,7 @@ import {
} from "@webstudio-is/react-sdk";
import type { Style } from "@webstudio-is/css-data";
import type { DragStartPayload } from "~/canvas/shared/use-drag-drop";
-import { useSyncInitializeOnce } from "../hook-utils";
+import { useMount } from "~/shared/hook-utils/use-mount";
import { shallowComputed } from "../store-utils";
import { type InstanceSelector } from "../tree-utils";
import type { htmlTags as HtmlTags } from "html-tags";
@@ -73,7 +73,7 @@ export const dataSourceVariablesStore = atom<Map<DataSource["id"], unknown>>(
export const useSetDataSources = (
dataSources: [DataSource["id"], DataSource][]
) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
dataSourcesStore.set(new Map(dataSources));
});
};
@@ -131,7 +131,7 @@ export const propsIndexStore = computed(propsStore, (props) => {
};
});
export const useSetProps = (props: [Prop["id"], Prop][]) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
propsStore.set(new Map(props));
});
};
@@ -139,7 +139,7 @@ export const useSetProps = (props: [Prop["id"], Prop][]) => {
export const stylesStore = atom<Styles>(new Map());
export const useSetStyles = (styles: [StyleDeclKey, StyleDecl][]) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
stylesStore.set(new Map(styles));
});
};
@@ -178,7 +178,7 @@ export const availableStyleSourcesStore = shallowComputed(
export const useSetStyleSources = (
styleSources: [StyleSource["id"], StyleSource][]
) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
styleSourcesStore.set(new Map(styleSources));
});
};
@@ -189,7 +189,7 @@ export const styleSourceSelectionsStore = atom<StyleSourceSelections>(
export const useSetStyleSourceSelections = (
styleSourceSelections: [Instance["id"], StyleSourceSelection][]
) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
styleSourceSelectionsStore.set(new Map(styleSourceSelections));
});
};
@@ -248,14 +248,14 @@ export const stylesIndexStore = computed(
export const useSetBreakpoints = (
breakpoints: [Breakpoint["id"], Breakpoint][]
) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
breakpointsStore.set(new Map(breakpoints));
});
};
export const assetsStore = atom<Assets>(new Map());
export const useSetAssets = (assets: [Asset["id"], Asset][]) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
assetsStore.set(new Map(assets));
});
};
@@ -392,7 +392,7 @@ export const useIsPreviewMode = () => useValue(isPreviewModeStore);
const authPermitStore = atom<AuthPermit>("view");
export const useAuthPermit = () => useValue(authPermitStore);
export const useSetAuthPermit = (authPermit: AuthPermit) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
authPermitStore.set(authPermit);
});
};
@@ -400,7 +400,7 @@ export const useSetAuthPermit = (authPermit: AuthPermit) => {
export const authTokenStore = atom<string | undefined>(undefined);
export const useAuthToken = () => useValue(authTokenStore);
export const useSetAuthToken = (authToken: string | undefined) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
authTokenStore.set(authToken);
});
};
diff --git a/apps/builder/app/shared/nano-states/pages.ts b/apps/builder/app/shared/nano-states/pages.ts
index 2fbdb511ca..4e0cb68b8b 100644
--- a/apps/builder/app/shared/nano-states/pages.ts
+++ b/apps/builder/app/shared/nano-states/pages.ts
@@ -1,10 +1,10 @@
import { atom, computed } from "nanostores";
import type { Page, Pages } from "@webstudio-is/project-build";
-import { useSyncInitializeOnce } from "../hook-utils";
+import { useMount } from "~/shared/hook-utils/use-mount";
export const pagesStore = atom<undefined | Pages>(undefined);
export const useSetPages = (pages: Pages) => {
- useSyncInitializeOnce(() => {
+ useMount(() => {
pagesStore.set(pages);
});
};
diff --git a/apps/builder/app/shared/pages/use-switch-page.ts b/apps/builder/app/shared/pages/use-switch-page.ts
index 21cefd21a1..fba6a7c88b 100644
--- a/apps/builder/app/shared/pages/use-switch-page.ts
+++ b/apps/builder/app/shared/pages/use-switch-page.ts
@@ -2,6 +2,7 @@ import { useStore } from "@nanostores/react";
import { useNavigate } from "@remix-run/react";
import { findPageByIdOrPath, type Page } from "@webstudio-is/project-build";
import { useEffect } from "react";
+import { useMount } from "~/shared/hook-utils/use-mount";
import {
authTokenStore,
pagesStore,
@@ -13,7 +14,6 @@ import {
isPreviewModeStore,
} from "~/shared/nano-states";
import { builderPath } from "~/shared/router-utils";
-import { useSyncInitializeOnce } from "../hook-utils";
export const switchPage = (pageId?: Page["id"], pageHash?: string) => {
const pages = pagesStore.get();
@@ -59,7 +59,7 @@ export const useSyncPageUrl = () => {
const isPreviewMode = useStore(isPreviewModeStore);
// Get pageId and pageHash from URL
- useSyncInitializeOnce(() => {
+ useMount(() => {
setPageStateFromUrl();
});