diff --git a/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/CopyButton.tsx b/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/CopyButton.tsx new file mode 100644 index 000000000..8337d1c4d --- /dev/null +++ b/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/CopyButton.tsx @@ -0,0 +1,52 @@ +import { useCallback } from "react"; +import { faCheck, faCopy } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; +import { Button } from "@app/components/v2"; +import { useToggle } from "@app/hooks"; + +type Props = { + value: string; + hoverText: string; + notificationText: string; + children: React.ReactNode; +}; + +export const CopyButton = ({ value, children, hoverText, notificationText }: Props) => { + const [isProjectIdCopied, setIsProjectIdCopied] = useToggle(false); + const { createNotification } = useNotificationContext(); + + const copyToClipboard = useCallback(() => { + if (isProjectIdCopied) { + return; + } + + setIsProjectIdCopied.on(); + navigator.clipboard.writeText(value); + + createNotification({ + text: notificationText, + type: "success" + }); + + const timer = setTimeout(() => setIsProjectIdCopied.off(), 2000); + + // eslint-disable-next-line consistent-return + return () => clearTimeout(timer); + }, [isProjectIdCopied]); + + return ( + } + onClick={copyToClipboard} + > + {children} + + {hoverText} + + + ); +}; diff --git a/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/ProjectNameChangeSection.tsx b/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/ProjectNameChangeSection.tsx index 719129ebf..34261f47a 100644 --- a/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/ProjectNameChangeSection.tsx +++ b/frontend/src/views/Settings/ProjectSettingsPage/components/ProjectNameChangeSection/ProjectNameChangeSection.tsx @@ -1,7 +1,5 @@ import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; -import { faCheck, faCopy } from "@fortawesome/free-solid-svg-icons"; -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { yupResolver } from "@hookform/resolvers/yup"; import * as yup from "yup"; @@ -9,9 +7,10 @@ import { useNotificationContext } from "@app/components/context/Notifications/No import { ProjectPermissionCan } from "@app/components/permissions"; import { Button, FormControl, Input } from "@app/components/v2"; import { ProjectPermissionActions, ProjectPermissionSub, useWorkspace } from "@app/context"; -import { useToggle } from "@app/hooks"; import { useRenameWorkspace } from "@app/hooks/api"; +import { CopyButton } from "./CopyButton"; + const formSchema = yup.object({ name: yup.string().required().label("Project Name") }); @@ -22,7 +21,6 @@ export const ProjectNameChangeSection = () => { const { createNotification } = useNotificationContext(); const { currentWorkspace } = useWorkspace(); const { mutateAsync, isLoading } = useRenameWorkspace(); - const [isProjectIdCopied, setIsProjectIdCopied] = useToggle(false); const { handleSubmit, control, reset } = useForm({ resolver: yupResolver(formSchema) }); @@ -34,16 +32,6 @@ export const ProjectNameChangeSection = () => { } }, [currentWorkspace]); - useEffect(() => { - let timer: NodeJS.Timeout; - - if (isProjectIdCopied) { - timer = setTimeout(() => setIsProjectIdCopied.off(), 2000); - } - - return () => clearTimeout(timer); -}, [setIsProjectIdCopied]); - const onFormSubmit = async ({ name }: FormData) => { try { if (!currentWorkspace?.id) return; @@ -66,35 +54,28 @@ export const ProjectNameChangeSection = () => { } }; - const copyProjectIdToClipboard = () => { - navigator.clipboard.writeText(currentWorkspace?.id || ""); - setIsProjectIdCopied.on(); - - createNotification({ - text: "Copied Project ID to clipboard", - type: "success" - }); - } - return ( - - Project Name - - } - onClick={copyProjectIdToClipboard} + + Project Name + + + Copy Project Slug + + Copy Project ID - - Click to copy - - +