-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathform-workspace-name.tsx
90 lines (84 loc) · 2.21 KB
/
form-workspace-name.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {
Card,
CardBody,
CardFooter,
Description,
FormDiscardChangesButton,
FormSubmitButton,
FormTextField,
FormV2,
Input,
Label,
} from '@stacklok/ui-kit'
import { useMutationCreateWorkspace } from '../hooks/use-mutation-create-workspace'
import { useNavigate } from 'react-router-dom'
import { twMerge } from 'tailwind-merge'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
const schema = z.object({
rename_to: z.string().nonempty(),
})
type FieldValues = z.infer<typeof schema>
const FIELD_NAME = schema.keyof().Enum
export function FormWorkspaceName({
className,
workspaceName,
isArchived,
}: {
className?: string
workspaceName: string
isArchived: boolean | undefined
}) {
const navigate = useNavigate()
const { mutateAsync, isPending } = useMutationCreateWorkspace()
const isDefault = workspaceName === 'default'
const isDisabled = isArchived || isPending || isDefault
const description: string | null = isDefault
? 'Cannot rename the default workspace'
: isArchived
? 'Cannot rename an archived workspace'
: null
const handleSubmit = ({ rename_to }: FieldValues) => {
mutateAsync(
{ body: { name: workspaceName, rename_to } },
{
onSuccess: () => {
navigate(`/workspace/${rename_to}`)
},
}
)
}
return (
<FormV2<FieldValues>
onSubmit={handleSubmit}
data-testid="workspace-name"
options={{
resolver: zodResolver(schema),
defaultValues: {
rename_to: workspaceName,
},
}}
>
<Card className={twMerge(className, 'shrink-0')}>
<CardBody>
<FormTextField
isDisabled={isDisabled}
key={workspaceName}
name={FIELD_NAME.rename_to}
isRequired
>
<Label>Workspace name</Label>
<Input />
{description ? (
<Description className="mt-2 block">{description}</Description>
) : null}
</FormTextField>
</CardBody>
<CardFooter className="justify-end gap-2">
<FormDiscardChangesButton />
<FormSubmitButton />
</CardFooter>
</Card>
</FormV2>
)
}