Skip to content

Commit 6fa4b7d

Browse files
authored
feat(core): avoid popup window being blocked (#6451)
1 parent 3e9e2ce commit 6fa4b7d

File tree

14 files changed

+78
-28
lines changed

14 files changed

+78
-28
lines changed

packages/frontend/core/src/commands/affine-help.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { registerAffineCommand } from '@toeverything/infra';
44
import type { createStore } from 'jotai';
55

66
import { openSettingModalAtom } from '../atoms';
7+
import { popupWindow } from '../utils';
78

89
export function registerAffineHelpCommands({
910
t,
@@ -20,7 +21,7 @@ export function registerAffineHelpCommands({
2021
icon: <NewIcon />,
2122
label: t['com.affine.cmdk.affine.whats-new'](),
2223
run() {
23-
window.open(runtimeConfig.changelogUrl, '_blank');
24+
popupWindow(runtimeConfig.changelogUrl);
2425
},
2526
})
2627
);

packages/frontend/core/src/components/affine/auth/subscription-redirect.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Loading } from '@affine/component/ui/loading';
44
import { AffineShapeIcon } from '@affine/core/components/page-list';
55
import { useCredentialsRequirement } from '@affine/core/hooks/affine/use-server-config';
66
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
7+
import { popupWindow } from '@affine/core/utils';
78
import { SubscriptionPlan, type SubscriptionRecurring } from '@affine/graphql';
89
import {
910
changePasswordMutation,
@@ -48,7 +49,7 @@ const usePaymentRedirect = () => {
4849
successCallbackLink: null,
4950
},
5051
});
51-
window.open(checkoutUrl, '_self', 'norefferer');
52+
popupWindow(checkoutUrl);
5253
}, [recurring, plan, coupon, idempotencyKey, checkoutSubscription]);
5354
};
5455

packages/frontend/core/src/components/affine/setting-modal/general-setting/about/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useCallback } from 'react';
1111

1212
import { useAppSettingHelper } from '../../../../../hooks/affine/use-app-setting-helper';
1313
import { appIconMap, appNames } from '../../../../../pages/open-app';
14-
import { mixpanel } from '../../../../../utils';
14+
import { mixpanel, popupWindow } from '../../../../../utils';
1515
import { relatedLinks } from './config';
1616
import * as styles from './style.css';
1717
import { UpdateCheckSection } from './update-check-section';
@@ -99,7 +99,7 @@ export const AboutAffine = () => {
9999
desc={t['com.affine.aboutAFFiNE.changelog.description']()}
100100
style={{ cursor: 'pointer' }}
101101
onClick={() => {
102-
window.open(runtimeConfig.changelogUrl, '_blank');
102+
popupWindow(runtimeConfig.changelogUrl);
103103
}}
104104
>
105105
<ArrowRightSmallIcon />
@@ -143,7 +143,7 @@ export const AboutAffine = () => {
143143
<div
144144
className={styles.communityItem}
145145
onClick={() => {
146-
window.open(link, '_blank');
146+
popupWindow(link);
147147
}}
148148
key={title}
149149
>

packages/frontend/core/src/components/affine/setting-modal/general-setting/billing/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { useMutation } from '../../../../../hooks/use-mutation';
3131
import { useQuery } from '../../../../../hooks/use-query';
3232
import type { SubscriptionMutator } from '../../../../../hooks/use-subscription';
3333
import { useUserSubscription } from '../../../../../hooks/use-subscription';
34-
import { mixpanel } from '../../../../../utils';
34+
import { mixpanel, popupWindow } from '../../../../../utils';
3535
import { SWRErrorBoundary } from '../../../../pure/swr-error-bundary';
3636
import { CancelAction, ResumeAction } from '../plans/actions';
3737
import * as styles from './style.css';
@@ -262,7 +262,7 @@ const PaymentMethodUpdater = () => {
262262
const update = useAsyncCallback(async () => {
263263
await trigger(null, {
264264
onSuccess: data => {
265-
window.open(data.createCustomerPortal, '_blank', 'noopener noreferrer');
265+
popupWindow(data.createCustomerPortal);
266266
},
267267
});
268268
}, [trigger]);
@@ -361,7 +361,7 @@ const InvoiceLine = ({
361361

362362
const open = useCallback(() => {
363363
if (invoice.link) {
364-
window.open(invoice.link, '_blank', 'noopener noreferrer');
364+
popupWindow(invoice.link);
365365
}
366366
}, [invoice.link]);
367367

packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/ai/subscribe.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Button } from '@affine/component';
22
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
33
import { useMutation } from '@affine/core/hooks/use-mutation';
4+
import { popupWindow } from '@affine/core/utils';
45
import { createCheckoutSessionMutation } from '@affine/graphql';
56
import { nanoid } from 'nanoid';
67
import { useCallback, useEffect, useMemo, useRef } from 'react';
@@ -51,11 +52,7 @@ export const AISubscribe = ({
5152
},
5253
{
5354
onSuccess: data => {
54-
const newTab = window.open(
55-
data.createCheckoutSession,
56-
'_blank',
57-
'noopener noreferrer'
58-
);
55+
const newTab = popupWindow(data.createCheckoutSession);
5956
if (newTab) {
6057
newTabRef.current = newTab;
6158
newTab.addEventListener('close', onClose);

packages/frontend/core/src/components/affine/setting-modal/general-setting/plans/plan-card.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Subscription,
66
SubscriptionMutator,
77
} from '@affine/core/hooks/use-subscription';
8+
import { popupWindow } from '@affine/core/utils';
89
import type { SubscriptionRecurring } from '@affine/graphql';
910
import {
1011
createCheckoutSessionMutation,
@@ -309,13 +310,7 @@ const Upgrade = ({
309310
},
310311
{
311312
onSuccess: data => {
312-
// FIXME: safari prevents from opening new tab by window api
313-
// TODO(@xp): what if electron?
314-
const newTab = window.open(
315-
data.createCheckoutSession,
316-
'_blank',
317-
'noopener noreferrer'
318-
);
313+
const newTab = popupWindow(data.createCheckoutSession);
319314

320315
if (newTab) {
321316
newTabRef.current = newTab;

packages/frontend/core/src/components/app-sidebar/app-updater-button/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Tooltip } from '@affine/component';
2+
import { popupWindow } from '@affine/core/utils';
23
import { Unreachable } from '@affine/env/constant';
34
import { useAFFiNEI18N } from '@affine/i18n/hooks';
45
import { CloseIcon, NewIcon, ResetIcon } from '@blocksuite/icons';
@@ -181,13 +182,12 @@ export function AppUpdaterButton({
181182
onDownloadUpdate();
182183
}
183184
} else {
184-
window.open(
185-
`https://github.com/toeverything/AFFiNE/releases/tag/v${updateAvailable.version}`,
186-
'_blank'
185+
popupWindow(
186+
`https://github.com/toeverything/AFFiNE/releases/tag/v${updateAvailable.version}`
187187
);
188188
}
189189
} else if (changelogUnread) {
190-
window.open(runtimeConfig.changelogUrl, '_blank');
190+
popupWindow(runtimeConfig.changelogUrl);
191191
onOpenChangelog();
192192
} else {
193193
throw new Unreachable();

packages/frontend/core/src/components/pure/help-island/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Tooltip } from '@affine/component/ui/tooltip';
2+
import { popupWindow } from '@affine/core/utils';
23
import { useAFFiNEI18N } from '@affine/i18n/hooks';
34
import { CloseIcon, NewIcon } from '@blocksuite/icons';
45
import { Doc, useLiveData, useServiceOptional } from '@toeverything/infra';
@@ -70,7 +71,7 @@ export const HelpIsland = () => {
7071
<StyledIconWrapper
7172
data-testid="right-bottom-change-log-icon"
7273
onClick={() => {
73-
window.open(runtimeConfig.changelogUrl, '_blank');
74+
popupWindow(runtimeConfig.changelogUrl);
7475
}}
7576
>
7677
<NewIcon />

packages/frontend/core/src/hooks/use-app-updater.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { atomWithObservable, atomWithStorage } from 'jotai/utils';
77
import { useCallback, useState } from 'react';
88
import { Observable } from 'rxjs';
99

10-
import { mixpanel } from '../utils';
10+
import { mixpanel, popupWindow } from '../utils';
1111
import { useAsyncCallback } from './affine-async-hooks';
1212

1313
function rpcToObservable<
@@ -191,7 +191,7 @@ export const useAppUpdater = () => {
191191
mixpanel.track('Button', {
192192
resolve: 'OpenChangelog',
193193
});
194-
window.open(runtimeConfig.changelogUrl, '_blank');
194+
popupWindow(runtimeConfig.changelogUrl);
195195
await setChangelogUnread(true);
196196
}, [setChangelogUnread]);
197197

packages/frontend/core/src/modules/workbench/view/workbench-link.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useAppSettingHelper } from '@affine/core/hooks/affine/use-app-setting-helper';
2+
import { popupWindow } from '@affine/core/utils';
23
import { useLiveData, useService } from '@toeverything/infra';
34
import type { To } from 'history';
45
import { useCallback } from 'react';
@@ -32,7 +33,7 @@ export const WorkbenchLink = ({
3233
typeof to === 'string'
3334
? to
3435
: `${to.pathname}${to.search}${to.hash}`;
35-
window.open(basename + href, '_blank');
36+
popupWindow(basename + href);
3637
}
3738
} else {
3839
workbench.open(to);

0 commit comments

Comments
 (0)