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.

Fix project creation race condition (#1933)

Ivan Starkovae74469f103e
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 02140a8fa2..b5392c87cc 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -1,4 +1,5 @@
import { useCallback, useEffect } from "react";
+import { useUnmount } from "react-use";
import { type Publish, usePublish } from "~/shared/pubsub";
import type { Build } from "@webstudio-is/project-build";
import type { Project } from "@webstudio-is/project";
@@ -22,6 +23,7 @@ import {
} from "./features/workspace";
import { usePublishShortcuts } from "./shared/shortcuts";
import {
+ pagesStore,
projectStore,
useIsPreviewMode,
useSetAssets,
@@ -248,6 +250,10 @@ export const Builder = ({
useSetStyleSourceSelections(build.styleSourceSelections);
useSetInstances(build.instances);
+ useUnmount(() => {
+ pagesStore.set(undefined);
+ });
+
useSyncPageUrl();
useSetAssets(assets);
diff --git a/apps/builder/app/dashboard/projects/project-card.tsx b/apps/builder/app/dashboard/projects/project-card.tsx
index 2e2d6ecd42..a7055e33b7 100644
--- a/apps/builder/app/dashboard/projects/project-card.tsx
+++ b/apps/builder/app/dashboard/projects/project-card.tsx
@@ -7,6 +7,7 @@ import {
IconButton,
css,
Flex,
+ Grid,
Text,
theme,
Box,
@@ -203,10 +204,10 @@ export const ProjectCard = ({
tabIndex={0}
onKeyDown={handleKeyDown}
>
- <Flex className={thumbnailStyle()}>
+ <Grid className={thumbnailStyle()}>
<ThumbnailLink title={title} to={linkPath} ref={thumbnailRef} />
{isTransitioning && <Spinner />}
- </Flex>
+ </Grid>
<Flex
justify="between"
diff --git a/apps/builder/app/dashboard/projects/project-dialogs.tsx b/apps/builder/app/dashboard/projects/project-dialogs.tsx
index a9495e2428..476e2776e9 100644
--- a/apps/builder/app/dashboard/projects/project-dialogs.tsx
+++ b/apps/builder/app/dashboard/projects/project-dialogs.tsx
@@ -98,12 +98,20 @@ const useCreateProject = () => {
const { send, data, state } = trpc.create.useMutation();
const [errors, setErrors] = useState<string>();
+ // For unknown reason React sometimes skips `useEffect(, [data])` (some race condition)
+ // This fixes that race condition.
+ const [projectId, setProjectId] = useState<DashboardProject["id"]>();
+
+ if (projectId !== data?.id) {
+ setProjectId(data?.id);
+ }
+
useEffect(() => {
- if (data === undefined) {
+ if (projectId === undefined) {
return;
}
- navigate(builderPath({ projectId: data.id }));
- }, [data, navigate]);
+ navigate(builderPath({ projectId }));
+ }, [projectId, navigate]);
const handleSubmit = ({ title }: { title: string }) => {
const parsed = Title.safeParse(title);