|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | +import { useTranslations } from "next-intl"; |
| 5 | +import { useAuth } from "@clerk/nextjs"; |
| 6 | +import { usePathname, useRouter, useSearchParams } from "next/navigation"; |
| 7 | +import { useQuery, useSuspenseQuery } from "@tanstack/react-query"; |
| 8 | +import { useToast } from "@/hooks/use-toast"; |
| 9 | +import { useStartCommunityStripeOnboarding } from "@/lib/mutations/stripe-onboarding"; |
| 10 | +import { captureException } from "@/lib/report"; |
| 11 | +import { |
| 12 | + communityPayoutStatus, |
| 13 | + communityUserRoles, |
| 14 | +} from "@/lib/queries/community"; |
| 15 | +import { userInfoOptions } from "@/lib/queries/user"; |
| 16 | +import SettingsSection from "@/components/layout/settings-section"; |
| 17 | +import { Card } from "@/components/widgets/cards/card"; |
| 18 | +import Heading from "@/components/widgets/texts/heading"; |
| 19 | +import { Badge } from "@/components/shadcn/badge"; |
| 20 | +import { Button } from "@/components/shadcn/button"; |
| 21 | +import { DateTimeText } from "@/components/widgets/date-time-text"; |
| 22 | +import { useDashboardCommunityContext } from "@/components/providers/dashboard-community-context-provider"; |
| 23 | + |
| 24 | +const payoutStatusBadge = { |
| 25 | + verified: "secondary", |
| 26 | + failed: "destructive", |
| 27 | + pending: "outline", |
| 28 | + unknown: "outline", |
| 29 | +} as const; |
| 30 | + |
| 31 | +export type PayoutStatus = keyof typeof payoutStatusBadge; |
| 32 | + |
| 33 | +export default function PayoutsConfiguration() { |
| 34 | + const { communityId } = useDashboardCommunityContext(); |
| 35 | + const { getToken, userId } = useAuth(); |
| 36 | + const { toast } = useToast(); |
| 37 | + const router = useRouter(); |
| 38 | + const pathname = usePathname(); |
| 39 | + const searchParams = useSearchParams(); |
| 40 | + const handledStripeParams = useRef(false); |
| 41 | + const t = useTranslations("community-form"); |
| 42 | + const tEdit = useTranslations("community-edit-form"); |
| 43 | + |
| 44 | + const { data: userInfo } = useSuspenseQuery( |
| 45 | + userInfoOptions(getToken, userId), |
| 46 | + ); |
| 47 | + const { data: userRoles = [] } = useSuspenseQuery( |
| 48 | + communityUserRoles(communityId, userInfo?.userId || ""), |
| 49 | + ); |
| 50 | + const isAdmin = userRoles.includes("administrator"); |
| 51 | + |
| 52 | + const { data: payoutStatus, isLoading: isPayoutStatusLoading } = useQuery({ |
| 53 | + ...communityPayoutStatus(communityId, getToken), |
| 54 | + enabled: isAdmin, |
| 55 | + }); |
| 56 | + const { |
| 57 | + mutateAsync: startCommunityStripeOnboarding, |
| 58 | + isPending: isStripeOnboardingPending, |
| 59 | + } = useStartCommunityStripeOnboarding(); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + if (handledStripeParams.current) return; |
| 63 | + const stripeParam = searchParams.get("stripe"); |
| 64 | + if (!stripeParam) return; |
| 65 | + |
| 66 | + handledStripeParams.current = true; |
| 67 | + if (stripeParam === "return") { |
| 68 | + toast({ |
| 69 | + title: tEdit("stripe-onboarding-return"), |
| 70 | + variant: "default", |
| 71 | + }); |
| 72 | + } else if (stripeParam === "refresh") { |
| 73 | + toast({ |
| 74 | + variant: "destructive", |
| 75 | + title: tEdit("stripe-onboarding-refresh"), |
| 76 | + }); |
| 77 | + } |
| 78 | + router.replace(pathname); |
| 79 | + }, [pathname, router, searchParams, tEdit, toast]); |
| 80 | + |
| 81 | + const handleStartCommunityStripeOnboarding = async () => { |
| 82 | + try { |
| 83 | + const token = await getToken(); |
| 84 | + if (!token) throw new Error("invalid clerk token"); |
| 85 | + |
| 86 | + const returnPath = `/dashboard/community/${communityId}/payouts?stripe=return`; |
| 87 | + const refreshPath = `/dashboard/community/${communityId}/payouts?stripe=refresh`; |
| 88 | + |
| 89 | + const onboardingUrl = await startCommunityStripeOnboarding({ |
| 90 | + token, |
| 91 | + communityId, |
| 92 | + returnPath, |
| 93 | + refreshPath, |
| 94 | + }); |
| 95 | + |
| 96 | + if (!onboardingUrl) { |
| 97 | + throw new Error("missing onboarding url"); |
| 98 | + } |
| 99 | + |
| 100 | + window.location.href = onboardingUrl; |
| 101 | + } catch (err) { |
| 102 | + captureException(err); |
| 103 | + toast({ |
| 104 | + variant: "destructive", |
| 105 | + title: tEdit("stripe-onboarding-failure"), |
| 106 | + }); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + if (!isAdmin) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + const statusMissingAccount = "missing_account"; |
| 115 | + const lastVerifiedAtSeconds = payoutStatus?.lastVerifiedAt |
| 116 | + ? Number(payoutStatus.lastVerifiedAt) |
| 117 | + : 0; |
| 118 | + const hasLastVerifiedAt = |
| 119 | + payoutStatus?.lastVerifiedAt != null && lastVerifiedAtSeconds > 0; |
| 120 | + const isOnboardingComplete = |
| 121 | + payoutStatus?.onboardingState === "completed" || |
| 122 | + payoutStatus?.verificationState === "verified"; |
| 123 | + const isMissingStripeAccountId = |
| 124 | + isOnboardingComplete && !payoutStatus?.platformAccountId; |
| 125 | + const isStripeAccountMissingError = |
| 126 | + payoutStatus?.verificationState === statusMissingAccount; |
| 127 | + const shouldShowRefreshError = |
| 128 | + !!payoutStatus?.refreshError && !isStripeAccountMissingError; |
| 129 | + const payoutStatusLabel = (payoutStatus?.verificationState ?? |
| 130 | + "unknown") as PayoutStatus; |
| 131 | + const payoutStatusText: Record<PayoutStatus, string> = { |
| 132 | + verified: t("payout-status-verified"), |
| 133 | + failed: t("payout-status-failed"), |
| 134 | + pending: t("payout-status-pending"), |
| 135 | + unknown: t("payout-status-unknown"), |
| 136 | + }; |
| 137 | + const payoutStatusKey = payoutStatusText[payoutStatusLabel ?? ""] |
| 138 | + ? payoutStatusLabel |
| 139 | + : "unknown"; |
| 140 | + |
| 141 | + return ( |
| 142 | + <SettingsSection |
| 143 | + title={t("payments-section")} |
| 144 | + description={t("payments-description")} |
| 145 | + > |
| 146 | + <Card className="p-6"> |
| 147 | + <div className="flex flex-col gap-2"> |
| 148 | + <Heading level={4}>{t("stripe-connect-label")}</Heading> |
| 149 | + <p className="text-sm text-muted-foreground"> |
| 150 | + {t("stripe-connect-description")} |
| 151 | + </p> |
| 152 | + <div className="pt-2"> |
| 153 | + <div className="flex items-center gap-2"> |
| 154 | + <Heading level={5}>{t("payout-status-label")}</Heading> |
| 155 | + {isPayoutStatusLoading || payoutStatusKey === undefined ? ( |
| 156 | + <Badge variant="outline">{t("payout-status-loading")}</Badge> |
| 157 | + ) : ( |
| 158 | + <Badge |
| 159 | + variant={ |
| 160 | + payoutStatusBadge[payoutStatusKey as PayoutStatus] ?? |
| 161 | + "outline" |
| 162 | + } |
| 163 | + > |
| 164 | + {payoutStatusText[payoutStatusKey]} |
| 165 | + </Badge> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + <div className="flex flex-col gap-1 text-sm text-muted-foreground pt-2"> |
| 169 | + <span>{t("payout-status-last-checked")}</span> |
| 170 | + {hasLastVerifiedAt ? ( |
| 171 | + <DateTimeText datetime={lastVerifiedAtSeconds} /> |
| 172 | + ) : ( |
| 173 | + <span>{t("payout-status-never")}</span> |
| 174 | + )} |
| 175 | + {payoutStatus?.isStale && <span>{t("payout-status-stale")}</span>} |
| 176 | + {shouldShowRefreshError && ( |
| 177 | + <span>{t("payout-status-refresh-error")}</span> |
| 178 | + )} |
| 179 | + {(isMissingStripeAccountId || isStripeAccountMissingError) && ( |
| 180 | + <span>{t("stripe-dashboard-missing-account")}</span> |
| 181 | + )} |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + <div className="pt-2"> |
| 185 | + <Button |
| 186 | + type="button" |
| 187 | + onClick={() => { |
| 188 | + if (isOnboardingComplete) { |
| 189 | + window.open( |
| 190 | + process.env.NEXT_PUBLIC_STRIPE_DASHBOARD_URL, |
| 191 | + "_blank", |
| 192 | + ); |
| 193 | + return; |
| 194 | + } |
| 195 | + void handleStartCommunityStripeOnboarding(); |
| 196 | + }} |
| 197 | + disabled={ |
| 198 | + isStripeOnboardingPending || |
| 199 | + isPayoutStatusLoading || |
| 200 | + isMissingStripeAccountId |
| 201 | + } |
| 202 | + > |
| 203 | + {isOnboardingComplete |
| 204 | + ? t("stripe-dashboard-cta") |
| 205 | + : t("stripe-connect-cta")} |
| 206 | + </Button> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + </Card> |
| 210 | + </SettingsSection> |
| 211 | + ); |
| 212 | +} |
0 commit comments