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 tree id in _id of breakpoints

Oleg Isonena0454ed4309e
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsx
index 0daac8f7c9..4d78b0352e 100644
--- a/app/canvas/canvas.tsx
+++ b/app/canvas/canvas.tsx
@@ -114,6 +114,9 @@ type CanvasProps = {
};
export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
+ if (data.tree === null) {
+ throw new Error("Tree is null");
+ }
useInitializeBreakpoints(data.breakpoints);
globalStyles();
useAllUserProps(data.props);
diff --git a/app/routes/index.tsx b/app/routes/index.tsx
index 585a791bb9..253d971f05 100644
--- a/app/routes/index.tsx
+++ b/app/routes/index.tsx
@@ -33,9 +33,16 @@ export const loader: LoaderFunction = async ({
const [tree, props, breakpoints] = await Promise.all([
db.tree.loadByProject(project, "production"),
db.props.loadByTreeId(project.prodTreeId),
- db.breakpoints.load(project.id),
+ // @todo copy breakpoints using prod tree id and then switch
+ db.breakpoints.load(project.devTreeId),
]);
- return { tree, props, breakpoints: breakpoints?.values ?? [] };
+ if (tree === null) {
+ throw new Error(`Tree ${project.prodTreeId} not found`);
+ }
+ if (breakpoints === null) {
+ throw new Error(`Breakpoints for tree ${project.prodTreeId} not found`);
+ }
+ return { tree, props, breakpoints: breakpoints.values };
} catch (error) {
if (error instanceof Error) {
return { errors: error.message };
diff --git a/app/routes/rest/breakpoints.$projectId.ts b/app/routes/rest/breakpoints.$projectId.ts
index 9cd0dea7e2..1f1a6ef8e6 100644
--- a/app/routes/rest/breakpoints.$projectId.ts
+++ b/app/routes/rest/breakpoints.$projectId.ts
@@ -10,8 +10,22 @@ export const loader: LoaderFunction = async ({
params,
}): Promise<Array<Breakpoint> | ErrorData> => {
try {
- const data = await db.breakpoints.load(params.projectId);
- return data?.values || [];
+ const project = await db.project.loadById(params.projectId);
+ if (project === null) {
+ throw new Error(`Project ${params.projectId} not found`);
+ }
+ if (project.prodTreeId === null) {
+ throw new Error(
+ `Project ${params.projectId} needs to be published first`
+ );
+ }
+ const data = await db.breakpoints.load(project.prodTreeId);
+ if (data === null) {
+ throw new Error(
+ `Breakpoints not found for project ${params.projectId} and tree ID ${project.prodTreeId}`
+ );
+ }
+ return data.values;
} catch (error) {
if (error instanceof Error) {
return {
diff --git a/app/routes/rest/tree.$projectId.ts b/app/routes/rest/tree.$projectId.ts
index fc8d900b77..b84260fe98 100644
--- a/app/routes/rest/tree.$projectId.ts
+++ b/app/routes/rest/tree.$projectId.ts
@@ -8,7 +8,7 @@ export type ErrorData = {
export const loader: LoaderFunction = async ({
params,
-}): Promise<Tree | ErrorData> => {
+}): Promise<Tree | null | ErrorData> => {
try {
const project = await db.project.loadById(params.projectId);
return await db.tree.loadByProject(project, "production");
diff --git a/app/shared/db/breakpoints.server.ts b/app/shared/db/breakpoints.server.ts
index 8d455c18f1..ce44b54005 100644
--- a/app/shared/db/breakpoints.server.ts
+++ b/app/shared/db/breakpoints.server.ts
@@ -8,13 +8,13 @@ import ObjectId from "bson-objectid";
import { applyPatches, type Patch } from "immer";
import { prisma } from "./prisma.server";
-export const load = async (projectId?: Project["id"]) => {
- if (typeof projectId !== "string") {
- throw new Error("Project ID required");
+export const load = async (treeId?: Tree["id"]) => {
+ if (typeof treeId !== "string") {
+ throw new Error("Tree ID required");
}
return await prisma.breakpoints.findUnique({
- where: { projectId },
+ where: { treeId },
});
};
@@ -25,11 +25,11 @@ export const getBreakpointsWithId = () =>
}));
export const create = async (
- projectId: Project["id"],
+ treeId: Project["id"],
breakpoints: Array<Breakpoint>
) => {
const data = {
- projectId,
+ treeId,
values: breakpoints,
};
await prisma.breakpoints.create({ data });
@@ -37,14 +37,14 @@ export const create = async (
};
export const patch = async (
- { projectId }: { treeId: Tree["id"]; projectId: Project["id"] },
+ { treeId }: { treeId: Tree["id"]; projectId: Project["id"] },
patches: Array<Patch>
) => {
- const breakpoints = await load(projectId);
+ const breakpoints = await load(treeId);
if (breakpoints === null) return;
const nextBreakpoints = applyPatches(breakpoints.values, patches);
await prisma.breakpoints.update({
- where: { projectId: projectId },
+ where: { treeId },
data: { values: nextBreakpoints },
});
};
diff --git a/app/shared/db/canvas.server.ts b/app/shared/db/canvas.server.ts
index 8d642c5211..ec4b1aa00a 100644
--- a/app/shared/db/canvas.server.ts
+++ b/app/shared/db/canvas.server.ts
@@ -8,17 +8,27 @@ export type ErrorData = {
};
const loadData = async (projectId: Project["id"]) => {
- const [breakpoints, project] = await Promise.all([
- db.breakpoints.load(projectId),
- db.project.loadById(projectId),
- ]);
+ const project = await db.project.loadById(projectId);
+
if (project === null) throw new Error(`Project "${projectId}" not found`);
- const [tree, props] = await Promise.all([
+ const [tree, props, breakpoints] = await Promise.all([
db.tree.loadByProject(project, "development"),
db.props.loadByProject(project, "development"),
+ db.breakpoints.load(project.devTreeId),
]);
- return { tree, props, project, breakpoints: breakpoints?.values || [] };
+
+ if (tree === null) {
+ throw new Error(
+ `Tree ${project.devTreeId} not found for project ${projectId}`
+ );
+ }
+
+ if (breakpoints === null) {
+ throw new Error(`Breakpoints not found for project ${projectId}`);
+ }
+
+ return { tree, props, project, breakpoints: breakpoints.values };
};
export const loadCanvasData = async ({
diff --git a/app/shared/db/project.server.ts b/app/shared/db/project.server.ts
index a80880e7e1..fa6423c2aa 100644
--- a/app/shared/db/project.server.ts
+++ b/app/shared/db/project.server.ts
@@ -63,7 +63,7 @@ export const create = async ({
devTreeId: tree.id,
},
});
- await db.breakpoints.create(project.id, breakpoints);
+ await db.breakpoints.create(tree.id, breakpoints);
return project;
};
diff --git a/app/shared/db/tree.server.ts b/app/shared/db/tree.server.ts
index 1a8d7eb117..22aa2b523a 100644
--- a/app/shared/db/tree.server.ts
+++ b/app/shared/db/tree.server.ts
@@ -66,9 +66,9 @@ export const create = async (root: Instance): Promise<Tree> => {
})) as Tree;
};
-export const loadById = async (id: string): Promise<Tree> => {
+export const loadById = async (treeId: string): Promise<Tree | null> => {
return (await prisma.tree.findUnique({
- where: { id },
+ where: { id: treeId },
})) as Tree;
};
@@ -89,8 +89,11 @@ export const loadByProject = async (
return await loadById(treeId);
};
-export const clone = async (id: string) => {
- const tree = await loadById(id);
+export const clone = async (treeId: string) => {
+ const tree = await loadById(treeId);
+ if (tree === null) {
+ throw new Error(`Tree ${treeId} not found`);
+ }
return await create(tree.root);
};
@@ -99,6 +102,9 @@ export const patchRoot = async (
patches: Array<Patch>
) => {
const tree = await loadById(treeId);
+ if (tree === null) {
+ throw new Error(`Tree ${treeId} not found`);
+ }
const root = applyPatches(tree.root, patches);
await prisma.tree.update({
data: { root },
diff --git a/mongo-migrations/published-breakpoints.js b/mongo-migrations/published-breakpoints.js
new file mode 100644
index 0000000000..8af778a63d
--- /dev/null
+++ b/mongo-migrations/published-breakpoints.js
@@ -0,0 +1,24 @@
+// I am running this migration manually over console,
+// because we don't have an automated setup for migrations yet.
+
+let migrateBreakpoints = () => {
+ const migrate = (project) => {
+ print(`updating breakpoints for ${project._id}`);
+ try {
+ const breakpoint = db.Breakpoints.findOne({ _id: project._id });
+ breakpoint._id = project.devTreeId;
+ db.Breakpoints.insertOne(breakpoint);
+ db.Breakpoints.remove({ _id: project._id }, true);
+ } catch (err) {
+ print(err);
+ }
+ };
+
+ const cursor = db.Project.find();
+
+ while (cursor.hasNext()) {
+ migrate(cursor.next());
+ }
+};
+
+migrateBreakpoints();
diff --git a/package.json b/package.json
index 5f9b90b390..1fa6cc219b 100644
--- a/package.json
+++ b/package.json
@@ -42,7 +42,7 @@
"@remix-run/serve": "^1.2.3",
"@remix-run/vercel": "^1.2.3",
"@stitches/react": "^1.2.7",
- "@webstudio-is/sdk": "^0.2.0",
+ "@webstudio-is/sdk": "^0.2.1",
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"hyphenate-style-name": "^1.0.4",
diff --git a/yarn.lock b/yarn.lock
index 17b142fe3b..fecd02c54c 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1814,10 +1814,10 @@
dependencies:
web-streams-polyfill "^3.1.1"
-"@webstudio-is/sdk@^0.2.0":
- version "0.2.0"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.2.0.tgz#8dfdac45b6700af812b491de089009c15e57d0e2"
- integrity sha512-6ZPGJYOcWrVlZKBJSg4IJJoQB5Lpz4NsACfAMxkYhAQ9lbHcZvG32fpJclymPe3Dug6MyUPUFZ4IDVYaB1LLZQ==
+"@webstudio-is/sdk@^0.2.1":
+ version "0.2.1"
+ resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.2.1.tgz#9b309add218a2ee19e946039b524fd5062bdec7c"
+ integrity sha512-fIdcLuaaH7xYJ/eIXbC6N8otBt5uB8pw3UaSOy4xh3uyw29NYz0j9Wp8NKkG6pLVNfBEP+45HUyl1z3n3nbFrA==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/core" "^1.2.7"