|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { createClient } from "@/lib/supabase/client"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import { Label } from "@/components/ui/label"; |
| 9 | +import { |
| 10 | + Dialog, |
| 11 | + DialogContent, |
| 12 | + DialogDescription, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | +} from "@/components/ui/dialog"; |
| 17 | +import { generateSlug } from "@/lib/workspace-utils"; |
| 18 | + |
| 19 | +interface CreateWorkspaceDialogProps { |
| 20 | + open: boolean; |
| 21 | + onOpenChange: (open: boolean) => void; |
| 22 | + userId: string; |
| 23 | +} |
| 24 | + |
| 25 | +export function CreateWorkspaceDialog({ |
| 26 | + open, |
| 27 | + onOpenChange, |
| 28 | + userId, |
| 29 | +}: CreateWorkspaceDialogProps) { |
| 30 | + const router = useRouter(); |
| 31 | + const [name, setName] = useState(""); |
| 32 | + const [error, setError] = useState<string | null>(null); |
| 33 | + const [loading, setLoading] = useState(false); |
| 34 | + |
| 35 | + function handleClose() { |
| 36 | + setName(""); |
| 37 | + setError(null); |
| 38 | + onOpenChange(false); |
| 39 | + } |
| 40 | + |
| 41 | + async function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 42 | + e.preventDefault(); |
| 43 | + setError(null); |
| 44 | + |
| 45 | + const trimmed = name.trim(); |
| 46 | + if (!trimmed) { |
| 47 | + setError("Name is required."); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const slug = generateSlug(trimmed); |
| 52 | + if (!slug) { |
| 53 | + setError("Name must contain at least one alphanumeric character."); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + setLoading(true); |
| 58 | + const supabase = createClient(); |
| 59 | + |
| 60 | + // Append a random suffix to ensure slug uniqueness |
| 61 | + const uniqueSlug = `${slug}-${crypto.randomUUID().slice(0, 6)}`; |
| 62 | + |
| 63 | + const { data, error: insertError } = await supabase |
| 64 | + .from("workspaces") |
| 65 | + .insert({ |
| 66 | + name: trimmed, |
| 67 | + slug: uniqueSlug, |
| 68 | + is_personal: false, |
| 69 | + created_by: userId, |
| 70 | + }) |
| 71 | + .select("id, slug") |
| 72 | + .single(); |
| 73 | + |
| 74 | + if (insertError) { |
| 75 | + // DB trigger returns P0001 when limit is reached |
| 76 | + if (insertError.message.includes("Workspace limit reached")) { |
| 77 | + setError("You can create at most 3 workspaces."); |
| 78 | + } else if (insertError.message.includes("duplicate key")) { |
| 79 | + setError("A workspace with this slug already exists. Try a different name."); |
| 80 | + } else { |
| 81 | + setError(insertError.message); |
| 82 | + } |
| 83 | + setLoading(false); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Create owner membership for the new workspace |
| 88 | + await supabase.from("members").insert({ |
| 89 | + workspace_id: data.id, |
| 90 | + user_id: userId, |
| 91 | + role: "owner", |
| 92 | + joined_at: new Date().toISOString(), |
| 93 | + }); |
| 94 | + |
| 95 | + setLoading(false); |
| 96 | + handleClose(); |
| 97 | + router.push(`/${data.slug}`); |
| 98 | + router.refresh(); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 103 | + <DialogContent> |
| 104 | + <DialogHeader> |
| 105 | + <DialogTitle>Create workspace</DialogTitle> |
| 106 | + <DialogDescription> |
| 107 | + Add a new workspace to organize your pages. |
| 108 | + </DialogDescription> |
| 109 | + </DialogHeader> |
| 110 | + <form onSubmit={handleSubmit} className="flex flex-col gap-3"> |
| 111 | + <div className="flex flex-col gap-1.5"> |
| 112 | + <Label htmlFor="workspace-name">Name</Label> |
| 113 | + <Input |
| 114 | + id="workspace-name" |
| 115 | + placeholder="My Workspace" |
| 116 | + value={name} |
| 117 | + onChange={(e) => setName(e.target.value)} |
| 118 | + required |
| 119 | + autoFocus |
| 120 | + autoComplete="off" |
| 121 | + /> |
| 122 | + {name.trim() && ( |
| 123 | + <p className="text-xs text-muted-foreground"> |
| 124 | + Slug: {generateSlug(name.trim()) || "—"} |
| 125 | + </p> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + {error && <p className="text-xs text-destructive">{error}</p>} |
| 129 | + <DialogFooter> |
| 130 | + <Button |
| 131 | + type="button" |
| 132 | + variant="outline" |
| 133 | + onClick={handleClose} |
| 134 | + disabled={loading} |
| 135 | + > |
| 136 | + Cancel |
| 137 | + </Button> |
| 138 | + <Button type="submit" disabled={loading || !name.trim()}> |
| 139 | + {loading ? "Creating…" : "Create"} |
| 140 | + </Button> |
| 141 | + </DialogFooter> |
| 142 | + </form> |
| 143 | + </DialogContent> |
| 144 | + </Dialog> |
| 145 | + ); |
| 146 | +} |
0 commit comments