import Modal from "@/ui/shared/modal"; import { useState, Dispatch, SetStateAction, useCallback, useMemo, } from "react"; import { LoadingDots } from "@/ui/shared/icons"; import { fetcher } from "@/lib/utils"; import { Session } from "next-auth"; import { useUserInfoByEmail } from "@/app/post/[id]/request"; import shortid from "shortid"; import { IResponse } from "@/lib/types/response"; import { Collaboration } from "@prisma/client"; import toast from "react-hot-toast"; import { useRouter } from "next/navigation"; import { defaultEditorContent } from "@/lib/consts"; import { v4 as uuidv4 } from "uuid"; import { ContentItem } from "@/lib/types/note"; import { Shapes } from "lucide-react"; import Link from "next/link"; import { addNote } from "@/store/db.model"; const CreatRoomModal = ({ initTitle, localId, session, showEditModal, setShowEditModal, }: { initTitle: string; localId: string; session: Session | null; showEditModal: boolean; setShowEditModal: Dispatch>; }) => { const [title, setTitle] = useState(""); const [loading, setLoading] = useState(false); const [isSendSuccess, setIsSendSuccess] = useState(false); const router = useRouter(); const handleSubmit = async () => { if (!session?.user) { toast("Please login first"); return; } if (!title || title.length < 3 || title.length > 20) { toast("Invalid space name"); return; } setLoading(true); const new_localId = uuidv4(); const roomId = shortid.generate().replace("_", "A").replace("-", "a"); const res = await fetcher>( "/api/collaboration", { method: "POST", body: JSON.stringify({ roomId, localId: new_localId, // 本地创建新笔记关联空间 title, }), }, ); if (res.code !== 200) { toast(res.msg, { icon: "😅", }); } else { const date = new Date(); const newItem: ContentItem = { id: new_localId, title: `Untitled-${new_localId.slice(0, 6)}-${ date.getMonth() + 1 }/${date.getDate()}`, content: defaultEditorContent, tag: "", created_at: date.getTime(), updated_at: date.getTime(), }; addNote(newItem); toast.success(res.msg, { icon: "🎉", }); router.push(`/post/${new_localId}?work=${roomId}`); } if (res) { setLoading(false); setIsSendSuccess(true); setShowEditModal(false); } }; const handleKeydown = (key: string) => { if (key === "Enter") { handleSubmit(); } }; return (

Collaboration space

setTitle(e.target.value)} onKeyDown={(e) => handleKeydown(e.key)} />

You are creating a collaboration space, just enter the space name and click Create.

Once created successfully, it will automatically jump to your collaboration space and generate an{" "} invitation link, which allows you to invite your team to join the collaboration.

{" "} See more about{" "} collaboration {" "} space.

); }; export function useCreatRoomModal( session: Session | null, initTitle: string, localId: string, ) { const [showRoomModal, setShowRoomModal] = useState(false); const RoomModalCallback = useCallback(() => { return ( ); }, [showRoomModal, setShowRoomModal]); return useMemo( () => ({ setShowRoomModal, RoomModal: RoomModalCallback }), [setShowRoomModal, RoomModalCallback], ); }