|
| 1 | +"use client"; |
| 2 | +import { createDedicatedSupportChannel } from "@/api/dedicated-support"; |
| 3 | +import type { Team } from "@/api/team"; |
| 4 | +import { SettingsCard } from "@/components/blocks/SettingsCard"; |
| 5 | +import { |
| 6 | + Select, |
| 7 | + SelectContent, |
| 8 | + SelectItem, |
| 9 | + SelectTrigger, |
| 10 | + SelectValue, |
| 11 | +} from "@/components/ui/select"; |
| 12 | +import { useDashboardRouter } from "@/lib/DashboardRouter"; |
| 13 | +import { useMutation } from "@tanstack/react-query"; |
| 14 | +import { useState } from "react"; |
| 15 | +import { toast } from "sonner"; |
| 16 | + |
| 17 | +const CHANNEL_TYPES = [ |
| 18 | + { name: "Slack", value: "slack" }, |
| 19 | + { name: "Telegram", value: "telegram" }, |
| 20 | +] as const; |
| 21 | +type ChannelType = (typeof CHANNEL_TYPES)[number]["value"]; |
| 22 | + |
| 23 | +interface DedicatedSupportFormProps { |
| 24 | + teamId: string; |
| 25 | + teamSlug: string; |
| 26 | + billingPlan: Team["billingPlan"]; |
| 27 | + channelType?: ChannelType; |
| 28 | + channelName?: string; |
| 29 | +} |
| 30 | + |
| 31 | +export function TeamDedicatedSupportCard({ |
| 32 | + teamId, |
| 33 | + teamSlug, |
| 34 | + billingPlan, |
| 35 | + channelType, |
| 36 | + channelName, |
| 37 | +}: DedicatedSupportFormProps) { |
| 38 | + const router = useDashboardRouter(); |
| 39 | + const [selectedChannelType, setSelectedChannelType] = useState<ChannelType>( |
| 40 | + CHANNEL_TYPES[0].value, |
| 41 | + ); |
| 42 | + |
| 43 | + const isFeatureEnabled = billingPlan === "scale" || billingPlan === "pro"; |
| 44 | + |
| 45 | + const createMutation = useMutation({ |
| 46 | + mutationFn: async (params: { |
| 47 | + teamId: string; |
| 48 | + channelType: "slack" | "telegram"; |
| 49 | + }) => { |
| 50 | + const res = await createDedicatedSupportChannel( |
| 51 | + params.teamId, |
| 52 | + params.channelType, |
| 53 | + ); |
| 54 | + if (res.error) { |
| 55 | + throw new Error(res.error); |
| 56 | + } |
| 57 | + }, |
| 58 | + onSuccess: () => { |
| 59 | + toast.success( |
| 60 | + "Dedicated support channel requested. Please check your email for an invite link shortly.", |
| 61 | + ); |
| 62 | + }, |
| 63 | + onError: (error) => { |
| 64 | + toast.error(error.message); |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + // Already set up. |
| 69 | + if (channelType && channelName) { |
| 70 | + return ( |
| 71 | + <SettingsCard |
| 72 | + header={{ |
| 73 | + title: "Dedicated Support", |
| 74 | + description: |
| 75 | + "Get a dedicated support channel with the thirdweb team.", |
| 76 | + }} |
| 77 | + errorText={undefined} |
| 78 | + noPermissionText={undefined} |
| 79 | + bottomText={undefined} |
| 80 | + > |
| 81 | + <div className="md:w-[450px]"> |
| 82 | + <p className="text-muted-foreground text-sm"> |
| 83 | + Your dedicated support channel: #<strong>{channelName}</strong> on{" "} |
| 84 | + {CHANNEL_TYPES.find((c) => c.value === channelType)?.name} |
| 85 | + </p> |
| 86 | + </div> |
| 87 | + </SettingsCard> |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + const renderContent = () => { |
| 92 | + return ( |
| 93 | + <> |
| 94 | + <div className="md:w-[450px]"> |
| 95 | + <Select |
| 96 | + onValueChange={(val) => setSelectedChannelType(val as ChannelType)} |
| 97 | + value={selectedChannelType} |
| 98 | + disabled={!isFeatureEnabled} |
| 99 | + > |
| 100 | + <SelectTrigger> |
| 101 | + <SelectValue placeholder="Select Channel Type" /> |
| 102 | + </SelectTrigger> |
| 103 | + <SelectContent> |
| 104 | + {CHANNEL_TYPES.map(({ name, value }) => ( |
| 105 | + <SelectItem key={value} value={value}> |
| 106 | + {name} |
| 107 | + </SelectItem> |
| 108 | + ))} |
| 109 | + </SelectContent> |
| 110 | + </Select> |
| 111 | + </div> |
| 112 | + <p className="mt-2 text-muted-foreground text-sm"> |
| 113 | + All team members for this team will be sent an invite link to their |
| 114 | + email. You can invite other members later. |
| 115 | + </p> |
| 116 | + </> |
| 117 | + ); |
| 118 | + }; |
| 119 | + |
| 120 | + return ( |
| 121 | + <SettingsCard |
| 122 | + header={{ |
| 123 | + title: "Dedicated Support", |
| 124 | + description: "Get a dedicated support channel with the thirdweb team.", |
| 125 | + }} |
| 126 | + errorText={undefined} |
| 127 | + noPermissionText={undefined} |
| 128 | + saveButton={ |
| 129 | + isFeatureEnabled |
| 130 | + ? { |
| 131 | + label: "Create Support Channel", |
| 132 | + onClick: () => |
| 133 | + createMutation.mutate({ |
| 134 | + teamId, |
| 135 | + channelType: selectedChannelType, |
| 136 | + }), |
| 137 | + disabled: createMutation.isPending, |
| 138 | + isPending: createMutation.isPending, |
| 139 | + } |
| 140 | + : { |
| 141 | + label: "Upgrade Plan", |
| 142 | + onClick: () => |
| 143 | + router.push( |
| 144 | + `/team/${teamSlug}/~/settings/billing?showPlans=true&highlight=scale`, |
| 145 | + ), |
| 146 | + disabled: false, |
| 147 | + isPending: false, |
| 148 | + } |
| 149 | + } |
| 150 | + bottomText={ |
| 151 | + !isFeatureEnabled |
| 152 | + ? "Upgrade to the Scale plan to enable this feature." |
| 153 | + : undefined |
| 154 | + } |
| 155 | + > |
| 156 | + {renderContent()} |
| 157 | + </SettingsCard> |
| 158 | + ); |
| 159 | +} |
0 commit comments