import { useState } from "react"; import { Control, Controller, FieldErrors, UseFormHandleSubmit, UseFormSetValue } from "react-hook-form"; import { IUser, IWorkspace, TOnboardingSteps } from "@plane/types"; // ui import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui"; // types import { WORKSPACE_CREATED } from "@/constants/event-tracker"; import { RESTRICTED_URLS } from "@/constants/workspace"; import { useEventTracker, useUser, useWorkspace } from "@/hooks/store"; // services import { WorkspaceService } from "@/services/workspace.service"; // constants type Props = { stepChange: (steps: Partial) => Promise; user: IUser | undefined; control: Control; handleSubmit: UseFormHandleSubmit; errors: FieldErrors; setValue: UseFormSetValue; isSubmitting: boolean; }; // services const workspaceService = new WorkspaceService(); export const Workspace: React.FC = (props) => { const { stepChange, user, control, handleSubmit, setValue, errors, isSubmitting } = props; // states const [slugError, setSlugError] = useState(false); const [invalidSlug, setInvalidSlug] = useState(false); // store hooks const { updateCurrentUser } = useUser(); const { createWorkspace, fetchWorkspaces, workspaces } = useWorkspace(); const { captureWorkspaceEvent } = useEventTracker(); 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 updateCurrentUser({ last_workspace_id: firstWorkspace?.id, }); }; return (

Name it.

/^[\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="h-[46px] w-full border-onboarding-border-100 text-base placeholder:text-base placeholder:text-custom-text-400/50" />
)} /> {errors.name && {errors.name.message}}

You can edit the slug.

(
{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="h-[46px] w-full border-none !px-0" />
)} /> {slugError && Workspace URL is already taken!} {invalidSlug && ( {`URL can only contain ( - ), ( _ ) & alphanumeric characters.`} )}
); };