mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 22:38:56 +08:00
feat(infra): framework
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { notify } from '@affine/component';
|
||||
import { events } from '@affine/electron-api';
|
||||
import { WorkspaceFlavour } from '@affine/env/workspace';
|
||||
import {
|
||||
useLiveData,
|
||||
useService,
|
||||
Workspace,
|
||||
WorkspaceManager,
|
||||
useServiceOptional,
|
||||
WorkspaceService,
|
||||
WorkspacesService,
|
||||
} from '@toeverything/infra';
|
||||
import { useAtom } from 'jotai';
|
||||
import type { ReactElement } from 'react';
|
||||
@@ -19,13 +21,10 @@ import {
|
||||
openSignOutModalAtom,
|
||||
} from '../atoms';
|
||||
import { PaymentDisableModal } from '../components/affine/payment-disable';
|
||||
import { useSession } from '../hooks/affine/use-current-user';
|
||||
import { useAsyncCallback } from '../hooks/affine-async-hooks';
|
||||
import { useNavigateHelper } from '../hooks/use-navigate-helper';
|
||||
import { CurrentWorkspaceService } from '../modules/workspace/current-workspace';
|
||||
import { AuthService } from '../modules/cloud/services/auth';
|
||||
import { WorkspaceSubPath } from '../shared';
|
||||
import { mixpanel } from '../utils';
|
||||
import { signOutCloud } from '../utils/cloud-utils';
|
||||
|
||||
const SettingModal = lazy(() =>
|
||||
import('../components/affine/setting-modal').then(module => ({
|
||||
@@ -182,7 +181,7 @@ export const AuthModal = (): ReactElement => {
|
||||
};
|
||||
|
||||
export function CurrentWorkspaceModals() {
|
||||
const currentWorkspace = useService(Workspace);
|
||||
const currentWorkspace = useService(WorkspaceService).workspace;
|
||||
const [openDisableCloudAlertModal, setOpenDisableCloudAlertModal] = useAtom(
|
||||
openDisableCloudAlertModalAtom
|
||||
);
|
||||
@@ -213,21 +212,25 @@ export function CurrentWorkspaceModals() {
|
||||
|
||||
export const SignOutConfirmModal = () => {
|
||||
const { openPage } = useNavigateHelper();
|
||||
const { reload } = useSession();
|
||||
const authService = useService(AuthService);
|
||||
const [open, setOpen] = useAtom(openSignOutModalAtom);
|
||||
const currentWorkspace = useLiveData(
|
||||
useService(CurrentWorkspaceService).currentWorkspace$
|
||||
);
|
||||
const currentWorkspace = useServiceOptional(WorkspaceService)?.workspace;
|
||||
const workspaces = useLiveData(
|
||||
useService(WorkspaceManager).list.workspaceList$
|
||||
useService(WorkspacesService).list.workspaces$
|
||||
);
|
||||
|
||||
const onConfirm = useAsyncCallback(async () => {
|
||||
setOpen(false);
|
||||
await signOutCloud();
|
||||
await reload();
|
||||
|
||||
mixpanel.reset();
|
||||
try {
|
||||
await authService.signOut();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
// TODO: i18n
|
||||
notify({
|
||||
style: 'alert',
|
||||
message: 'Failed to sign out',
|
||||
});
|
||||
}
|
||||
|
||||
// if current workspace is affine cloud, switch to local workspace
|
||||
if (currentWorkspace?.flavour === WorkspaceFlavour.AFFINE_CLOUD) {
|
||||
@@ -238,7 +241,7 @@ export const SignOutConfirmModal = () => {
|
||||
openPage(localWorkspace.id, WorkspaceSubPath.ALL);
|
||||
}
|
||||
}
|
||||
}, [currentWorkspace?.flavour, openPage, reload, setOpen, workspaces]);
|
||||
}, [authService, currentWorkspace, openPage, setOpen, workspaces]);
|
||||
|
||||
return (
|
||||
<SignOutModal open={open} onOpenChange={setOpen} onConfirm={onConfirm} />
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
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 type { PropsWithChildren } from 'react';
|
||||
import { startTransition, useEffect, useRef } from 'react';
|
||||
|
||||
import { useOnceSignedInEvents } from '../atoms/event';
|
||||
import { mixpanel } from '../utils';
|
||||
|
||||
export const CloudSessionProvider = (props: PropsWithChildren) => {
|
||||
const session = useSession();
|
||||
const prevSession = useRef<ReturnType<typeof useSession>>();
|
||||
const onceSignedInEvents = useOnceSignedInEvents();
|
||||
const t = useAFFiNEI18N();
|
||||
|
||||
const refreshAfterSignedInEvents = useAsyncCallback(async () => {
|
||||
await onceSignedInEvents();
|
||||
new BroadcastChannel(
|
||||
CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY
|
||||
).postMessage(1);
|
||||
}, [onceSignedInEvents]);
|
||||
|
||||
useEffect(() => {
|
||||
if (session.user?.id) {
|
||||
mixpanel.identify(session.user.id);
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
useEffect(() => {
|
||||
if (prevSession.current !== session && session.status !== 'loading') {
|
||||
// unauthenticated -> authenticated
|
||||
if (
|
||||
prevSession.current?.status === 'unauthenticated' &&
|
||||
session.status === 'authenticated'
|
||||
) {
|
||||
startTransition(() => refreshAfterSignedInEvents());
|
||||
notify.success({
|
||||
title: t['com.affine.auth.has.signed'](),
|
||||
message: t['com.affine.auth.has.signed.message'](),
|
||||
});
|
||||
|
||||
if (environment.isDesktop) {
|
||||
affine?.ipcRenderer.send('affine:login');
|
||||
}
|
||||
}
|
||||
prevSession.current = session;
|
||||
}
|
||||
}, [session, prevSession, refreshAfterSignedInEvents, t]);
|
||||
|
||||
return props.children;
|
||||
};
|
||||
Reference in New Issue
Block a user