|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import { runAsynchronouslyWithAlert } from "@stackframe/stack-shared/dist/utils/promises"; |
3 | 4 | import { Skeleton, Typography } from '@stackframe/stack-ui'; |
4 | 5 | import { Contact, ShieldCheck, Bell, Monitor, Key, Settings, CirclePlus, CreditCard } from 'lucide-react'; |
5 | | -import React, { Suspense } from "react"; |
| 6 | +import React, { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react"; |
6 | 7 | import { useStackApp, useUser } from '..'; |
7 | 8 | import { MaybeFullPage } from "../components/elements/maybe-full-page"; |
8 | 9 | import { SidebarLayout } from '../components/elements/sidebar-layout'; |
@@ -88,6 +89,69 @@ export function AccountSettings(props: { |
88 | 89 | const project = props.mockProject || projectFromHook; |
89 | 90 | const teams = user?.useTeams() || []; |
90 | 91 | const billing = user?.useBilling() || null; |
| 92 | + const teamsKey = useMemo(() => teams.map(team => team.id).join("|"), [teams]); |
| 93 | + const teamsById = useMemo(() => teams, [teamsKey]); |
| 94 | + const userRef = useRef(userFromHook ?? null); |
| 95 | + const userId = userFromHook?.id ?? null; |
| 96 | + const [paymentsAvailability, setPaymentsAvailability] = useState<{ |
| 97 | + userHasProducts: boolean, |
| 98 | + teamIdsWithProducts: Set<string>, |
| 99 | + isReady: boolean, |
| 100 | + }>(() => ({ |
| 101 | + userHasProducts: false, |
| 102 | + teamIdsWithProducts: new Set<string>(), |
| 103 | + isReady: !!props.mockUser, |
| 104 | + })); |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + userRef.current = userFromHook ?? null; |
| 108 | + }, [userFromHook]); |
| 109 | + |
| 110 | + useEffect(() => { |
| 111 | + if (props.mockUser || !userId) { |
| 112 | + return; |
| 113 | + } |
| 114 | + let cancelled = false; |
| 115 | + runAsynchronouslyWithAlert(async () => { |
| 116 | + const currentUser = userRef.current; |
| 117 | + if (!currentUser || currentUser.id !== userId) { |
| 118 | + return; |
| 119 | + } |
| 120 | + const [userProducts, teamsWithProducts] = await Promise.all([ |
| 121 | + currentUser.listProducts({ limit: 1 }), |
| 122 | + Promise.all(teamsById.map(async (team) => { |
| 123 | + const isTeamAdmin = await currentUser.hasPermission(team, "team_admin"); |
| 124 | + if (!isTeamAdmin) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + const teamProducts = await team.listProducts({ limit: 1 }); |
| 128 | + const hasTeamProducts = teamProducts.some((product) => product.customerType === "team"); |
| 129 | + return hasTeamProducts ? team.id : null; |
| 130 | + })), |
| 131 | + ]); |
| 132 | + if (cancelled) { |
| 133 | + return; |
| 134 | + } |
| 135 | + const userHasProducts = userProducts.some((product) => product.customerType === "user"); |
| 136 | + const teamIdsWithProducts = new Set<string>(teamsWithProducts.filter((id): id is string => id !== null)); |
| 137 | + setPaymentsAvailability({ |
| 138 | + userHasProducts, |
| 139 | + teamIdsWithProducts, |
| 140 | + isReady: true, |
| 141 | + }); |
| 142 | + }); |
| 143 | + return () => { |
| 144 | + cancelled = true; |
| 145 | + }; |
| 146 | + }, [props.mockUser, teamsById, userId]); |
| 147 | + |
| 148 | + const teamsWithProducts = useMemo( |
| 149 | + () => teamsById.filter(team => paymentsAvailability.teamIdsWithProducts.has(team.id)), |
| 150 | + [paymentsAvailability.teamIdsWithProducts, teamsById], |
| 151 | + ); |
| 152 | + const shouldShowPaymentsTab = props.mockUser |
| 153 | + || (paymentsAvailability.isReady |
| 154 | + && (paymentsAvailability.userHasProducts || teamsWithProducts.length > 0)); |
91 | 155 |
|
92 | 156 | // If we're not in mock mode and don't have a user, the useUser hook will handle redirect |
93 | 157 | if (!props.mockUser && !userFromHook) { |
@@ -142,15 +206,19 @@ export function AccountSettings(props: { |
142 | 206 | <ApiKeysPage mockApiKeys={props.mockApiKeys} mockMode={!!props.mockUser} /> |
143 | 207 | </Suspense>, |
144 | 208 | }] as const : []), |
145 | | - { |
| 209 | + ...(shouldShowPaymentsTab ? [{ |
146 | 210 | title: t('Payments'), |
147 | 211 | type: 'item', |
148 | 212 | id: 'payments', |
149 | 213 | icon: <Icon name="CreditCard" />, |
150 | 214 | content: <Suspense fallback={<PaymentsPageSkeleton/>}> |
151 | | - <PaymentsPage mockMode={!!props.mockUser} /> |
| 215 | + <PaymentsPage |
| 216 | + mockMode={!!props.mockUser} |
| 217 | + allowPersonal={paymentsAvailability.userHasProducts} |
| 218 | + availableTeams={teamsWithProducts} |
| 219 | + /> |
152 | 220 | </Suspense>, |
153 | | - }, |
| 221 | + }] as const : []), |
154 | 222 | { |
155 | 223 | title: t('Settings'), |
156 | 224 | type: 'item', |
|
0 commit comments