From c3d3b2d73527203f3e4d9708f78688a0d3028211 Mon Sep 17 00:00:00 2001 From: Cats Juice Date: Mon, 22 Apr 2024 16:25:16 +0800 Subject: [PATCH] feat(core): open desktop directly in subscription landing page --- .../components/affine/subscription-landing/index.tsx | 12 +++++++++--- .../frontend/core/src/hooks/use-navigate-helper.ts | 10 ++++++++++ .../core/src/modules/cloud/stores/subscription.ts | 6 ++++++ packages/frontend/electron/renderer/app.tsx | 1 + packages/frontend/electron/src/main/deep-link.ts | 6 ++++++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/frontend/core/src/components/affine/subscription-landing/index.tsx b/packages/frontend/core/src/components/affine/subscription-landing/index.tsx index 1da4f1ee23dde..026d66460a044 100644 --- a/packages/frontend/core/src/components/affine/subscription-landing/index.tsx +++ b/packages/frontend/core/src/components/affine/subscription-landing/index.tsx @@ -4,6 +4,7 @@ import { useNavigateHelper } from '@affine/core/hooks/use-navigate-helper'; import { Trans } from '@affine/i18n'; import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { type ReactNode, useCallback } from 'react'; +import { useSearchParams } from 'react-router-dom'; import * as styles from './styles.css'; @@ -15,11 +16,16 @@ const UpgradeSuccessLayout = ({ description?: ReactNode; }) => { const t = useAFFiNEI18N(); + const [params] = useSearchParams(); - const { jumpToIndex } = useNavigateHelper(); + const { jumpToIndex, openInApp } = useNavigateHelper(); const openAffine = useCallback(() => { - jumpToIndex(); - }, [jumpToIndex]); + if (params.get('schema')) { + openInApp(params.get('schema') ?? 'affine', 'bring-to-front'); + } else { + jumpToIndex(); + } + }, [jumpToIndex, openInApp, params]); const subtitle = (
diff --git a/packages/frontend/core/src/hooks/use-navigate-helper.ts b/packages/frontend/core/src/hooks/use-navigate-helper.ts index 5c86cdd4af944..c15a2a5218d2a 100644 --- a/packages/frontend/core/src/hooks/use-navigate-helper.ts +++ b/packages/frontend/core/src/hooks/use-navigate-helper.ts @@ -155,6 +155,14 @@ export function useNavigateHelper() { [navigate] ); + const openInApp = useCallback( + (schema: string, path: string) => { + const encodedUrl = encodeURIComponent(`${schema}://${path}`); + return navigate(`/open-app/url?schema=${schema}&url=${encodedUrl}`); + }, + [navigate] + ); + return useMemo( () => ({ jumpToPage, @@ -169,6 +177,7 @@ export function useNavigateHelper() { jumpToCollections, jumpToTags, jumpToTag, + openInApp, }), [ jumpToPage, @@ -183,6 +192,7 @@ export function useNavigateHelper() { jumpToCollections, jumpToTags, jumpToTag, + openInApp, ] ); } diff --git a/packages/frontend/core/src/modules/cloud/stores/subscription.ts b/packages/frontend/core/src/modules/cloud/stores/subscription.ts index 8524ee0e9df3a..31c829b8f98bb 100644 --- a/packages/frontend/core/src/modules/cloud/stores/subscription.ts +++ b/packages/frontend/core/src/modules/cloud/stores/subscription.ts @@ -110,6 +110,12 @@ export class SubscriptionStore extends Store { } async createCheckoutSession(input: CreateCheckoutSessionInput) { + const { successCallbackLink } = input; + if (successCallbackLink && environment.isDesktop) { + const url = new URL(successCallbackLink); + url.searchParams.set('schema', window.appInfo.schema); + input.successCallbackLink = url.toString(); + } const data = await this.gqlService.gql({ query: createCheckoutSessionMutation, variables: { input }, diff --git a/packages/frontend/electron/renderer/app.tsx b/packages/frontend/electron/renderer/app.tsx index c0e0f09f73eac..240f3545299bd 100644 --- a/packages/frontend/electron/renderer/app.tsx +++ b/packages/frontend/electron/renderer/app.tsx @@ -32,6 +32,7 @@ import { RouterProvider } from 'react-router-dom'; const desktopWhiteList = [ '/desktop-signin', '/open-app/signin-redirect', + '/open-app/url', '/upgrade-success', '/ai-upgrade-success', ]; diff --git a/packages/frontend/electron/src/main/deep-link.ts b/packages/frontend/electron/src/main/deep-link.ts index e0c88af7fbbbb..df762d716152b 100644 --- a/packages/frontend/electron/src/main/deep-link.ts +++ b/packages/frontend/electron/src/main/deep-link.ts @@ -67,6 +67,12 @@ async function handleAffineUrl(url: string) { if (urlObj.hostname === 'signin-redirect') { await handleOauthJwt(url); } + if (urlObj.hostname === 'bring-to-front') { + const mainWindow = await getMainWindow(); + if (mainWindow) { + mainWindow.show(); + } + } } async function handleOauthJwt(url: string) {