@@ -32,13 +32,43 @@ import {
3232 Undo2Icon ,
3333} from "lucide-react" ;
3434import { Doc } from "../../../../../convex/_generated/dataModel" ;
35+ import { useMutation } from "convex/react" ;
36+ import { api } from "../../../../../convex/_generated/api" ;
37+ import { useRouter } from "next/navigation" ;
38+ import { useToast } from "@/hooks/use-toast" ;
39+ import { RemoveDialog } from "@/components/remove-dialog" ;
40+ import { RenameDialog } from "@/components/rename-dialog" ;
3541
3642type Props = {
3743 data : Doc < "documents" > ;
3844} ;
3945
4046export const MenuBar : React . FC < Props > = ( { data } ) => {
47+ const router = useRouter ( ) ;
4148 const { editor } = useEditorStore ( ) ;
49+ const mutation = useMutation ( api . document . create ) ;
50+ const { toast } = useToast ( ) ;
51+
52+ const onNewDoc = ( ) => {
53+ mutation ( {
54+ title : "Untitled" ,
55+ initialContent : "" ,
56+ } )
57+ . then ( ( id ) => {
58+ toast ( {
59+ title : "New document created" ,
60+ description : "New document has been created." ,
61+ } ) ;
62+ router . push ( `/document/${ id } ` ) ;
63+ } )
64+ . catch ( ( ) => {
65+ toast ( {
66+ title : "Document creation failed" ,
67+ description : "Your document could not be created" ,
68+ variant : "destructive" ,
69+ } ) ;
70+ } ) ;
71+ } ;
4272
4373 const addTable = ( { rows, cols } : { rows : number ; cols : number } ) => {
4474 editor
@@ -129,22 +159,32 @@ export const MenuBar: React.FC<Props> = ({ data }) => {
129159 </ MenubarSubContent >
130160 </ MenubarSub >
131161
132- < MenubarItem >
162+ < MenubarItem onClick = { onNewDoc } >
133163 < FilePlusIcon className = "size-4 mr-2" />
134164 New Document
135165 </ MenubarItem >
136166
137167 < MenubarSeparator />
138168
139- < MenubarItem >
140- < FilePenIcon className = "size-4 mr-2" />
141- Rename
142- </ MenubarItem >
169+ < RenameDialog documentId = { data . _id } title = { data . title } >
170+ < MenubarItem
171+ onClick = { ( e ) => e . stopPropagation ( ) }
172+ onSelect = { ( e ) => e . preventDefault ( ) }
173+ >
174+ < FilePenIcon className = "size-4 mr-2" />
175+ Rename
176+ </ MenubarItem >
177+ </ RenameDialog >
143178
144- < MenubarItem >
145- < TrashIcon className = "size-4 mr-2" />
146- Remove
147- </ MenubarItem >
179+ < RemoveDialog documentId = { data . _id } >
180+ < MenubarItem
181+ onClick = { ( e ) => e . stopPropagation ( ) }
182+ onSelect = { ( e ) => e . preventDefault ( ) }
183+ >
184+ < TrashIcon className = "size-4 mr-2" />
185+ Remove
186+ </ MenubarItem >
187+ </ RemoveDialog >
148188
149189 < MenubarSeparator />
150190 < MenubarItem onClick = { ( ) => window ?. print ( ) } >
0 commit comments