2024-08-04 11:16:51 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { useWorkspace } from '@/contexts/workspace';
|
2024-08-01 15:24:59 +02:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
2024-08-04 11:16:51 +02:00
|
|
|
DialogTitle,
|
|
|
|
|
} from '@/components/ui/dialog';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from '@/components/ui/form';
|
|
|
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Spinner } from '@/components/ui/spinner';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
import { Node } from '@/types/nodes';
|
|
|
|
|
import { NeuronId } from '@/lib/id';
|
2024-08-05 13:51:04 +02:00
|
|
|
import { generateKeyBetween } from 'fractional-indexing-jittered';
|
2024-08-01 15:24:59 +02:00
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
|
name: z.string().min(3, 'Name must be at least 3 characters long.'),
|
|
|
|
|
description: z.string(),
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-01 16:17:59 +02:00
|
|
|
interface SpaceCreateDialogProps {
|
2024-08-01 15:24:59 +02:00
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
2024-08-01 16:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const SpaceCreateDialog = ({
|
2024-08-04 11:16:51 +02:00
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
2024-08-01 16:17:59 +02:00
|
|
|
}: SpaceCreateDialogProps) => {
|
2024-08-01 15:24:59 +02:00
|
|
|
const workspace = useWorkspace();
|
|
|
|
|
const [isPending, setIsPending] = React.useState(false);
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
|
defaultValues: {
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-01 16:30:10 +02:00
|
|
|
const handleCancel = () => {
|
2024-08-01 15:24:59 +02:00
|
|
|
form.reset();
|
|
|
|
|
onOpenChange(false);
|
2024-08-04 11:16:51 +02:00
|
|
|
};
|
2024-08-01 15:24:59 +02:00
|
|
|
|
2024-08-01 16:30:10 +02:00
|
|
|
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
|
2024-08-01 15:24:59 +02:00
|
|
|
setIsPending(true);
|
|
|
|
|
|
2024-08-05 13:51:04 +02:00
|
|
|
const spaceNode: Node = {
|
|
|
|
|
id: NeuronId.generate(NeuronId.Type.Space),
|
|
|
|
|
type: 'space',
|
|
|
|
|
parentId: null,
|
|
|
|
|
attrs: {
|
|
|
|
|
name: values.name,
|
|
|
|
|
description: values.description,
|
|
|
|
|
},
|
|
|
|
|
workspaceId: workspace.id,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
createdBy: workspace.userNodeId,
|
|
|
|
|
versionId: NeuronId.generate(NeuronId.Type.Version),
|
|
|
|
|
};
|
2024-08-02 19:59:09 +02:00
|
|
|
const pageNode: Node = {
|
|
|
|
|
id: NeuronId.generate(NeuronId.Type.Page),
|
|
|
|
|
type: 'page',
|
|
|
|
|
attrs: {
|
2024-08-04 11:16:51 +02:00
|
|
|
name: 'Home',
|
2024-08-02 19:59:09 +02:00
|
|
|
},
|
2024-08-05 13:51:04 +02:00
|
|
|
index: generateKeyBetween(null, null),
|
2024-08-02 19:59:09 +02:00
|
|
|
workspaceId: workspace.id,
|
2024-08-05 13:51:04 +02:00
|
|
|
parentId: spaceNode.id,
|
2024-08-02 19:59:09 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
createdBy: workspace.userNodeId,
|
2024-08-04 11:16:51 +02:00
|
|
|
versionId: NeuronId.generate(NeuronId.Type.Version),
|
2024-08-02 19:59:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const channelNode: Node = {
|
|
|
|
|
id: NeuronId.generate(NeuronId.Type.Channel),
|
|
|
|
|
type: 'channel',
|
|
|
|
|
attrs: {
|
2024-08-04 11:16:51 +02:00
|
|
|
name: 'Discussions',
|
2024-08-02 19:59:09 +02:00
|
|
|
},
|
2024-08-05 13:51:04 +02:00
|
|
|
index: generateKeyBetween(pageNode.index, null),
|
2024-08-02 19:59:09 +02:00
|
|
|
workspaceId: workspace.id,
|
2024-08-05 13:51:04 +02:00
|
|
|
parentId: spaceNode.id,
|
2024-08-01 15:24:59 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
createdBy: workspace.userNodeId,
|
2024-08-04 11:16:51 +02:00
|
|
|
versionId: NeuronId.generate(NeuronId.Type.Version),
|
2024-08-02 19:59:09 +02:00
|
|
|
};
|
2024-08-01 15:24:59 +02:00
|
|
|
|
2024-08-02 19:59:09 +02:00
|
|
|
await workspace.addNodes([spaceNode, pageNode, channelNode]);
|
2024-08-01 15:24:59 +02:00
|
|
|
setIsPending(false);
|
|
|
|
|
onOpenChange(false);
|
2024-08-04 11:16:51 +02:00
|
|
|
};
|
2024-08-01 15:24:59 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Create space</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
Create a new space to collaborate with your peers
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form
|
|
|
|
|
className="flex flex-col"
|
|
|
|
|
onSubmit={form.handleSubmit(handleSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex-grow space-y-4 py-2 pb-4">
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="name"
|
2024-08-04 11:16:51 +02:00
|
|
|
render={({ field }) => (
|
2024-08-01 15:24:59 +02:00
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>Name *</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input placeholder="Name" {...field} />
|
|
|
|
|
</FormControl>
|
2024-08-04 11:16:51 +02:00
|
|
|
<FormMessage />
|
2024-08-01 15:24:59 +02:00
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="description"
|
2024-08-04 11:16:51 +02:00
|
|
|
render={({ field }) => (
|
2024-08-01 15:24:59 +02:00
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Description</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Textarea
|
|
|
|
|
placeholder="Write a short description about the workspace"
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
2024-08-04 11:16:51 +02:00
|
|
|
<FormMessage />
|
2024-08-01 15:24:59 +02:00
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={handleCancel}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
2024-08-02 08:43:44 +02:00
|
|
|
<Button type="submit" disabled={isPending}>
|
2024-08-01 15:24:59 +02:00
|
|
|
{isPending && <Spinner className="mr-1" />}
|
|
|
|
|
Create
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
2024-08-04 11:16:51 +02:00
|
|
|
};
|