From 74d71b7eb0237c10103b806638b569d1c38161ad Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Fri, 20 Dec 2024 17:42:46 +0800 Subject: [PATCH] feat(core): open in app for self-hosted --- .../core/src/components/sign-in/index.tsx | 18 +++++-- .../components/sign-in/sign-in-with-email.tsx | 9 ++-- .../sign-in/sign-in-with-password.tsx | 9 ++-- .../core/src/components/sign-in/sign-in.tsx | 13 +++-- .../src/desktop/dialogs/sign-in/index.tsx | 13 ++++- .../core/src/desktop/pages/auth/sign-in.tsx | 48 ++++++++++++------- .../src/desktop/pages/workspace/index.tsx | 20 +++++++- .../src/mobile/components/sign-in/index.tsx | 17 ++++++- .../src/modules/cloud/entities/session.ts | 5 ++ .../core/src/modules/open-in-app/utils.ts | 4 ++ tools/cli/src/webpack/config.ts | 13 ++++- tools/cli/src/webpack/runtime-config.ts | 1 - 12 files changed, 131 insertions(+), 39 deletions(-) diff --git a/packages/frontend/core/src/components/sign-in/index.tsx b/packages/frontend/core/src/components/sign-in/index.tsx index e5ac4a9a7a66d..db834ab491c5c 100644 --- a/packages/frontend/core/src/components/sign-in/index.tsx +++ b/packages/frontend/core/src/components/sign-in/index.tsx @@ -6,6 +6,7 @@ import { AddSelfhostedStep } from './add-selfhosted'; import { SignInStep } from './sign-in'; import { SignInWithEmailStep } from './sign-in-with-email'; import { SignInWithPasswordStep } from './sign-in-with-password'; +import type { AuthSessionStatus } from '@affine/core/modules/cloud/entities/session'; export type SignInStep = | 'signIn' @@ -22,11 +23,13 @@ export interface SignInState { } export const SignInPanel = ({ - onClose, + onSkip, server: initialServerBaseUrl, initStep, + onAuthenticated, }: { - onClose: () => void; + onAuthenticated?: (status: AuthSessionStatus) => void; + onSkip: () => void; server?: string; initStep?: SignInStep | undefined; }) => { @@ -47,18 +50,23 @@ export const SignInPanel = ({ return ( {step === 'signIn' ? ( - + ) : step === 'signInWithEmail' ? ( ) : step === 'signInWithPassword' ? ( ) : step === 'addSelfhosted' ? ( diff --git a/packages/frontend/core/src/components/sign-in/sign-in-with-email.tsx b/packages/frontend/core/src/components/sign-in/sign-in-with-email.tsx index 7a99ecaa27975..1a19a52884d0c 100644 --- a/packages/frontend/core/src/components/sign-in/sign-in-with-email.tsx +++ b/packages/frontend/core/src/components/sign-in/sign-in-with-email.tsx @@ -23,15 +23,16 @@ import { import type { SignInState } from '.'; import { Captcha } from './captcha'; import * as style from './style.css'; +import type { AuthSessionStatus } from '@affine/core/modules/cloud/entities/session'; export const SignInWithEmailStep = ({ state, changeState, - close, + onAuthenticated, }: { state: SignInState; changeState: Dispatch>; - close: () => void; + onAuthenticated?: (status: AuthSessionStatus) => void; }) => { const initialSent = useRef(false); const [resendCountDown, setResendCountDown] = useState(0); @@ -66,13 +67,13 @@ export const SignInWithEmailStep = ({ useEffect(() => { if (loginStatus === 'authenticated') { - close(); notify.success({ title: t['com.affine.auth.toast.title.signed-in'](), message: t['com.affine.auth.toast.message.signed-in'](), }); } - }, [close, loginStatus, t]); + onAuthenticated?.(loginStatus); + }, [loginStatus, onAuthenticated, t]); const sendEmail = useAsyncCallback(async () => { if (isSending || (!verifyToken && needCaptcha)) return; diff --git a/packages/frontend/core/src/components/sign-in/sign-in-with-password.tsx b/packages/frontend/core/src/components/sign-in/sign-in-with-password.tsx index 30b00e1c5859f..5cd57a4da9bd6 100644 --- a/packages/frontend/core/src/components/sign-in/sign-in-with-password.tsx +++ b/packages/frontend/core/src/components/sign-in/sign-in-with-password.tsx @@ -21,15 +21,16 @@ import { useCallback, useEffect, useState } from 'react'; import type { SignInState } from '.'; import { Captcha } from './captcha'; import * as styles from './style.css'; +import type { AuthSessionStatus } from '@affine/core/modules/cloud/entities/session'; export const SignInWithPasswordStep = ({ state, changeState, - close, + onAuthenticated, }: { state: SignInState; changeState: Dispatch>; - close: () => void; + onAuthenticated?: (status: AuthSessionStatus) => void; }) => { const t = useI18n(); const authService = useService(AuthService); @@ -62,13 +63,13 @@ export const SignInWithPasswordStep = ({ useEffect(() => { if (loginStatus === 'authenticated') { - close(); notify.success({ title: t['com.affine.auth.toast.title.signed-in'](), message: t['com.affine.auth.toast.message.signed-in'](), }); } - }, [close, loginStatus, t]); + onAuthenticated?.(loginStatus); + }, [loginStatus, onAuthenticated, t]); const onSignIn = useAsyncCallback(async () => { if (isLoading || (!verifyToken && needCaptcha)) return; diff --git a/packages/frontend/core/src/components/sign-in/sign-in.tsx b/packages/frontend/core/src/components/sign-in/sign-in.tsx index 60d06e3f0d5b5..7d8f4087646cd 100644 --- a/packages/frontend/core/src/components/sign-in/sign-in.tsx +++ b/packages/frontend/core/src/components/sign-in/sign-in.tsx @@ -22,6 +22,7 @@ import { import type { SignInState } from '.'; import * as style from './style.css'; +import type { AuthSessionStatus } from '@affine/core/modules/cloud/entities/session'; const emailRegex = /^(?:(?:[^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|((?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; @@ -33,11 +34,13 @@ function validateEmail(email: string) { export const SignInStep = ({ state, changeState, - close, + onSkip, + onAuthenticated, }: { state: SignInState; changeState: Dispatch>; - close: () => void; + onSkip: () => void; + onAuthenticated?: (status: AuthSessionStatus) => void; }) => { const t = useI18n(); const serverService = useService(ServerService); @@ -64,13 +67,13 @@ export const SignInStep = ({ useEffect(() => { if (loginStatus === 'authenticated') { - close(); notify.success({ title: t['com.affine.auth.toast.title.signed-in'](), message: t['com.affine.auth.toast.message.signed-in'](), }); } - }, [close, loginStatus, t]); + onAuthenticated?.(loginStatus); + }, [loginStatus, onAuthenticated, t]); const onContinue = useAsyncCallback(async () => { if (!validateEmail(email)) { @@ -208,7 +211,7 @@ export const SignInStep = ({