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.

validate breakpoints when loading canvas data

Oleg Isonen3c3c4d910772
diff --git a/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts b/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts
index 4421327cb3..d8d91f2fca 100644
--- a/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts
+++ b/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts
@@ -1,3 +1,4 @@
+import { useSubscribe } from "@webstudio-is/sdk";
import { useCallback, useEffect } from "react";
import {
useSelectedBreakpoint,
@@ -31,6 +32,8 @@ export const useUpdateCanvasWidth = () => {
setCanvasWidth(Math.max(selectedBreakpoint.minWidth, minWidth));
}, [selectedBreakpoint, setCanvasWidth]);
+ useSubscribe("canvasWidth", setCanvasWidth);
+
// Set the initial canvas width based on the selected breakpoint upper bound, which starts where the next breakpoint begins.
return useCallback(
(iframe: HTMLIFrameElement | null) => {
diff --git a/apps/designer/app/routes/canvas/$projectId.tsx b/apps/designer/app/routes/canvas/$projectId.tsx
index d10b9f2d81..66354931fb 100644
--- a/apps/designer/app/routes/canvas/$projectId.tsx
+++ b/apps/designer/app/routes/canvas/$projectId.tsx
@@ -3,12 +3,11 @@ import type { LoaderFunction } from "@remix-run/node";
import { Canvas } from "~/canvas";
import { loadCanvasData, type ErrorData, type CanvasData } from "~/shared/db";
import env, { Env } from "~/env.server";
+import { ErrorMessage } from "~/shared/error";
-type LoaderReturnTypes = Promise<
- (CanvasData & { env: Env }) | (ErrorData & { env: Env })
->;
+type Data = (CanvasData | ErrorData) & { env: Env };
-export const loader: LoaderFunction = async ({ params }): LoaderReturnTypes => {
+export const loader: LoaderFunction = async ({ params }): Promise<Data> => {
if (params.projectId === undefined) {
return { errors: "Missing projectId", env };
}
@@ -30,10 +29,9 @@ export const loader: LoaderFunction = async ({ params }): LoaderReturnTypes => {
};
const CanvasRoute = () => {
- const data = useLoaderData<CanvasData | ErrorData>();
- // @todo how should we treat this kind of errors?
+ const data = useLoaderData<Data>();
if ("errors" in data) {
- return <p>{data.errors}</p>;
+ return <ErrorMessage message={data.errors} />;
}
return <Canvas data={data} />;
};
diff --git a/apps/designer/app/shared/db/breakpoints.server.ts b/apps/designer/app/shared/db/breakpoints.server.ts
index b2e9c10b15..21107c1807 100644
--- a/apps/designer/app/shared/db/breakpoints.server.ts
+++ b/apps/designer/app/shared/db/breakpoints.server.ts
@@ -3,6 +3,7 @@ import {
type Project,
type Tree,
type Breakpoint,
+ schema,
} from "@webstudio-is/sdk";
import ObjectId from "bson-objectid";
import { applyPatches, type Patch } from "immer";
@@ -20,10 +21,11 @@ export const load = async (treeId?: Tree["id"]) => {
if (breakpoint === null) {
throw new Error("Breakpoint not found");
}
-
+ const values = JSON.parse(breakpoint.values);
+ schema.Breakpoints.parse(values);
return {
...breakpoint,
- values: JSON.parse(breakpoint.values),
+ values,
};
};
@@ -35,16 +37,18 @@ export const getBreakpointsWithId = () =>
export const create = async (
treeId: Project["id"],
- breakpoints: Array<Breakpoint>
+ values: Array<Breakpoint>
) => {
+ schema.Breakpoints.parse(values);
+
const data = {
treeId,
- values: JSON.stringify(breakpoints),
+ values: JSON.stringify(values),
};
await prisma.breakpoints.create({ data });
return {
...data,
- breakpoints,
+ breakpoints: values,
};
};
@@ -68,9 +72,12 @@ export const patch = async (
) => {
const breakpoints = await load(treeId);
if (breakpoints === null) return;
- const nextBreakpoints = applyPatches(breakpoints.values, patches);
+ const nextValues = applyPatches(breakpoints.values, patches);
+
+ schema.Breakpoints.parse(nextValues);
+
await prisma.breakpoints.update({
where: { treeId },
- data: { values: nextBreakpoints },
+ data: { values: JSON.stringify(nextValues) },
});
};
diff --git a/apps/designer/app/shared/error/error-message.tsx b/apps/designer/app/shared/error/error-message.tsx
new file mode 100644
index 0000000000..d715dd0afb
--- /dev/null
+++ b/apps/designer/app/shared/error/error-message.tsx
@@ -0,0 +1,14 @@
+import { publish } from "@webstudio-is/sdk";
+import { useEffect } from "react";
+
+export const ErrorMessage = ({ message }: { message: string }) => {
+ useEffect(() => {
+ publish({ type: "canvasWidth", payload: 600 });
+ }, []);
+ return (
+ <div>
+ <h1>Error</h1>
+ <pre>{message}</pre>
+ </div>
+ );
+};
diff --git a/apps/designer/app/shared/error/index.ts b/apps/designer/app/shared/error/index.ts
new file mode 100644
index 0000000000..b321e84cf6
--- /dev/null
+++ b/apps/designer/app/shared/error/index.ts
@@ -0,0 +1 @@
+export * from "./error-message";
diff --git a/apps/designer/package.json b/apps/designer/package.json
index c37cd69faf..1a574971d7 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -135,6 +135,6 @@
"sideEffects": false,
"license": "MIT",
"relativeDependencies": {
- "@webstudio-is/sdk": "../webstudio-sdk/"
+ "@webstudio-is/sdk": "../../../webstudio-sdk/"
}
}