-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
201 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { cssVar } from '@toeverything/theme'; | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const container = style({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
height: '100vh', | ||
width: '100%', | ||
lineHeight: 4, | ||
color: cssVar('--affine-text-secondary-color'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Button, Loading } from '@affine/component'; | ||
import { SubscriptionPlan, SubscriptionRecurring } from '@affine/graphql'; | ||
import { effect, fromPromise, useServices } from '@toeverything/infra'; | ||
import { nanoid } from 'nanoid'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { EMPTY, mergeMap, switchMap } from 'rxjs'; | ||
|
||
import { RouteLogic, useNavigateHelper } from '../hooks/use-navigate-helper'; | ||
import { AuthService, SubscriptionService } from '../modules/cloud'; | ||
import { container } from './subscribe.css'; | ||
|
||
export const Component = () => { | ||
const { authService, subscriptionService } = useServices({ | ||
AuthService, | ||
SubscriptionService, | ||
}); | ||
const [searchParams] = useSearchParams(); | ||
const [message, setMessage] = useState(''); | ||
const [error, setError] = useState(''); | ||
const { jumpToSignIn, jumpToIndex } = useNavigateHelper(); | ||
const idempotencyKey = useMemo(() => nanoid(), []); | ||
|
||
const plan = searchParams.get('plan') as string | null; | ||
const recurring = searchParams.get('recurring') as string | null; | ||
|
||
useEffect(() => { | ||
const call = effect( | ||
switchMap(() => { | ||
return fromPromise(async signal => { | ||
// TODO: i18n | ||
setMessage('Checking account status...'); | ||
setError(''); | ||
await authService.session.waitForRevalidation(signal); | ||
const loggedIn = | ||
authService.session.status$.value === 'authenticated'; | ||
if (!loggedIn) { | ||
setMessage('Redirecting to sign in...'); | ||
jumpToSignIn( | ||
location.pathname + location.search, | ||
RouteLogic.REPLACE | ||
); | ||
return; | ||
} | ||
setMessage('Checking subscription status...'); | ||
await subscriptionService.subscription.waitForRevalidation(signal); | ||
const subscribed = !!subscriptionService.subscription.ai$.value; | ||
if (!subscribed) { | ||
setMessage('Creating checkout...'); | ||
const checkout = await subscriptionService.createCheckoutSession({ | ||
idempotencyKey, | ||
plan: | ||
plan?.toLowerCase() === 'ai' | ||
? SubscriptionPlan.AI | ||
: SubscriptionPlan.Pro, | ||
coupon: null, | ||
recurring: | ||
recurring?.toLowerCase() === 'monthly' | ||
? SubscriptionRecurring.Monthly | ||
: SubscriptionRecurring.Yearly, | ||
successCallbackLink: null, | ||
}); | ||
setMessage('Redirecting...'); | ||
location.href = checkout; | ||
} else { | ||
setMessage('Your account is already subscribed. Redirecting...'); | ||
await new Promise(resolve => { | ||
setTimeout(resolve, 5000); | ||
}); | ||
jumpToIndex(RouteLogic.REPLACE); | ||
} | ||
}).pipe(mergeMap(() => EMPTY)); | ||
}) | ||
); | ||
|
||
call(); | ||
|
||
return () => { | ||
call.unsubscribe(); | ||
}; | ||
}, [ | ||
authService, | ||
subscriptionService, | ||
jumpToSignIn, | ||
idempotencyKey, | ||
plan, | ||
jumpToIndex, | ||
recurring, | ||
]); | ||
|
||
useEffect(() => { | ||
authService.session.revalidate(); | ||
}, [authService]); | ||
|
||
return ( | ||
<div className={container}> | ||
{!error ? ( | ||
<> | ||
{message} | ||
<br /> | ||
<Loading size={20} /> | ||
</> | ||
) : ( | ||
<> | ||
{error} | ||
<br /> | ||
<Button type="primary">Retry</Button> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters