({ organization }: Props)
| 23 | }; |
| 24 | |
| 25 | export default function Billing({ organization }: Props) { |
| 26 | const [success, setSuccess] = useQueryState('customer_session_token'); |
| 27 | const queryClient = useQueryClient(); |
| 28 | const trpc = useTRPC(); |
| 29 | const number = useNumber(); |
| 30 | |
| 31 | const productsQuery = useQuery( |
| 32 | trpc.subscription.products.queryOptions({ |
| 33 | organizationId: organization.id, |
| 34 | }) |
| 35 | ); |
| 36 | |
| 37 | const currentProductQuery = useQuery( |
| 38 | trpc.subscription.getCurrent.queryOptions({ |
| 39 | organizationId: organization.id, |
| 40 | }) |
| 41 | ); |
| 42 | |
| 43 | const portalMutation = useMutation( |
| 44 | trpc.subscription.portal.mutationOptions({ |
| 45 | onSuccess(data) { |
| 46 | if (data?.url) { |
| 47 | window.location.href = data.url; |
| 48 | } |
| 49 | }, |
| 50 | onError(error) { |
| 51 | toast.error(error.message); |
| 52 | }, |
| 53 | }) |
| 54 | ); |
| 55 | |
| 56 | useWS(`/live/organization/${organization.id}`, () => { |
| 57 | queryClient.invalidateQueries(trpc.organization.pathFilter()); |
| 58 | queryClient.invalidateQueries(trpc.subscription.pathFilter()); |
| 59 | }); |
| 60 | |
| 61 | const [recurringInterval, setRecurringInterval] = useState<'year' | 'month'>( |
| 62 | (organization.subscriptionInterval as 'year' | 'month') || 'month' |
| 63 | ); |
| 64 | |
| 65 | const products = useMemo(() => { |
| 66 | return (productsQuery.data || []) |
| 67 | .filter((product) => product.recurringInterval === recurringInterval) |
| 68 | .filter((product) => product.prices.some((p) => p.amountType !== 'free')); |
| 69 | }, [productsQuery.data, recurringInterval]); |
| 70 | |
| 71 | const currentProduct = currentProductQuery.data ?? null; |
| 72 | const currentPrice = currentProduct?.prices.flatMap((p) => |
| 73 | p.amountType === 'fixed' ? [p] : [] |
| 74 | )[0]; |
| 75 | |
| 76 | const renderStatus = () => { |
| 77 | const meta = getSubscriptionStateMeta(organization.subscriptionState, { |
| 78 | endsAt: organization.subscriptionEndsAt, |
| 79 | canceledAt: organization.subscriptionCanceledAt, |
| 80 | }); |
| 81 | |
| 82 | if (!meta.statusLine) { |
nothing calls this directly
no test coverage detected