Skip to content

Commit

Permalink
feat(core): open desktop directly in subscription landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
CatsJuice committed Apr 22, 2024
1 parent 62e277d commit e556872
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Button, type ButtonProps } from '@affine/component';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { SubscriptionService } from '@affine/core/modules/cloud';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { popupWindow } from '@affine/core/utils';
import { SubscriptionPlan, SubscriptionRecurring } from '@affine/graphql';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
Expand Down Expand Up @@ -43,7 +42,7 @@ export const AISubscribe = ({ ...btnProps }: AISubscribeProps) => {
idempotencyKey,
plan: SubscriptionPlan.AI,
coupon: null,
successCallbackLink: getAffineCloudBaseUrl() + '/ai-upgrade-success',
successCallbackLink: null,
});
popupWindow(session);
setOpenedExternalWindow(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 = (
<div className={styles.leftContentText}>
Expand Down
10 changes: 10 additions & 0 deletions packages/frontend/core/src/hooks/use-navigate-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -169,6 +177,7 @@ export function useNavigateHelper() {
jumpToCollections,
jumpToTags,
jumpToTag,
openInApp,
}),
[
jumpToPage,
Expand All @@ -183,6 +192,7 @@ export function useNavigateHelper() {
jumpToCollections,
jumpToTags,
jumpToTag,
openInApp,
]
);
}
25 changes: 23 additions & 2 deletions packages/frontend/core/src/modules/cloud/stores/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import type {
CreateCheckoutSessionInput,
SubscriptionPlan,
SubscriptionRecurring,
} from '@affine/graphql';
import {
cancelSubscriptionMutation,
createCheckoutSessionMutation,
pricesQuery,
resumeSubscriptionMutation,
SubscriptionPlan,
subscriptionQuery,
updateSubscriptionMutation,
} from '@affine/graphql';
import type { GlobalCacheService } from '@toeverything/infra';
import { Store } from '@toeverything/infra';

import type { SubscriptionType } from '../entities/subscription';
import { getAffineCloudBaseUrl } from '../services/fetch';
import type { GraphQLService } from '../services/graphql';

const SUBSCRIPTION_CACHE_KEY = 'subscription:';

const getDefaultSubscriptionSuccessCallbackLink = (
plan: SubscriptionPlan | null
) => {
const path =
plan === SubscriptionPlan.AI ? '/ai-upgrade-success' : '/upgrade-success';
const urlString = getAffineCloudBaseUrl() + path;
const url = new URL(urlString);
if (environment.isDesktop) {
url.searchParams.set('schema', window.appInfo.schema);
}
return url.toString();
};

export class SubscriptionStore extends Store {
constructor(
private readonly gqlService: GraphQLService,
Expand Down Expand Up @@ -112,7 +126,14 @@ export class SubscriptionStore extends Store {
async createCheckoutSession(input: CreateCheckoutSessionInput) {
const data = await this.gqlService.gql({
query: createCheckoutSessionMutation,
variables: { input },
variables: {
input: {
...input,
successCallbackLink:
input.successCallbackLink ||
getDefaultSubscriptionSuccessCallbackLink(input.plan),
},
},
});
return data.createCheckoutSession;
}
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/electron/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand Down
6 changes: 6 additions & 0 deletions packages/frontend/electron/src/main/deep-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e556872

Please sign in to comment.