-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworkspace-name.tsx
91 lines (86 loc) · 2.55 KB
/
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
91
import {
Card,
CardBody,
CardFooter,
Form,
Input,
Label,
TextField,
} from "@stacklok/ui-kit";
import { useMutationCreateWorkspace } from "../hooks/use-mutation-create-workspace";
import { useNavigate } from "react-router-dom";
import { twMerge } from "tailwind-merge";
import { useFormState } from "@/hooks/useFormState";
import { FormButtons } from "@/components/FormButtons";
import { FormEvent, useEffect } from "react";
export function WorkspaceName({
className,
workspaceName,
isArchived,
}: {
className?: string
workspaceName: string
isArchived: boolean | undefined
}) {
const navigate = useNavigate()
const { mutateAsync, isPending, error } = useMutationCreateWorkspace()
const errorMsg = error?.detail ? `${error?.detail}` : ''
const formState = useFormState({
workspaceName,
});
const { values, updateFormValues, setInitialValues } = formState;
const isDefault = workspaceName === "default";
const isUneditable = isArchived || isPending || isDefault;
useEffect(() => {
setInitialValues({ workspaceName });
}, [setInitialValues, workspaceName]);
const handleSubmit = (event: FormEvent) => {
event.preventDefault()
mutateAsync(
{ body: { name: workspaceName, rename_to: values.workspaceName } },
{
onSuccess: () => {
formState.setInitialValues({ workspaceName: values.workspaceName });
navigate(`/workspace/${values.workspaceName}`);
},
},
);
};
return (
<Form
onSubmit={handleSubmit}
validationBehavior="aria"
data-testid="workspace-name"
>
<Card className={twMerge(className, 'shrink-0')}>
<CardBody className="flex flex-col gap-6">
<TextField
isReadOnly={isUneditable}
key={workspaceName}
aria-label="Workspace name"
value={values.workspaceName}
name="Workspace name"
validationBehavior="aria"
isRequired
isDisabled={isUneditable}
onChange={(workspaceName) => updateFormValues({ workspaceName })}
>
<Label>Workspace name</Label>
<Input />
</TextField>
</CardBody>
<CardFooter className="justify-end">
<FormButtons
isPending={isPending}
formErrorMessage={errorMsg}
formSideNote={
isDefault ? 'Cannot rename the default workspace' : undefined
}
formState={formState}
canSubmit={!isArchived}
/>
</CardFooter>
</Card>
</Form>
)
}