From 4a93582799bda3fd335533588de342bbc8254ae2 Mon Sep 17 00:00:00 2001
From: CatsJuice
Date: Tue, 9 Apr 2024 08:54:53 +0000
Subject: [PATCH] feat(core): ai subscription in billing page (#6476)
---
.../general-setting/billing/index.tsx | 47 ++++++++++++++++++-
.../plans/ai/{ => actions}/cancel.tsx | 25 +++++-----
.../general-setting/plans/ai/actions/index.ts | 4 ++
.../plans/ai/{ => actions}/login.tsx | 12 +++--
.../plans/ai/{ => actions}/resume.tsx | 30 ++++++------
.../plans/ai/{ => actions}/subscribe.tsx | 24 ++++++----
.../general-setting/plans/ai/ai-plan.tsx | 35 +++-----------
.../general-setting/plans/ai/types.ts | 7 +--
.../plans/ai/use-affine-ai-price.ts | 14 ++++++
.../plans/ai/use-affine-ai-subscription.ts | 40 ++++++++++++++++
packages/frontend/i18n/src/resources/en.json | 3 +-
11 files changed, 163 insertions(+), 78 deletions(-)
rename packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/{ => actions}/cancel.tsx (68%)
create mode 100644 packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/actions/index.ts
rename packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/{ => actions}/login.tsx (50%)
rename packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/{ => actions}/resume.tsx (73%)
rename packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/{ => actions}/subscribe.tsx (72%)
create mode 100644 packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/use-affine-ai-price.ts
create mode 100644 packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/use-affine-ai-subscription.ts
diff --git a/packages/frontend/core/src/components/affine/setting-modal/general-setting/billing/index.tsx b/packages/frontend/core/src/components/affine/setting-modal/general-setting/billing/index.tsx
index c9a8c0089a..c67858abc1 100644
--- a/packages/frontend/core/src/components/affine/setting-modal/general-setting/billing/index.tsx
+++ b/packages/frontend/core/src/components/affine/setting-modal/general-setting/billing/index.tsx
@@ -21,6 +21,7 @@ import {
} from '@affine/graphql';
import { Trans } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
+import { assertExists } from '@blocksuite/global/utils';
import { ArrowRightSmallIcon } from '@blocksuite/icons';
import { useSetAtom } from 'jotai';
import { Suspense, useCallback, useMemo, useState } from 'react';
@@ -34,6 +35,8 @@ import { useUserSubscription } from '../../../../../hooks/use-subscription';
import { mixpanel, popupWindow } from '../../../../../utils';
import { SWRErrorBoundary } from '../../../../pure/swr-error-bundary';
import { CancelAction, ResumeAction } from '../plans/actions';
+import { useAffineAIPrice } from '../plans/ai/use-affine-ai-price';
+import { useAffineAISubscription } from '../plans/ai/use-affine-ai-subscription';
import * as styles from './style.css';
enum DescriptionI18NKey {
@@ -93,6 +96,11 @@ export const BillingSettings = () => {
const SubscriptionSettings = () => {
const [subscription, mutateSubscription] = useUserSubscription();
const [openCancelModal, setOpenCancelModal] = useState(false);
+ const {
+ actionType: aiActionType,
+ Action: AIAction,
+ billingTip,
+ } = useAffineAISubscription();
const { data: pricesQueryResult } = useQuery({
query: pricesQuery,
@@ -102,6 +110,10 @@ const SubscriptionSettings = () => {
const recurring = subscription?.recurring ?? SubscriptionRecurring.Monthly;
const price = pricesQueryResult.prices.find(price => price.plan === plan);
+ const aiPrice = pricesQueryResult.prices.find(
+ price => price.plan === SubscriptionPlan.AI
+ );
+ assertExists(aiPrice);
const amount =
plan === SubscriptionPlan.Free
? '0'
@@ -111,6 +123,8 @@ const SubscriptionSettings = () => {
: String((price.yearlyAmount ?? 0) / 100)
: '?';
+ const { priceReadable: aiPriceReadable, priceFrequency: aiPriceFrequency } =
+ useAffineAIPrice(aiPrice);
const t = useAFFiNEI18N();
const setOpenSettingModalAtom = useSetAtom(openSettingModalAtom);
@@ -167,6 +181,30 @@ const SubscriptionSettings = () => {
+
+
+
+
+ {aiPrice?.yearlyAmount ? (
+
+ {aiActionType === 'subscribe' ? 'Purchase' : null}
+
+ ) : null}
+
+
+ {aiPriceReadable}
+ /{aiPriceFrequency}
+
+
{subscription?.status === SubscriptionStatus.Active && (
<>