|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { LoadingSpinner } from "@dub/ui"; |
| 4 | +import { cn } from "@dub/utils"; |
| 5 | +import { useState } from "react"; |
| 6 | +import { useFormStatus } from "react-dom"; |
| 7 | +import { toast } from "sonner"; |
| 8 | + |
| 9 | +export function SlackSupportInvite() { |
| 10 | + const [needsChannelId, setNeedsChannelId] = useState(false); |
| 11 | + |
| 12 | + return ( |
| 13 | + <div className="flex flex-col space-y-5"> |
| 14 | + <form |
| 15 | + action={async (data) => { |
| 16 | + try { |
| 17 | + const res = await fetch("/api/admin/slack-support-invite", { |
| 18 | + method: "POST", |
| 19 | + body: JSON.stringify({ |
| 20 | + email: data.get("email"), |
| 21 | + workspaceSlug: data.get("workspaceSlug"), |
| 22 | + channelId: data.get("channelId") || undefined, |
| 23 | + }), |
| 24 | + }); |
| 25 | + |
| 26 | + const json = await res.json().catch(() => ({})); |
| 27 | + |
| 28 | + if (!res.ok) { |
| 29 | + if (json.nameTaken) { |
| 30 | + setNeedsChannelId(true); |
| 31 | + } |
| 32 | + toast.error( |
| 33 | + json.error ?? "Something went wrong. Please try again.", |
| 34 | + ); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + setNeedsChannelId(false); |
| 39 | + toast.success(`Slack invite sent (ID: ${json.inviteId})`); |
| 40 | + } catch { |
| 41 | + toast.error( |
| 42 | + "Network error. Please check your connection and try again.", |
| 43 | + ); |
| 44 | + } |
| 45 | + }} |
| 46 | + > |
| 47 | + <Form needsChannelId={needsChannelId} /> |
| 48 | + </form> |
| 49 | + </div> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +const Form = ({ needsChannelId }: { needsChannelId: boolean }) => { |
| 54 | + const { pending } = useFormStatus(); |
| 55 | + |
| 56 | + const inputClass = cn( |
| 57 | + "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm", |
| 58 | + pending && "bg-neutral-100", |
| 59 | + ); |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="flex flex-col gap-2"> |
| 63 | + <div className="relative flex w-full rounded-md shadow-sm"> |
| 64 | + <input |
| 65 | + name="email" |
| 66 | + id="email" |
| 67 | + type="email" |
| 68 | + required |
| 69 | + disabled={pending} |
| 70 | + autoComplete="off" |
| 71 | + className={inputClass} |
| 72 | + placeholder="user@example.com" |
| 73 | + /> |
| 74 | + {pending && ( |
| 75 | + <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-neutral-400" /> |
| 76 | + )} |
| 77 | + </div> |
| 78 | + |
| 79 | + <div className="relative flex w-full rounded-md shadow-sm"> |
| 80 | + <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm"> |
| 81 | + app.dub.co/ |
| 82 | + </span> |
| 83 | + <input |
| 84 | + name="workspaceSlug" |
| 85 | + id="workspaceSlug" |
| 86 | + type="text" |
| 87 | + required |
| 88 | + disabled={pending} |
| 89 | + autoComplete="off" |
| 90 | + className={cn( |
| 91 | + "block w-full rounded-r-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm", |
| 92 | + pending && "bg-neutral-100", |
| 93 | + )} |
| 94 | + placeholder="acme" |
| 95 | + /> |
| 96 | + {pending && ( |
| 97 | + <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-neutral-400" /> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + |
| 101 | + {needsChannelId && ( |
| 102 | + <div className="relative flex w-full rounded-md shadow-sm"> |
| 103 | + <input |
| 104 | + name="channelId" |
| 105 | + id="channelId" |
| 106 | + type="text" |
| 107 | + required |
| 108 | + disabled={pending} |
| 109 | + autoComplete="off" |
| 110 | + className={inputClass} |
| 111 | + placeholder="Slack channel ID (e.g. C01234ABCDE)" |
| 112 | + pattern="^[CG][A-Z0-9]{8,}$" |
| 113 | + /> |
| 114 | + {pending && ( |
| 115 | + <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-neutral-400" /> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + )} |
| 119 | + |
| 120 | + <button |
| 121 | + type="submit" |
| 122 | + disabled={pending} |
| 123 | + className={cn( |
| 124 | + "rounded-md bg-neutral-900 px-4 py-2 text-sm font-medium text-white hover:bg-neutral-700 focus:outline-none", |
| 125 | + pending && "opacity-50", |
| 126 | + )} |
| 127 | + > |
| 128 | + {pending ? "Sending…" : "Send invite"} |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
0 commit comments