|
| 1 | +import { findErrorTraceId } from "@t3tools/client-runtime/errors"; |
| 2 | +import { |
| 3 | + isAtomCommandInterrupted, |
| 4 | + squashAtomCommandFailure, |
| 5 | +} from "@t3tools/client-runtime/state/runtime"; |
| 6 | +import { T3CHAT_PLAN_TUNNEL_LIMITS, type RelayT3ChatLink } from "@t3tools/contracts/relay"; |
| 7 | +import { MessageSquareIcon } from "lucide-react"; |
| 8 | +import { useState } from "react"; |
| 9 | + |
| 10 | +import { |
| 11 | + createManagedRelayT3ChatLinkRequestCommand, |
| 12 | + unlinkManagedRelayT3ChatCommand, |
| 13 | + useManagedRelayT3ChatLink, |
| 14 | +} from "../../cloud/managedRelayState"; |
| 15 | +import { readLocalApi } from "../../localApi"; |
| 16 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 17 | +import { Button } from "../ui/button"; |
| 18 | +import { toastManager } from "../ui/toast"; |
| 19 | +import { |
| 20 | + ClerkUserProfilePage, |
| 21 | + ClerkUserProfileRefreshButton, |
| 22 | + ClerkUserProfileRow, |
| 23 | +} from "./ClerkUserProfilePage"; |
| 24 | + |
| 25 | +function planLabel(link: RelayT3ChatLink): string { |
| 26 | + switch (link.plan) { |
| 27 | + case "premier": |
| 28 | + return `Premier · up to ${T3CHAT_PLAN_TUNNEL_LIMITS.premier} tunnels`; |
| 29 | + case "pro": |
| 30 | + return `Pro · up to ${T3CHAT_PLAN_TUNNEL_LIMITS.pro} tunnels, or ${T3CHAT_PLAN_TUNNEL_LIMITS.premier} with Premier`; |
| 31 | + case "free": |
| 32 | + return "Upgrade on T3 Chat for more tunnels"; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function reportFailure(title: string, cause: unknown) { |
| 37 | + const traceId = findErrorTraceId(cause); |
| 38 | + toastManager.add({ |
| 39 | + type: "error", |
| 40 | + title, |
| 41 | + description: cause instanceof Error ? cause.message : "Try again.", |
| 42 | + data: traceId |
| 43 | + ? { |
| 44 | + secondaryActionProps: { |
| 45 | + children: "Copy trace ID", |
| 46 | + onClick: () => void navigator.clipboard?.writeText(traceId), |
| 47 | + }, |
| 48 | + } |
| 49 | + : undefined, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +/** Links a T3 Chat account so paid subscribers get more tunnels. */ |
| 54 | +export function T3ChatUserProfilePage() { |
| 55 | + const linkState = useManagedRelayT3ChatLink(); |
| 56 | + const createLinkRequest = useAtomCommand(createManagedRelayT3ChatLinkRequestCommand, { |
| 57 | + reportFailure: false, |
| 58 | + }); |
| 59 | + const unlink = useAtomCommand(unlinkManagedRelayT3ChatCommand, { reportFailure: false }); |
| 60 | + const [pending, setPending] = useState<"link" | "unlink" | null>(null); |
| 61 | + // Kept so a blocked popup still leaves the user a link to open. |
| 62 | + const [linkUrl, setLinkUrl] = useState<string | null>(null); |
| 63 | + |
| 64 | + const handleLink = async () => { |
| 65 | + const accountId = linkState.accountId; |
| 66 | + if (!accountId || pending) return; |
| 67 | + setPending("link"); |
| 68 | + const result = await createLinkRequest({ accountId }); |
| 69 | + if (result._tag === "Success") { |
| 70 | + setLinkUrl(result.value); |
| 71 | + await readLocalApi() |
| 72 | + ?.shell.openExternal(result.value) |
| 73 | + .catch(() => undefined); |
| 74 | + } else if (!isAtomCommandInterrupted(result)) { |
| 75 | + reportFailure("Could not start linking", squashAtomCommandFailure(result)); |
| 76 | + } |
| 77 | + setPending(null); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleUnlink = async () => { |
| 81 | + const accountId = linkState.accountId; |
| 82 | + if (!accountId || pending) return; |
| 83 | + setPending("unlink"); |
| 84 | + const result = await unlink({ accountId }); |
| 85 | + if (result._tag === "Success") { |
| 86 | + setLinkUrl(null); |
| 87 | + linkState.refresh(); |
| 88 | + } else if (!isAtomCommandInterrupted(result)) { |
| 89 | + reportFailure("Could not unlink T3 Chat", squashAtomCommandFailure(result)); |
| 90 | + } |
| 91 | + setPending(null); |
| 92 | + }; |
| 93 | + |
| 94 | + const data = linkState.data; |
| 95 | + const link = data?.link ?? null; |
| 96 | + |
| 97 | + return ( |
| 98 | + <ClerkUserProfilePage |
| 99 | + title="T3 Chat" |
| 100 | + description={`Link a paid T3 Chat account for more T3 Connect tunnels: up to ${T3CHAT_PLAN_TUNNEL_LIMITS.pro} with Pro, ${T3CHAT_PLAN_TUNNEL_LIMITS.premier} with Premier.`} |
| 101 | + action={ |
| 102 | + <ClerkUserProfileRefreshButton |
| 103 | + disabled={pending !== null} |
| 104 | + isPending={linkState.isPending} |
| 105 | + onClick={linkState.refresh} |
| 106 | + /> |
| 107 | + } |
| 108 | + > |
| 109 | + {linkState.error ? ( |
| 110 | + <div className="border-t py-3 text-xs" role="alert"> |
| 111 | + <p className="font-medium text-destructive-foreground">Could not load T3 Chat link</p> |
| 112 | + <p className="mt-0.5 text-muted-foreground">{linkState.error}</p> |
| 113 | + </div> |
| 114 | + ) : data === null ? null : !data.available ? ( |
| 115 | + <p className="border-t py-4 text-xs text-muted-foreground"> |
| 116 | + This T3 Connect server does not support T3 Chat linking. |
| 117 | + </p> |
| 118 | + ) : ( |
| 119 | + <ul className="border-t"> |
| 120 | + <ClerkUserProfileRow icon={<MessageSquareIcon className="size-4" />}> |
| 121 | + <div className="flex items-start gap-4"> |
| 122 | + <div className="min-w-0 flex-1"> |
| 123 | + <h3 className="truncate text-sm leading-4.5 font-medium text-foreground"> |
| 124 | + {link ? link.email : "Not linked"} |
| 125 | + </h3> |
| 126 | + <p className="mt-1 text-xs leading-4.5 text-muted-foreground"> |
| 127 | + {link ? ( |
| 128 | + planLabel(link) |
| 129 | + ) : linkUrl ? ( |
| 130 | + <> |
| 131 | + Confirm on{" "} |
| 132 | + <a |
| 133 | + className="underline" |
| 134 | + href={linkUrl} |
| 135 | + target="_blank" |
| 136 | + rel="noopener noreferrer" |
| 137 | + > |
| 138 | + t3.chat |
| 139 | + </a> |
| 140 | + , then select Refresh. |
| 141 | + </> |
| 142 | + ) : ( |
| 143 | + "You confirm the link on t3.chat." |
| 144 | + )} |
| 145 | + </p> |
| 146 | + </div> |
| 147 | + {link ? ( |
| 148 | + <Button |
| 149 | + size="sm" |
| 150 | + variant="destructive-outline" |
| 151 | + disabled={pending !== null} |
| 152 | + onClick={() => void handleUnlink()} |
| 153 | + > |
| 154 | + {pending === "unlink" ? "Unlinking…" : "Unlink"} |
| 155 | + </Button> |
| 156 | + ) : ( |
| 157 | + <Button size="sm" disabled={pending !== null} onClick={() => void handleLink()}> |
| 158 | + {pending === "link" ? "Opening…" : "Link T3 Chat"} |
| 159 | + </Button> |
| 160 | + )} |
| 161 | + </div> |
| 162 | + </ClerkUserProfileRow> |
| 163 | + </ul> |
| 164 | + )} |
| 165 | + </ClerkUserProfilePage> |
| 166 | + ); |
| 167 | +} |
0 commit comments