import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; // types import { IUser, IWorkspace, TOnboardingSteps } from "@plane/types"; // ui import { Button, CustomSelect, Input, Spinner, TOAST_TYPE, setToast } from "@plane/ui"; // constants import { WORKSPACE_CREATED } from "@/constants/event-tracker"; import { ORGANIZATION_SIZE, RESTRICTED_URLS } from "@/constants/workspace"; // hooks import { useEventTracker, useUserProfile, useWorkspace } from "@/hooks/store"; // services import { WorkspaceService } from "@/services/workspace.service"; type Props = { stepChange: (steps: Partial) => Promise; user: IUser | undefined; invitedWorkspaces: number; handleCurrentViewChange: () => void; }; // services const workspaceService = new WorkspaceService(); export const CreateWorkspace: React.FC = (props) => { const { stepChange, user, invitedWorkspaces, handleCurrentViewChange } = props; // states const [slugError, setSlugError] = useState(false); const [invalidSlug, setInvalidSlug] = useState(false); // store hooks const { updateUserProfile } = useUserProfile(); const { createWorkspace, fetchWorkspaces, workspaces } = useWorkspace(); const { captureWorkspaceEvent } = useEventTracker(); // form info const { handleSubmit, control, setValue, formState: { errors, isSubmitting, isValid }, } = useForm({ defaultValues: { name: "", slug: "", organization_size: "", }, mode: "onChange", }); const handleCreateWorkspace = async (formData: IWorkspace) => { if (isSubmitting) return; await workspaceService .workspaceSlugCheck(formData.slug) .then(async (res) => { if (res.status === true && !RESTRICTED_URLS.includes(formData.slug)) { setSlugError(false); await createWorkspace(formData) .then(async (res) => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Workspace created successfully.", }); captureWorkspaceEvent({ eventName: WORKSPACE_CREATED, payload: { ...res, state: "SUCCESS", first_time: true, element: "Onboarding", }, }); await fetchWorkspaces(); await completeStep(); }) .catch(() => { captureWorkspaceEvent({ eventName: WORKSPACE_CREATED, payload: { state: "FAILED", first_time: true, element: "Onboarding", }, }); setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Workspace could not be created. Please try again.", }); }); } else setSlugError(true); }) .catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Some error occurred while creating workspace. Please try again.", }) ); }; const completeStep = async () => { if (!user || !workspaces) return; const firstWorkspace = Object.values(workspaces ?? {})?.[0]; const payload: Partial = { workspace_create: true, workspace_join: true, }; await stepChange(payload); await updateUserProfile({ last_workspace_id: firstWorkspace?.id, }); }; const isButtonDisabled = !isValid || invalidSlug || isSubmitting; return (
{!!invitedWorkspaces && ( <>

or


)}

Create a workspace

To start using Plane, you need to create or join a workspace.

/^[\w\s-]*$/.test(value) || `Name can only contain (" "), ( - ), ( _ ) & alphanumeric characters.`, maxLength: { value: 80, message: "Workspace name should not exceed 80 characters", }, }} render={({ field: { value, ref, onChange } }) => (
{ onChange(event.target.value); setValue("name", event.target.value); setValue("slug", event.target.value.toLocaleLowerCase().trim().replace(/ /g, "-")); }} placeholder="Enter workspace name..." ref={ref} hasError={Boolean(errors.name)} className="w-full border-onboarding-border-100 placeholder:text-custom-text-400" autoFocus />
)} /> {errors.name && {errors.name.message}}
(
{window && window.location.host}/ { /^[a-zA-Z0-9_-]+$/.test(e.target.value) ? setInvalidSlug(false) : setInvalidSlug(true); onChange(e.target.value.toLowerCase()); }} ref={ref} hasError={Boolean(errors.slug)} className="w-full border-none !px-0" />
)} />

You can only edit the slug of the URL

{slugError &&

Workspace URL is already taken!

} {invalidSlug && (

{`URL can only contain ( - ), ( _ ) & alphanumeric characters.`}

)}

( c === value) ?? ( Select organization size ) } buttonClassName="!border-[0.5px] !border-onboarding-border-100 !shadow-none !rounded-md" input optionsClassName="w-full" > {ORGANIZATION_SIZE.map((item) => ( {item} ))} )} /> {errors.organization_size && ( {errors.organization_size.message} )}
); };