Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix: unexpected react warning (#4316)

JimmFly7d6c150ecd65
diff --git a/apps/core/src/providers/session-provider.tsx b/apps/core/src/providers/session-provider.tsx
index 2b1e4d96c0..3cc82312ef 100644
--- a/apps/core/src/providers/session-provider.tsx
+++ b/apps/core/src/providers/session-provider.tsx
@@ -7,7 +7,12 @@ import { refreshRootMetadataAtom } from '@affine/workspace/atom';
import { useAtom, useSetAtom } from 'jotai';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { SessionProvider, useSession } from 'next-auth/react';
-import { type PropsWithChildren, startTransition, useRef } from 'react';
+import {
+ type PropsWithChildren,
+ startTransition,
+ useEffect,
+ useRef,
+} from 'react';
import { sessionAtom } from '../atoms/cloud-user';
import { useOnceSignedInEvents } from '../atoms/event';
@@ -20,34 +25,44 @@ const SessionDefence = (props: PropsWithChildren) => {
const refreshMetadata = useSetAtom(refreshRootMetadataAtom);
const onceSignedInEvents = useOnceSignedInEvents();
const t = useAFFiNEI18N();
+ useEffect(() => {
+ if (sessionInAtom !== session && session.status === 'authenticated') {
+ setSession(session);
+ }
- if (sessionInAtom !== session && session.status === 'authenticated') {
- setSession(session);
- }
-
- if (prevSession.current !== session && session.status !== 'loading') {
- // unauthenticated -> authenticated
- if (
- prevSession.current?.status === 'unauthenticated' &&
- session.status === 'authenticated'
- ) {
- startTransition(() => {
- onceSignedInEvents().then(() => {
- refreshMetadata();
+ if (prevSession.current !== session && session.status !== 'loading') {
+ // unauthenticated -> authenticated
+ if (
+ prevSession.current?.status === 'unauthenticated' &&
+ session.status === 'authenticated'
+ ) {
+ startTransition(() => {
+ onceSignedInEvents().then(() => {
+ refreshMetadata();
+ });
+ });
+ pushNotification({
+ title: t['com.affine.auth.has.signed'](),
+ message: t['com.affine.auth.has.signed.message'](),
+ type: 'success',
});
- });
- pushNotification({
- title: t['com.affine.auth.has.signed'](),
- message: t['com.affine.auth.has.signed.message'](),
- type: 'success',
- });
- if (isDesktop) {
- window.affine.ipcRenderer.send('affine:login');
+ if (isDesktop) {
+ window.affine.ipcRenderer.send('affine:login');
+ }
}
+ prevSession.current = session;
}
- prevSession.current = session;
- }
+ }, [
+ session,
+ sessionInAtom,
+ prevSession,
+ setSession,
+ onceSignedInEvents,
+ pushNotification,
+ refreshMetadata,
+ t,
+ ]);
return props.children;
};