Files
webstudio/apps/builder/app/shared/clone-project.tsx
T
Oleg Isonen 6cb74d4e6a feat: Floating panel v2 (#4613)
Closes https://github.com/webstudio-is/webstudio/issues/4572

## Description

- All floating panels are now using a dialog and are draggable (bindings
and expressions not migrated yet, separate PR)
- Logic for positioning the panels is now in our control and easier to
manage, because we have lots of special cases
- Deleted FloatingPanelPopover, now everything uses either FloatingPanel
or Popover, difference is in positioning logic, FloatingPanel is using
Dialog
- Big center positioned dialogs like code for html embed allow having
another panel open at the same time, wile all other floating panels
close the other panel when clicked
- Most extended settings in style panel are now positioned above style
panel, not on the left

## Steps for reproduction

1. click button
3. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
0000)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-12-20 11:22:53 +01:00

168 lines
3.8 KiB
TypeScript

import { useState } from "react";
import {
Box,
Button,
Flex,
Label,
Text,
theme,
InputField,
DialogContent,
DialogTitle,
DialogClose,
Dialog,
DialogActions,
toast,
} from "@webstudio-is/design-system";
import { Title, type Project } from "@webstudio-is/project";
import { nativeClient } from "./trpc/trpc-client";
import { useEffectEvent } from "./hook-utils/effect-event";
const useCloneProject = ({
projectId,
onCreate,
authToken,
}: {
projectId: Project["id"];
authToken?: string;
onCreate: (projectId: Project["id"]) => void;
}) => {
const [state, setState] = useState<"idle" | "loading" | "submitting">("idle");
const [errors, setErrors] = useState<string>();
const handleSubmit = async ({ title }: { title: string }) => {
const parsed = Title.safeParse(title);
const errors =
"error" in parsed
? parsed.error.issues.map((issue) => issue.message).join("\n")
: undefined;
setErrors(errors);
if (parsed.success) {
try {
setState("submitting");
const data = await nativeClient.project.clone.mutate({
projectId,
title,
authToken,
});
setState("idle");
onCreate(data.id);
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
setState("idle");
}
}
};
return {
handleSubmit,
errors,
state,
};
};
const CloneProjectView = ({
isOpen,
title,
errors,
state,
onOpenChange,
onSubmit,
}: {
isOpen: boolean;
title: string;
errors?: string;
state: "idle" | "loading" | "submitting";
onOpenChange: (isOpen: boolean) => void;
onSubmit: ({ title }: { title: string }) => void;
}) => {
return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent>
<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["7"],
paddingTop: theme.spacing["5"],
}}
gap="1"
>
<Label>Project Title</Label>
<InputField
name="title"
defaultValue={title}
color={errors ? "error" : undefined}
/>
<Box css={{ minHeight: theme.spacing["10"] }}>
{errors && <Text color="destructive">{errors}</Text>}
</Box>
</Flex>
<DialogActions>
<Button
type="submit"
state={state === "idle" ? undefined : "pending"}
>
Clone
</Button>
<DialogClose>
<Button color="ghost">Cancel</Button>
</DialogClose>
</DialogActions>
</form>
<DialogTitle>Clone Project</DialogTitle>
</DialogContent>
</Dialog>
);
};
export const CloneProjectDialog = ({
isOpen,
project: { id, title },
onOpenChange,
authToken,
onCreate,
}: {
isOpen: boolean;
project: Pick<Project, "id" | "title">;
authToken?: string;
onOpenChange: (isOpen: boolean) => void;
onCreate: (projectId: Project["id"]) => void;
}) => {
const handleOnCreate = useEffectEvent((projectId: Project["id"]) => {
onCreate(projectId);
onOpenChange(false);
});
const { handleSubmit, errors, state } = useCloneProject({
projectId: id,
authToken,
onCreate: handleOnCreate,
});
return (
<CloneProjectView
title={title}
onSubmit={handleSubmit}
errors={errors}
state={state}
isOpen={isOpen}
onOpenChange={onOpenChange}
/>
);
};