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.

move shared code to a better location

Roman Pominovf066779e440d
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx b/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
index 5f55fd0ce9..a5ea6dee2a 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
@@ -7,7 +7,6 @@ import {
useId,
styled,
Flex,
- toastNonFieldErrors,
InputErrorsTooltip,
} from "@webstudio-is/design-system";
import { useFetcher, type Fetcher } from "@remix-run/react";
@@ -20,6 +19,16 @@ import { type Page } from "@webstudio-is/project";
import { usePages } from "~/designer/shared/nano-states";
import { useDebounce, useUnmount } from "react-use";
import { useOnFetchEnd, usePersistentFetcher } from "~/shared/fetcher";
+import {
+ type FetcherData,
+ normalizeErrors,
+ toastNonFieldErrors,
+} from "~/shared/form-utils";
+import type {
+ DeletePageData,
+ EditPageData,
+ CreatePageData,
+} from "~/shared/pages";
const Group = styled(Flex, {
marginBottom: "$3",
@@ -27,17 +36,6 @@ const Group = styled(Flex, {
defaultVariants: { direction: "column" },
});
-type FetcherData<Payload> =
- | ({ ok: true } & Payload)
- | { errors: string | ZodError["formErrors"] };
-
-const normalizeErrors = (
- errors: string | ZodError["formErrors"]
-): ZodError["formErrors"] =>
- typeof errors === "string"
- ? { formErrors: [errors], fieldErrors: {} }
- : errors;
-
type EditablePage = Omit<Page, "treeId" | "id">;
const FormFields = ({
@@ -118,8 +116,6 @@ const FormFields = ({
);
};
-export type CreatePageData = FetcherData<{ page: Page }>;
-
export const NewPageSettings = ({
onClose,
onSuccess,
@@ -165,12 +161,6 @@ export const NewPageSettings = ({
);
};
-// eslint-disable-next-line @typescript-eslint/ban-types
-export type EditPageData = FetcherData<{}>;
-
-// eslint-disable-next-line @typescript-eslint/ban-types
-export type DeletePageData = FetcherData<{}>;
-
const toFormData = (page: Partial<EditablePage> & { id: string }): FormData => {
const formData = new FormData();
for (const [key, value] of Object.entries(page)) {
diff --git a/apps/designer/app/routes/rest/pages.$projectId.ts b/apps/designer/app/routes/rest/pages.$projectId.ts
index 1b83b04c4b..6fc79900c9 100644
--- a/apps/designer/app/routes/rest/pages.$projectId.ts
+++ b/apps/designer/app/routes/rest/pages.$projectId.ts
@@ -3,12 +3,12 @@ import { db } from "@webstudio-is/project/index.server";
import { type Pages, utils } from "@webstudio-is/project";
import { zfd } from "zod-form-data";
import { z } from "zod";
-import {
+import { sentryException } from "~/shared/sentry";
+import type {
CreatePageData,
DeletePageData,
EditPageData,
-} from "~/designer/features/sidebar-left/panels/pages/settings";
-import { sentryException } from "~/shared/sentry";
+} from "~/shared/pages";
const nonEmptyString = z
.string({
diff --git a/apps/designer/app/shared/form-utils/index.ts b/apps/designer/app/shared/form-utils/index.ts
new file mode 100644
index 0000000000..99455b8180
--- /dev/null
+++ b/apps/designer/app/shared/form-utils/index.ts
@@ -0,0 +1 @@
+export * from "./validation";
diff --git a/apps/designer/app/shared/form-utils/validation.ts b/apps/designer/app/shared/form-utils/validation.ts
new file mode 100644
index 0000000000..d797129823
--- /dev/null
+++ b/apps/designer/app/shared/form-utils/validation.ts
@@ -0,0 +1,34 @@
+import { toast } from "@webstudio-is/design-system";
+import type { ZodError } from "zod";
+
+export type FetcherData<Payload> =
+ | ({ ok: true } & Payload)
+ | { errors: string | ZodError["formErrors"] };
+
+export const normalizeErrors = (
+ errors: string | ZodError["formErrors"]
+): ZodError["formErrors"] =>
+ typeof errors === "string"
+ ? { formErrors: [errors], fieldErrors: {} }
+ : errors;
+
+// Show a toast for each of formErrors
+// as well as fieldErrors which we cannot display near a corresponding field
+export const toastNonFieldErrors = (
+ errors: ZodError["formErrors"],
+ knownFields?: string[]
+) => {
+ for (const message of errors.formErrors) {
+ toast.error(message);
+ }
+
+ if (knownFields !== undefined) {
+ for (const fieldName in errors.fieldErrors) {
+ if (knownFields.includes(fieldName) === false) {
+ for (const message of errors.fieldErrors[fieldName] as string[]) {
+ toast.error(`${fieldName}: ${message}`);
+ }
+ }
+ }
+ }
+};
diff --git a/apps/designer/app/shared/pages/index.ts b/apps/designer/app/shared/pages/index.ts
new file mode 100644
index 0000000000..eea524d655
--- /dev/null
+++ b/apps/designer/app/shared/pages/index.ts
@@ -0,0 +1 @@
+export * from "./types";
diff --git a/apps/designer/app/shared/pages/types.ts b/apps/designer/app/shared/pages/types.ts
new file mode 100644
index 0000000000..f73a1d5267
--- /dev/null
+++ b/apps/designer/app/shared/pages/types.ts
@@ -0,0 +1,10 @@
+import { type Page } from "@webstudio-is/project";
+import { FetcherData } from "~/shared/form-utils";
+
+export type CreatePageData = FetcherData<{ page: Page }>;
+
+// eslint-disable-next-line @typescript-eslint/ban-types
+export type EditPageData = FetcherData<{}>;
+
+// eslint-disable-next-line @typescript-eslint/ban-types
+export type DeletePageData = FetcherData<{}>;
diff --git a/packages/design-system/src/components/toast.tsx b/packages/design-system/src/components/toast.tsx
index a423369d62..0cef44d2ce 100644
--- a/packages/design-system/src/components/toast.tsx
+++ b/packages/design-system/src/components/toast.tsx
@@ -1,6 +1,5 @@
import * as ToastPrimitive from "@radix-ui/react-toast";
import toast, { useToaster } from "react-hot-toast/headless";
-import type { ZodError } from "zod";
import { keyframes, styled } from "../stitches.config";
import {
CheckCircledIcon,
@@ -131,22 +130,3 @@ export const Toaster = () => {
};
export { toast };
-
-export const toastNonFieldErrors = (
- errors: ZodError["formErrors"],
- knownFields?: string[]
-) => {
- for (const message of errors.formErrors) {
- toast.error(message);
- }
-
- if (knownFields !== undefined) {
- for (const fieldName in errors.fieldErrors) {
- if (knownFields.includes(fieldName) === false) {
- for (const message of errors.fieldErrors[fieldName] as string[]) {
- toast.error(`${fieldName}: ${message}`);
- }
- }
- }
- }
-};