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.

Dashboard remaining dialogs (#907)

Oleg Isonen27a00fdc79c5
diff --git a/apps/designer/app/dashboard/projects/create-project.tsx b/apps/designer/app/dashboard/projects/create-project.tsx
deleted file mode 100644
index 7a59c42aa7..0000000000
--- a/apps/designer/app/dashboard/projects/create-project.tsx
+++ /dev/null
@@ -1,85 +0,0 @@
-import type { DashboardProjectRouter } from "@webstudio-is/dashboard";
-import {
- Box,
- Button,
- Flex,
- Label,
- Text,
- TextField,
- theme,
-} from "@webstudio-is/design-system";
-import { PlusIcon } from "@webstudio-is/icons";
-import { FormEventHandler, useEffect } from "react";
-import { useNavigate } from "@remix-run/react";
-import { dashboardProjectPath, designerPath } from "~/shared/router-utils";
-import { createTrpcRemixProxy } from "~/shared/remix/trpc-remix-proxy";
-import { DialogActions, DialogClose, Dialog } from "./dialog";
-
-const trpc = createTrpcRemixProxy<DashboardProjectRouter>(dashboardProjectPath);
-
-const useForm = () => {
- const { send, data } = trpc.create.useMutation();
- const navigate = useNavigate();
-
- useEffect(() => {
- if (data === undefined || "errors" in data) {
- return;
- }
- navigate(designerPath({ projectId: data.id }));
- }, [data, navigate]);
-
- const handleSubmit: FormEventHandler = (event) => {
- event.preventDefault();
- const formData = new FormData(event.currentTarget as HTMLFormElement);
- const title = String(formData.get("title") ?? "");
- send({ title });
- };
-
- return {
- handleSubmit,
- errors: data && "errors" in data ? data.errors : undefined,
- };
-};
-
-const Content = () => {
- const { handleSubmit, errors } = useForm();
- return (
- <form onSubmit={handleSubmit}>
- <Flex
- direction="column"
- css={{
- px: theme.spacing["9"],
- paddingTop: theme.spacing["5"],
- }}
- gap="1"
- >
- <Label>Project Title</Label>
- <TextField
- placeholder="New Project"
- name="title"
- state={errors ? "invalid" : undefined}
- />
- <Box css={{ minHeight: theme.spacing["10"] }}>
- {errors && <Text color="error">{errors}</Text>}
- </Box>
- </Flex>
- <DialogActions>
- <Button type="submit">Create Project</Button>
- <DialogClose asChild>
- <Button color="ghost">Cancel</Button>
- </DialogClose>
- </DialogActions>
- </form>
- );
-};
-
-export const CreateProject = () => {
- return (
- <Dialog
- title="New Project"
- trigger={<Button prefix={<PlusIcon />}>New Project</Button>}
- >
- <Content />
- </Dialog>
- );
-};
diff --git a/apps/designer/app/dashboard/projects/dialog.tsx b/apps/designer/app/dashboard/projects/dialog.tsx
index 814ab82958..9d35b5ea14 100644
--- a/apps/designer/app/dashboard/projects/dialog.tsx
+++ b/apps/designer/app/dashboard/projects/dialog.tsx
@@ -5,14 +5,16 @@ import {
DialogContent,
DialogTitle,
DialogClose,
+ DialogDescription,
theme,
} from "@webstudio-is/design-system";
type DialogProps = {
title: string;
- children: JSX.Element;
- trigger: JSX.Element;
+ children: JSX.Element | Array<JSX.Element>;
+ trigger?: JSX.Element;
onOpenChange?: (open: boolean) => void;
+ isOpen?: boolean;
};
export const Dialog = ({
@@ -20,10 +22,11 @@ export const Dialog = ({
children,
trigger,
onOpenChange,
+ isOpen,
}: DialogProps) => {
return (
- <BaseDialog onOpenChange={onOpenChange}>
- <DialogTrigger asChild>{trigger}</DialogTrigger>
+ <BaseDialog open={isOpen} onOpenChange={onOpenChange}>
+ {trigger && <DialogTrigger asChild>{trigger}</DialogTrigger>}
<DialogContent>
{children}
<DialogTitle>{title}</DialogTitle>
@@ -52,4 +55,4 @@ export const DialogActions = ({
);
};
-export { DialogClose };
+export { DialogClose, DialogDescription };
diff --git a/apps/designer/app/dashboard/projects/project-card.tsx b/apps/designer/app/dashboard/projects/project-card.tsx
index 5d93ec7fce..5d6a5a2555 100644
--- a/apps/designer/app/dashboard/projects/project-card.tsx
+++ b/apps/designer/app/dashboard/projects/project-card.tsx
@@ -8,20 +8,14 @@ import {
Flex,
Text,
theme,
- toast,
} from "@webstudio-is/design-system";
import { MenuIcon } from "@webstudio-is/icons";
import type { DashboardProject } from "@webstudio-is/prisma-client";
import { KeyboardEvent, useEffect, useRef, useState } from "react";
-import {
- dashboardProjectPath,
- designerPath,
- getPublishedUrl,
-} from "~/shared/router-utils";
-import { Link as RemixLink, useFetcher } from "@remix-run/react";
+import { designerPath, getPublishedUrl } from "~/shared/router-utils";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
-import type { DashboardProjectRouter } from "@webstudio-is/dashboard";
-import { createTrpcRemixProxy } from "~/shared/remix/trpc-remix-proxy";
+import { RenameProject, DeleteProject, useDuplicate } from "./project-dialogs";
+import { ThumbnailLink } from "./thumbnail-link";
const containerStyle = css({
overflow: "hidden",
@@ -37,28 +31,6 @@ const containerStyle = css({
},
});
-// @todo use typography from figma tokens
-const thumbnailStyle = css({
- display: "flex",
- alignItems: "center",
- alignSelf: "center",
- minHeight: 0,
- fontFamily: theme.fonts.manrope,
- fontWeight: 200,
- fontSize: 360,
- letterSpacing: "-0.05em",
- background: theme.colors.brandBackgroundProjectCardFront,
- WebkitBackgroundClip: "text",
- backgroundClip: "text",
- color: "transparent",
- userSelect: "none",
- outline: "none",
- "&:hover, &:focus": {
- fontWeight: 800,
- transition: "100ms",
- },
-});
-
const footerStyle = css({
background: theme.colors.brandBackgroundProjectCardTextArea,
height: theme.spacing[17],
@@ -142,18 +114,7 @@ const Menu = ({
};
const useProjectCard = () => {
- const fetcher = useFetcher();
const thumbnailRef = useRef<HTMLAnchorElement>(null);
- const { send: deleteProject } = trpc.delete.useMutation();
- const { send: rename } = trpc.rename.useMutation();
- const { send: duplicate } = trpc.duplicate.useMutation();
-
- // @todo with dialog it can be displayed in the dialog
- useEffect(() => {
- if (fetcher.data?.errors) {
- toast.error(fetcher.data.errors);
- }
- }, [fetcher.data]);
const handleKeyDown = (event: KeyboardEvent<HTMLElement>) => {
const elements: Array<HTMLElement> = Array.from(
@@ -164,7 +125,11 @@ const useProjectCard = () => {
);
switch (event.key) {
case "Enter": {
- thumbnailRef.current?.click();
+ // Only open project on enter when the project card container was focused,
+ // otherwise we will always open project, even when a menu was pressed.
+ if (event.currentTarget === document.activeElement) {
+ thumbnailRef.current?.click();
+ }
break;
}
case "ArrowUp":
@@ -182,44 +147,16 @@ const useProjectCard = () => {
}
};
- const handleDelete = (projectId: string) => {
- deleteProject({ projectId });
- };
-
- const handleRename = (projectId: string) => {
- // @todo replace with the new dialog UI, waiting for dialog component
- const title = prompt();
- // User has aborted
- if (title === null) {
- return;
- }
- rename({ projectId, title });
- };
-
- const handleDuplicate = (projectId: string) => {
- duplicate({ projectId });
- };
-
return {
thumbnailRef,
handleKeyDown,
- handleDelete,
- handleRename,
- handleDuplicate,
};
};
-const trpc = createTrpcRemixProxy<DashboardProjectRouter>(dashboardProjectPath);
-
-// My Next Project > MN
-const getThumbnailAbbreviation = (title: string) =>
- title
- .split(/\s+/)
- .slice(0, 2)
- .map((word) => word.charAt(0).toUpperCase())
- .join("");
-
-type ProjectCardProps = DashboardProject;
+type ProjectCardProps = Pick<
+ DashboardProject,
+ "id" | "title" | "domain" | "isPublished"
+>;
export const ProjectCard = ({
id,
@@ -227,54 +164,72 @@ export const ProjectCard = ({
domain,
isPublished,
}: ProjectCardProps) => {
- const {
- thumbnailRef,
- handleKeyDown,
- handleDelete,
- handleRename,
- handleDuplicate,
- } = useProjectCard();
+ const [isRenameDialogOpen, setIsRenameDialogOpen] = useState(false);
+ const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
+ const { thumbnailRef, handleKeyDown } = useProjectCard();
+ const handleDuplicate = useDuplicate(id);
+
return (
- <Flex
- direction="column"
- shrink={false}
- as="article"
- className={containerStyle()}
- tabIndex={0}
- onKeyDown={handleKeyDown}
- >
- <RemixLink
- ref={thumbnailRef}
- to={designerPath({ projectId: id })}
- className={thumbnailStyle()}
- tabIndex={-1}
+ <>
+ <Flex
+ direction="column"
+ shrink={false}
+ as="article"
+ className={containerStyle()}
+ tabIndex={0}
+ onKeyDown={handleKeyDown}
>
- {getThumbnailAbbreviation(title)}
- </RemixLink>
- <Flex justify="between" shrink={false} gap="1" className={footerStyle()}>
- <Flex direction="column" justify="around">
- <Text variant="title" truncate>
- {title}
- </Text>
- {isPublished ? (
- <PublishedLink domain={domain} tabIndex={-1} />
- ) : (
- <Text color="hint">Not Published</Text>
- )}
- </Flex>
- <Menu
- tabIndex={-1}
- onDelete={() => {
- handleDelete(id);
- }}
- onRename={() => {
- handleRename(id);
- }}
- onDuplicate={() => {
- handleDuplicate(id);
- }}
+ <ThumbnailLink
+ title={title}
+ to={designerPath({ projectId: id })}
+ ref={thumbnailRef}
/>
+ <Flex
+ justify="between"
+ shrink={false}
+ gap="1"
+ className={footerStyle()}
+ >
+ <Flex direction="column" justify="around">
+ <Text variant="title" truncate>
+ {title}
+ </Text>
+ {isPublished ? (
+ <PublishedLink domain={domain} tabIndex={-1} />
+ ) : (
+ <Text color="hint">Not Published</Text>
+ )}
+ </Flex>
+ <Menu
+ tabIndex={-1}
+ onDelete={() => {
+ setIsDeleteDialogOpen(true);
+ }}
+ onRename={() => {
+ setIsRenameDialogOpen(true);
+ }}
+ onDuplicate={handleDuplicate}
+ />
+ </Flex>
</Flex>
- </Flex>
+ <RenameProject
+ isOpen={isRenameDialogOpen}
+ title={title}
+ projectId={id}
+ onComplete={() => {
+ setIsRenameDialogOpen(false);
+ }}
+ onOpenChange={setIsRenameDialogOpen}
+ />
+ <DeleteProject
+ isOpen={isDeleteDialogOpen}
+ title={title}
+ projectId={id}
+ onComplete={() => {
+ setIsDeleteDialogOpen(false);
+ }}
+ onOpenChange={setIsDeleteDialogOpen}
+ />
+ </>
);
};
diff --git a/apps/designer/app/dashboard/projects/project-dialogs.tsx b/apps/designer/app/dashboard/projects/project-dialogs.tsx
new file mode 100644
index 0000000000..59ff1862f7
--- /dev/null
+++ b/apps/designer/app/dashboard/projects/project-dialogs.tsx
@@ -0,0 +1,290 @@
+import type { DashboardProjectRouter } from "@webstudio-is/dashboard";
+import {
+ Box,
+ Button,
+ Flex,
+ Label,
+ Paragraph,
+ Text,
+ TextField,
+ theme,
+} from "@webstudio-is/design-system";
+import { PlusIcon } from "@webstudio-is/icons";
+import { useEffect, useState } from "react";
+import { useNavigate } from "@remix-run/react";
+import { dashboardProjectPath, designerPath } from "~/shared/router-utils";
+import { createTrpcRemixProxy } from "~/shared/remix/trpc-remix-proxy";
+import {
+ DialogActions,
+ DialogClose,
+ Dialog,
+ DialogDescription,
+} from "./dialog";
+import { DashboardProject } from "@webstudio-is/prisma-client";
+
+const Content = ({
+ onSubmit,
+ onChange,
+ placeholder,
+ errors,
+ primaryButton,
+ title,
+ description,
+ label,
+ width,
+}: {
+ onSubmit: (data: { title: string }) => void;
+ onChange?: (data: { title: string }) => void;
+ errors?: string;
+ placeholder?: string;
+ title?: string;
+ label: string | JSX.Element;
+ description?: string;
+ primaryButton: JSX.Element;
+ width?: string;
+}) => {
+ return (
+ <form
+ onSubmit={(event) => {
+ event.preventDefault();
+ const formData = new FormData(event.currentTarget as HTMLFormElement);
+ const title = String(formData.get("title") ?? "");
+ onSubmit({ title });
+ }}
+ >
+ <Flex
+ direction="column"
+ css={{
+ px: theme.spacing["9"],
+ paddingTop: theme.spacing["5"],
+ width,
+ }}
+ gap="1"
+ >
+ {description && (
+ <DialogDescription asChild>
+ <Paragraph>{description}</Paragraph>
+ </DialogDescription>
+ )}
+ <Label>{label}</Label>
+ <TextField
+ placeholder={placeholder}
+ name="title"
+ defaultValue={title}
+ state={errors ? "invalid" : undefined}
+ onChange={(event) => {
+ onChange?.({ title: event.currentTarget.value });
+ }}
+ />
+ <Box css={{ minHeight: theme.spacing["10"] }}>
+ {errors && <Text color="error">{errors}</Text>}
+ </Box>
+ </Flex>
+ <DialogActions>
+ {primaryButton}
+ <DialogClose asChild>
+ <Button color="ghost">Cancel</Button>
+ </DialogClose>
+ </DialogActions>
+ </form>
+ );
+};
+
+const trpc = createTrpcRemixProxy<DashboardProjectRouter>(dashboardProjectPath);
+
+const useCreateProject = () => {
+ const navigate = useNavigate();
+ const { send, data } = trpc.create.useMutation();
+
+ useEffect(() => {
+ if (data === undefined || "errors" in data) {
+ return;
+ }
+ navigate(designerPath({ projectId: data.id }));
+ }, [data, navigate]);
+
+ return {
+ handleSubmit: send,
+ errors: data && "errors" in data ? data.errors : undefined,
+ };
+};
+
+export const CreateProject = () => {
+ const { handleSubmit, errors } = useCreateProject();
+ return (
+ <Dialog
+ title="New Project"
+ trigger={<Button prefix={<PlusIcon />}>New Project</Button>}
+ >
+ <Content
+ onSubmit={handleSubmit}
+ errors={errors}
+ placeholder="New Project"
+ label="Project Title"
+ primaryButton={<Button type="submit">Create Project</Button>}
+ />
+ </Dialog>
+ );
+};
+
+const useRenameProject = ({
+ projectId,
+ onComplete,
+}: {
+ projectId: DashboardProject["id"];
+ onComplete: () => void;
+}) => {
+ const { send, data, state } = trpc.rename.useMutation();
+ const errors = data && "errors" in data ? data.errors : undefined;
+
+ useEffect(() => {
+ if (errors === undefined && state === "loading") {
+ onComplete();
+ }
+ }, [errors, state, onComplete]);
+
+ const handleSubmit = ({ title }: { title: string }) => {
+ send({ projectId, title });
+ };
+
+ return {
+ handleSubmit,
+ errors,
+ state,
+ };
+};
+
+export const RenameProject = ({
+ isOpen,
+ title,
+ projectId,
+ onComplete,
+ onOpenChange,
+}: {
+ isOpen: boolean;
+ title: string;
+ projectId: DashboardProject["id"];
+ onComplete: () => void;
+ onOpenChange: (isOpen: boolean) => void;
+}) => {
+ const { handleSubmit, errors, state } = useRenameProject({
+ projectId,
+ onComplete,
+ });
+ return (
+ <Dialog title="Rename" isOpen={isOpen} onOpenChange={onOpenChange}>
+ <Content
+ onSubmit={handleSubmit}
+ errors={errors}
+ title={title}
+ label="Project Title"
+ primaryButton={
+ <Button
+ type="submit"
+ state={state === "idle" ? undefined : "pending"}
+ >
+ Rename Project
+ </Button>
+ }
+ />
+ </Dialog>
+ );
+};
+
+const useDeleteProject = ({
+ projectId,
+ title,
+ onComplete,
+}: {
+ projectId: DashboardProject["id"];
+ title: string;
+ onComplete: () => void;
+}) => {
+ const { send, data, state } = trpc.delete.useMutation();
+ const [isMatch, setIsMatch] = useState(false);
+ const errors = data && "errors" in data ? data.errors : undefined;
+
+ useEffect(() => {
+ if (errors === undefined && state === "loading") {
+ onComplete();
+ }
+ }, [errors, state, onComplete]);
+
+ const handleSubmit = () => {
+ send({ projectId });
+ };
+
+ const handleChange = ({ title: currentTitle }: { title: string }) => {
+ setIsMatch(currentTitle.trim().toLowerCase() === title.toLowerCase());
+ };
+
+ return {
+ handleSubmit,
+ handleChange,
+ errors,
+ isMatch,
+ state,
+ };
+};
+
+export const DeleteProject = ({
+ isOpen,
+ title,
+ projectId,
+ onComplete,
+ onOpenChange,
+}: {
+ isOpen: boolean;
+ title: string;
+ projectId: DashboardProject["id"];
+ onComplete: () => void;
+ onOpenChange: (isOpen: boolean) => void;
+}) => {
+ const { handleSubmit, handleChange, errors, isMatch, state } =
+ useDeleteProject({
+ projectId,
+ onComplete,
+ title,
+ });
+ return (
+ <Dialog
+ title="Delete Confirmation"
+ isOpen={isOpen}
+ onOpenChange={onOpenChange}
+ >
+ <Content
+ onSubmit={handleSubmit}
+ onChange={handleChange}
+ errors={errors}
+ label={
+ <>
+ Confirm by typing{" "}
+ <Text as="span" color="error" variant="label">
+ {title}
+ </Text>{" "}
+ below
+ </>
+ }
+ description="This project and its styles, pages and images will be deleted permanently."
+ primaryButton={
+ <Button
+ type="submit"
+ color="destructive"
+ disabled={isMatch === false}
+ state={state === "idle" ? undefined : "pending"}
+ >
+ Delete Forever
+ </Button>
+ }
+ width={theme.spacing["33"]}
+ />
+ </Dialog>
+ );
+};
+
+export const useDuplicate = (projectId: DashboardProject["id"]) => {
+ const { send } = trpc.duplicate.useMutation();
+ return () => {
+ send({ projectId });
+ };
+};
diff --git a/apps/designer/app/dashboard/projects/projects.tsx b/apps/designer/app/dashboard/projects/projects.tsx
index 1e69096f48..b7439587e5 100644
--- a/apps/designer/app/dashboard/projects/projects.tsx
+++ b/apps/designer/app/dashboard/projects/projects.tsx
@@ -4,7 +4,7 @@ import { Panel } from "./panel";
import { Heading } from "../heading";
import type { DashboardProject } from "@webstudio-is/prisma-client";
import { ProjectCard } from "./project-card";
-import { CreateProject } from "./create-project";
+import { CreateProject } from "./project-dialogs";
type ProjectsProps = {
projects: Array<DashboardProject>;
diff --git a/apps/designer/app/dashboard/projects/thumbnail-link.tsx b/apps/designer/app/dashboard/projects/thumbnail-link.tsx
new file mode 100644
index 0000000000..1a8737dad6
--- /dev/null
+++ b/apps/designer/app/dashboard/projects/thumbnail-link.tsx
@@ -0,0 +1,44 @@
+import { css, theme } from "@webstudio-is/design-system";
+import { forwardRef } from "react";
+import { Link } from "@remix-run/react";
+
+// @todo use typography from figma tokens
+const thumbnailStyle = css({
+ display: "flex",
+ alignItems: "center",
+ alignSelf: "center",
+ minHeight: 0,
+ fontFamily: theme.fonts.manrope,
+ fontWeight: 200,
+ fontSize: 360,
+ letterSpacing: "-0.05em",
+ background: theme.colors.brandBackgroundProjectCardFront,
+ WebkitBackgroundClip: "text",
+ backgroundClip: "text",
+ color: "transparent",
+ userSelect: "none",
+ outline: "none",
+ "&:hover, &:focus": {
+ fontWeight: 800,
+ transition: "100ms",
+ },
+});
+
+// My Next Project > MN
+const getThumbnailAbbreviation = (title: string) =>
+ title
+ .split(/\s+/)
+ .slice(0, 2)
+ .map((word) => word.charAt(0).toUpperCase())
+ .join("");
+
+export const ThumbnailLink = forwardRef<
+ HTMLAnchorElement,
+ { title: string; to: string }
+>(({ title, to }, ref) => (
+ <Link ref={ref} to={to} className={thumbnailStyle()} tabIndex={-1}>
+ {getThumbnailAbbreviation(title)}
+ </Link>
+));
+
+ThumbnailLink.displayName = "ThumbnailLink";
diff --git a/packages/design-system/src/components/text.tsx b/packages/design-system/src/components/text.tsx
index 31f91b0425..80d36b8557 100644
--- a/packages/design-system/src/components/text.tsx
+++ b/packages/design-system/src/components/text.tsx
@@ -4,7 +4,6 @@ export const textStyles = css({
// Reset
margin: 0,
lineHeight: 1,
- userSelect: "none",
fontFamily: theme.fonts.sans,
variants: {
diff --git a/packages/project/src/db/project.ts b/packages/project/src/db/project.ts
index 281f3d633b..5fa9ffd86a 100644
--- a/packages/project/src/db/project.ts
+++ b/packages/project/src/db/project.ts
@@ -137,7 +137,7 @@ export const markAsDeleted = async (
);
if (canDelete === false) {
- throw new Error("Only the owner can delete the project");
+ return { errors: "Only the owner can delete the project" };
}
return await prisma.project.update({