|
| 1 | +import { Button, Switch } from '@affine/component'; |
| 2 | +import { |
| 3 | + SettingHeader, |
| 4 | + SettingRow, |
| 5 | +} from '@affine/component/setting-components'; |
| 6 | +import { getUpgradeQuestionnaireLink } from '@affine/core/components/hooks/affine/use-subscription-notify'; |
| 7 | +import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks'; |
| 8 | +import { useMutation } from '@affine/core/components/hooks/use-mutation'; |
| 9 | +import { |
| 10 | + AuthService, |
| 11 | + WorkspaceSubscriptionService, |
| 12 | +} from '@affine/core/modules/cloud'; |
| 13 | +import { UrlService } from '@affine/core/modules/url'; |
| 14 | +import { WorkspaceService } from '@affine/core/modules/workspace'; |
| 15 | +import { |
| 16 | + createCustomerPortalMutation, |
| 17 | + SubscriptionPlan, |
| 18 | + SubscriptionRecurring, |
| 19 | +} from '@affine/graphql'; |
| 20 | +import { useI18n } from '@affine/i18n'; |
| 21 | +import { FrameworkScope, useLiveData, useService } from '@toeverything/infra'; |
| 22 | +import { useCallback, useEffect, useState } from 'react'; |
| 23 | + |
| 24 | +import type { SettingState } from '../../types'; |
| 25 | +import { SelfHostTeamCard } from './self-host-team-card'; |
| 26 | +import * as styles from './styles.css'; |
| 27 | + |
| 28 | +export const WorkspaceSettingLicense = ({ |
| 29 | + onChangeSettingState, |
| 30 | +}: { |
| 31 | + onChangeSettingState: (state: SettingState) => void; |
| 32 | +}) => { |
| 33 | + const workspace = useService(WorkspaceService).workspace; |
| 34 | + |
| 35 | + const t = useI18n(); |
| 36 | + |
| 37 | + const subscriptionService = workspace?.scope.get( |
| 38 | + WorkspaceSubscriptionService |
| 39 | + ); |
| 40 | + const subscription = useLiveData( |
| 41 | + subscriptionService?.subscription.subscription$ |
| 42 | + ); |
| 43 | + |
| 44 | + // TODO(@JimmFly): add sign in check |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + subscriptionService?.subscription.revalidate(); |
| 48 | + }, [subscriptionService?.subscription]); |
| 49 | + |
| 50 | + if (workspace === null) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + <FrameworkScope scope={workspace.scope}> |
| 56 | + <SettingHeader |
| 57 | + title={t['com.affine.settings.workspace.license']()} |
| 58 | + subtitle={t['com.affine.settings.workspace.license.description']()} |
| 59 | + /> |
| 60 | + {workspace.flavour !== 'local' ? ( |
| 61 | + <> |
| 62 | + <SelfHostTeamCard onChangeSettingState={onChangeSettingState} /> |
| 63 | + <TypeFormLink /> |
| 64 | + <PaymentMethodUpdater /> |
| 65 | + <Checkout /> |
| 66 | + {subscription?.end ? ( |
| 67 | + <ResumeSubscription expirationDate={subscription.end} /> |
| 68 | + ) : null} |
| 69 | + </> |
| 70 | + ) : ( |
| 71 | + 'Self-hosted Team workspace depends on cloud workspace, please sign in and enable cloud first.' |
| 72 | + )} |
| 73 | + </FrameworkScope> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const ResumeSubscription = ({ expirationDate }: { expirationDate: string }) => { |
| 78 | + const t = useI18n(); |
| 79 | + const handleClick = useCallback(() => { |
| 80 | + window.open('', '_blank'); |
| 81 | + }, []); |
| 82 | + |
| 83 | + return ( |
| 84 | + <SettingRow |
| 85 | + name={t['com.affine.payment.billing-setting.expiration-date']()} |
| 86 | + desc={t['com.affine.payment.billing-setting.expiration-date.description']( |
| 87 | + { |
| 88 | + expirationDate: new Date(expirationDate).toLocaleDateString(), |
| 89 | + } |
| 90 | + )} |
| 91 | + > |
| 92 | + <Button onClick={handleClick} variant="primary"> |
| 93 | + {t['com.affine.settings.workspace.license.buy-more-seat']()} |
| 94 | + </Button> |
| 95 | + </SettingRow> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +const TypeFormLink = () => { |
| 100 | + const t = useI18n(); |
| 101 | + const workspaceSubscriptionService = useService(WorkspaceSubscriptionService); |
| 102 | + const authService = useService(AuthService); |
| 103 | + |
| 104 | + const workspaceSubscription = useLiveData( |
| 105 | + workspaceSubscriptionService.subscription.subscription$ |
| 106 | + ); |
| 107 | + const account = useLiveData(authService.session.account$); |
| 108 | + |
| 109 | + if (!account) return null; |
| 110 | + |
| 111 | + const link = getUpgradeQuestionnaireLink({ |
| 112 | + name: account.info?.name, |
| 113 | + id: account.id, |
| 114 | + email: account.email, |
| 115 | + recurring: workspaceSubscription?.recurring ?? SubscriptionRecurring.Yearly, |
| 116 | + plan: SubscriptionPlan.SelfHostedTeam, |
| 117 | + }); |
| 118 | + |
| 119 | + return ( |
| 120 | + <SettingRow |
| 121 | + className={styles.paymentMethod} |
| 122 | + name={t['com.affine.payment.billing-type-form.title']()} |
| 123 | + desc={t['com.affine.payment.billing-type-form.description']()} |
| 124 | + > |
| 125 | + <a target="_blank" href={link} rel="noreferrer"> |
| 126 | + <Button>{t['com.affine.payment.billing-type-form.go']()}</Button> |
| 127 | + </a> |
| 128 | + </SettingRow> |
| 129 | + ); |
| 130 | +}; |
| 131 | + |
| 132 | +const PaymentMethodUpdater = () => { |
| 133 | + const { isMutating, trigger } = useMutation({ |
| 134 | + mutation: createCustomerPortalMutation, |
| 135 | + }); |
| 136 | + const urlService = useService(UrlService); |
| 137 | + const t = useI18n(); |
| 138 | + |
| 139 | + const update = useAsyncCallback(async () => { |
| 140 | + await trigger(null, { |
| 141 | + onSuccess: data => { |
| 142 | + urlService.openPopupWindow(data.createCustomerPortal); |
| 143 | + }, |
| 144 | + }); |
| 145 | + }, [trigger, urlService]); |
| 146 | + |
| 147 | + return ( |
| 148 | + <SettingRow |
| 149 | + className={styles.paymentMethod} |
| 150 | + name={t['com.affine.payment.billing-setting.payment-method']()} |
| 151 | + desc={t[ |
| 152 | + 'com.affine.payment.billing-setting.payment-method.description' |
| 153 | + ]()} |
| 154 | + > |
| 155 | + <Button onClick={update} loading={isMutating} disabled={isMutating}> |
| 156 | + {t['com.affine.payment.billing-setting.payment-method.go']()} |
| 157 | + </Button> |
| 158 | + </SettingRow> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +const Checkout = () => { |
| 163 | + const t = useI18n(); |
| 164 | + const [recurring, setRecurring] = useState(SubscriptionRecurring.Monthly); |
| 165 | + const onCheckout = useCallback(() => { |
| 166 | + // https://affine.fail/ |
| 167 | + // http://localhost:8080/ |
| 168 | + window.open( |
| 169 | + `http://localhost:8080/subscribe?product=${recurring === SubscriptionRecurring.Monthly ? 'monthly' : 'yearly'}-selfhost-team`, |
| 170 | + '_blank' |
| 171 | + ); |
| 172 | + }, [recurring]); |
| 173 | + const toggleSwitch = useCallback((checked: boolean) => { |
| 174 | + if (checked) { |
| 175 | + setRecurring(SubscriptionRecurring.Yearly); |
| 176 | + return; |
| 177 | + } |
| 178 | + setRecurring(SubscriptionRecurring.Monthly); |
| 179 | + }, []); |
| 180 | + return ( |
| 181 | + <SettingRow |
| 182 | + className={styles.paymentMethod} |
| 183 | + name={ |
| 184 | + <Switch |
| 185 | + checked={recurring === SubscriptionRecurring.Yearly} |
| 186 | + onChange={toggleSwitch} |
| 187 | + > |
| 188 | + {t['com.affine.payment.cloud.pricing-plan.toggle-billed-yearly']()} |
| 189 | + </Switch> |
| 190 | + } |
| 191 | + desc={''} |
| 192 | + > |
| 193 | + <Button onClick={onCheckout}>Checkout</Button> |
| 194 | + </SettingRow> |
| 195 | + ); |
| 196 | +}; |
0 commit comments