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