|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogContent, |
| 5 | + DialogDescription, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | + DialogTrigger, |
| 10 | +} from "@/components/ui/dialog"; |
| 11 | +import { Input } from "@/components/ui/input"; |
| 12 | +import { Label } from "@/components/ui/label"; |
| 13 | +import { useTeam } from "@/context/team-context"; |
| 14 | +import { useState } from "react"; |
| 15 | +import { toast } from "sonner"; |
| 16 | +import { UpgradePlanModal } from "../billing/upgrade-plan-modal"; |
| 17 | +import { usePlan } from "@/lib/swr/use-billing"; |
| 18 | +import { useAnalytics } from "@/lib/analytics"; |
| 19 | +import { mutate } from "swr"; |
| 20 | + |
| 21 | +export function AddDataroomModal({ children }: { children?: React.ReactNode }) { |
| 22 | + const [dataroomName, setDataroomName] = useState<string>(""); |
| 23 | + const [loading, setLoading] = useState<boolean>(false); |
| 24 | + const [open, setOpen] = useState<boolean>(false); |
| 25 | + |
| 26 | + const teamInfo = useTeam(); |
| 27 | + const { plan } = usePlan(); |
| 28 | + const analytics = useAnalytics(); |
| 29 | + |
| 30 | + const handleSubmit = async (event: any) => { |
| 31 | + event.preventDefault(); |
| 32 | + event.stopPropagation(); |
| 33 | + |
| 34 | + if (dataroomName == "") return; |
| 35 | + |
| 36 | + setLoading(true); |
| 37 | + |
| 38 | + try { |
| 39 | + const response = await fetch( |
| 40 | + `/api/teams/${teamInfo?.currentTeam?.id}/datarooms`, |
| 41 | + { |
| 42 | + method: "POST", |
| 43 | + headers: { |
| 44 | + "Content-Type": "application/json", |
| 45 | + }, |
| 46 | + body: JSON.stringify({ |
| 47 | + name: dataroomName, |
| 48 | + }), |
| 49 | + }, |
| 50 | + ); |
| 51 | + |
| 52 | + if (!response.ok) { |
| 53 | + const { message } = await response.json(); |
| 54 | + setLoading(false); |
| 55 | + toast.error(message); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + analytics.capture("Dataroom Created", { dataroomName: dataroomName }); |
| 60 | + toast.success("Dataroom successfully created! 🎉"); |
| 61 | + |
| 62 | + mutate(`/api/teams/${teamInfo?.currentTeam?.id}/datarooms`); |
| 63 | + } catch (error) { |
| 64 | + setLoading(false); |
| 65 | + toast.error("Error adding folder. Please try again."); |
| 66 | + return; |
| 67 | + } finally { |
| 68 | + setLoading(false); |
| 69 | + setOpen(false); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + // If the team is on a free plan, show the upgrade modal |
| 74 | + if (plan && plan.plan === "free") { |
| 75 | + if (children) { |
| 76 | + return ( |
| 77 | + <UpgradePlanModal clickedPlan="Pro" trigger={"add_dataroom_overview"}> |
| 78 | + {children} |
| 79 | + </UpgradePlanModal> |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <Dialog open={open} onOpenChange={setOpen}> |
| 86 | + <DialogTrigger asChild>{children}</DialogTrigger> |
| 87 | + <DialogContent className="sm:max-w-[425px]"> |
| 88 | + <DialogHeader className="text-start"> |
| 89 | + <DialogTitle>Create dataroom</DialogTitle> |
| 90 | + <DialogDescription> |
| 91 | + Start creating a dataroom with a name. |
| 92 | + </DialogDescription> |
| 93 | + </DialogHeader> |
| 94 | + <form onSubmit={handleSubmit}> |
| 95 | + <Label htmlFor="dataroom-name" className="opacity-80"> |
| 96 | + Dataroom Name |
| 97 | + </Label> |
| 98 | + <Input |
| 99 | + id="dataroom-name" |
| 100 | + placeholder="ACME Aquisition" |
| 101 | + className="w-full mt-1 mb-4" |
| 102 | + onChange={(e) => setDataroomName(e.target.value)} |
| 103 | + /> |
| 104 | + <DialogFooter> |
| 105 | + <Button type="submit" className="w-full h-9" loading={loading}> |
| 106 | + Add new dataroom |
| 107 | + </Button> |
| 108 | + </DialogFooter> |
| 109 | + </form> |
| 110 | + </DialogContent> |
| 111 | + </Dialog> |
| 112 | + ); |
| 113 | +} |
0 commit comments