From 7c5ca1674b5a0b9076c17331e95907e736132eb5 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Tue, 5 Nov 2024 20:10:00 +0800 Subject: [PATCH] feat: cleanup codes --- .../core/src/desktop/pages/auth/oauth-callback.tsx | 5 ++--- .../frontend/core/src/desktop/pages/open-app/index.tsx | 8 ++------ packages/frontend/core/src/utils/link.ts | 6 ++++++ 3 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 packages/frontend/core/src/utils/link.ts diff --git a/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx b/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx index ba3c5ff0d3212..c2a706adaf1b0 100644 --- a/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx +++ b/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx @@ -1,3 +1,4 @@ +import { extractLinkSearchParams } from '@affine/core/utils/link'; import { useService } from '@toeverything/infra'; import { useEffect, useRef } from 'react'; import { @@ -27,9 +28,7 @@ type LoaderData = z.infer; type ParsedState = z.infer; async function parseState(url: string): Promise { - const { code, state: stateStr } = Array.from( - new URL(url).searchParams.entries() - ).reduce((acc, [k, v]) => ((acc[k] = v), acc), {} as Record); + const { code, state: stateStr } = extractLinkSearchParams(url); if (!code || !stateStr) throw new Error('Invalid oauth callback parameters'); try { /** @deprecated old client compatibility*/ diff --git a/packages/frontend/core/src/desktop/pages/open-app/index.tsx b/packages/frontend/core/src/desktop/pages/open-app/index.tsx index 1653121aa52fb..0df666a8e6438 100644 --- a/packages/frontend/core/src/desktop/pages/open-app/index.tsx +++ b/packages/frontend/core/src/desktop/pages/open-app/index.tsx @@ -1,5 +1,6 @@ import { OpenInAppPage } from '@affine/core/modules/open-in-app/views/open-in-app-page'; import { appSchemaUrl, appSchemes } from '@affine/core/utils'; +import { extractLinkSearchParams } from '@affine/core/utils/link'; import type { GetCurrentUserQuery } from '@affine/graphql'; import { fetcher, getCurrentUserQuery } from '@affine/graphql'; import type { LoaderFunction } from 'react-router-dom'; @@ -67,12 +68,7 @@ export const loader: LoaderFunction = async args => { const action = args.params.action || ''; try { - const { url, ...params } = Array.from( - new URL(args.request.url).searchParams.entries() - ).reduce( - (acc, [k, v]) => ((acc[k] = v), acc), - {} as Record - ); + const { url, ...params } = extractLinkSearchParams(args.request.url); const res = (action === 'signin-redirect' && (await fetcher({ diff --git a/packages/frontend/core/src/utils/link.ts b/packages/frontend/core/src/utils/link.ts new file mode 100644 index 0000000000000..1ac15781885dd --- /dev/null +++ b/packages/frontend/core/src/utils/link.ts @@ -0,0 +1,6 @@ +export function extractLinkSearchParams(link: string): Record { + return Array.from(new URL(link).searchParams.entries()).reduce( + (acc, [k, v]) => ((acc[k] = v), acc), + {} as Record + ); +}