import { type FormEvent, type ReactElement, useState } from 'react' import { useLocation } from 'wouter' import { useCreateWorkspace } from '@/client/features/workspace/add/api' import { useHarnessStatuses } from '@/client/features/harness/api' import { useAppConfig } from '@/client/api/app-config' import { Button } from '@/client/components/ui/button' import { Dialog, DialogDescription, DialogTitle, DialogTrigger } from '@/client/components/ui/dialog' import { Input } from '@/client/components/ui/input' import { Tooltip, TooltipContent, TooltipTrigger } from '@/client/components/ui/tooltip' import { validateWorkspaceName } from '@/lib/workspace-name' import { WORKSPACE_TYPE_ORDER } from '@/lib/workspace-types' import type { WorkspaceType } from '@/lib/types' import { DemoDialog } from '@/client/features/demo/demo-dialog' import { welcomeNewWorkspace } from '../setup/workspace-welcome-state' import { WorkspaceAgentSelector } from './workspace-agent-selector' import { getWorkspaceAgentOptions } from './workspace-agent-options' import { WorkspaceDialogContent } from './workspace-dialog-content' type CreateWorkspaceDialogProps = { trigger: ReactElement } type CreateWorkspaceStep = 'agent' | 'name' const DEFAULT_WORKSPACE_TYPE = WORKSPACE_TYPE_ORDER[0] export function CreateWorkspaceDialog({ trigger }: CreateWorkspaceDialogProps) { const [, navigate] = useLocation() const appConfig = useAppConfig() const info = useHarnessStatuses() const createMutation = useCreateWorkspace() const [open, setOpen] = useState(false) const [step, setStep] = useState('name') const [type, setType] = useState(DEFAULT_WORKSPACE_TYPE) const [name, setName] = useState('') const trimmedName = name.trim() const nameError = trimmedName ? validateWorkspaceName(trimmedName) : null const isCreating = createMutation.isPending const isChecking = info.isPending || info.isFetching const installed = info.isError ? undefined : info.data const options = getWorkspaceAgentOptions(installed) const selectedAvailable = options.some(option => option.type === type && !option.disabled) const detectionError = !isChecking && !installed ? 'Could not check installed agents.' : undefined const canUseAgent = !isChecking && !detectionError && selectedAvailable // Cloud demo: creating workspaces is off — the same trigger opens the // cloud-demo promo dialog instead. After every hook so the hook order is stable. if (appConfig.demo) { return } function finish(workspaceId: string) { welcomeNewWorkspace(workspaceId) setOpen(false) navigate(`/workspace/${workspaceId}`) } function resetDialog() { setStep('name') setType(DEFAULT_WORKSPACE_TYPE) setName('') createMutation.reset() } function continueToAgent() { if (!trimmedName || nameError) return createMutation.reset() setStep('agent') } function createWorkspace() { if (!trimmedName || nameError || isCreating || !canUseAgent) return createMutation.reset() createMutation.mutate({ name: trimmedName, type }, { onSuccess: entry => finish(entry.id) }) } return ( { if (!nextOpen && isCreating) return setOpen(nextOpen) if (nextOpen) void info.refetch({ cancelRefetch: false }) }} onOpenChangeComplete={nextOpen => { if (!nextOpen) resetDialog() }} > {step === 'name' ? ( ) : ( setStep('name')}> Back } primaryLabel="Create workspace" pendingLabel={isCreating ? 'Creating…' : undefined} onTypeChange={setType} onSubmit={createWorkspace} /> )} ) } type WorkspaceNameStepProps = { name: string showExistingFolder: boolean validationError: string | null onNameChange: (name: string) => void onSubmit: () => void } function WorkspaceNameStep({ name, showExistingFolder, validationError, onNameChange, onSubmit }: WorkspaceNameStepProps) { function handleSubmit(event: FormEvent) { event.preventDefault() onSubmit() } return (
Create new workspace Give it a short and recognizable name
onNameChange(event.target.value)} placeholder="Workspace name" autoFocus aria-invalid={Boolean(validationError)} aria-describedby={validationError ? 'workspace-name-error' : undefined} autoComplete="off" spellCheck={false} /> {validationError && ( )}
{showExistingFolder && }
) } // Importing a folder from the app is on hold: the native picker only ever ran on // macOS and cannot work at all when moi is opened from another device. The // button stays as the signpost for the console route. function ExistingFolderButton() { const button = ( ) return ( {/* The popup lays its children out in a row — one span keeps the copy flowing as a sentence with the command inline. */} Not supported yet — run{' '} moi init {' '} in the folder from your console to add it ) }