refactor(core): replace all notification relies on jotai (#6417)

- remove all notification that implemented with jotai and replaced with new `notify`
- Add some notify presets:
  - `notify.error`
  - `notify.success`
  - `notify.warning`
This commit is contained in:
CatsJuice
2024-04-02 03:19:06 +00:00
parent a4cd51e503
commit 9127bfae67
24 changed files with 150 additions and 201 deletions

View File

@@ -1,10 +1,9 @@
import { pushNotificationAtom } from '@affine/component/notification-center';
import { notify } from '@affine/component';
import { useSession } from '@affine/core/hooks/affine/use-current-user';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { affine } from '@affine/electron-api';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace-impl';
import { useSetAtom } from 'jotai';
import type { PropsWithChildren } from 'react';
import { startTransition, useEffect, useRef } from 'react';
@@ -14,7 +13,6 @@ import { mixpanel } from '../utils';
export const CloudSessionProvider = (props: PropsWithChildren) => {
const session = useSession();
const prevSession = useRef<ReturnType<typeof useSession>>();
const pushNotification = useSetAtom(pushNotificationAtom);
const onceSignedInEvents = useOnceSignedInEvents();
const t = useAFFiNEI18N();
@@ -39,10 +37,9 @@ export const CloudSessionProvider = (props: PropsWithChildren) => {
session.status === 'authenticated'
) {
startTransition(() => refreshAfterSignedInEvents());
pushNotification({
notify.success({
title: t['com.affine.auth.has.signed'](),
message: t['com.affine.auth.has.signed.message'](),
type: 'success',
});
if (environment.isDesktop) {
@@ -51,7 +48,7 @@ export const CloudSessionProvider = (props: PropsWithChildren) => {
}
prevSession.current = session;
}
}, [session, prevSession, pushNotification, refreshAfterSignedInEvents, t]);
}, [session, prevSession, refreshAfterSignedInEvents, t]);
return props.children;
};

View File

@@ -1,7 +1,6 @@
import { pushNotificationAtom } from '@affine/component/notification-center';
import { notify } from '@affine/component';
import { assertExists } from '@blocksuite/global/utils';
import { GraphQLError } from 'graphql';
import { useSetAtom } from 'jotai';
import type { PropsWithChildren, ReactNode } from 'react';
import { useCallback } from 'react';
import type { SWRConfiguration } from 'swr';
@@ -11,7 +10,6 @@ const swrConfig: SWRConfiguration = {
suspense: true,
use: [
useSWRNext => (key, fetcher, config) => {
const pushNotification = useSetAtom(pushNotificationAtom);
const fetcherWrapper = useCallback(
async (...args: any[]) => {
assertExists(fetcher);
@@ -23,18 +21,14 @@ const swrConfig: SWRConfiguration = {
(Array.isArray(e) && e[0] instanceof GraphQLError)
) {
const graphQLError = e instanceof GraphQLError ? e : e[0];
pushNotification({
notify.error({
title: 'GraphQL Error',
message: graphQLError.toString(),
key: Date.now().toString(),
type: 'error',
});
} else {
pushNotification({
notify.error({
title: 'Error',
message: e.toString(),
key: Date.now().toString(),
type: 'error',
});
}
throw e;
@@ -42,7 +36,7 @@ const swrConfig: SWRConfiguration = {
}
return d;
},
[fetcher, pushNotification]
[fetcher]
);
return useSWRNext(key, fetcher ? fetcherWrapper : fetcher, config);
},