mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
refactor: workspace manager (#5060)
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { WorkspaceSubPath } from '@affine/env/workspace';
|
||||
import { WorkspaceFlavour, WorkspaceSubPath } from '@affine/env/workspace';
|
||||
import {
|
||||
currentWorkspaceAtom,
|
||||
waitForCurrentWorkspaceAtom,
|
||||
workspaceListAtom,
|
||||
} from '@affine/workspace/atom';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { useAsyncCallback } from '@toeverything/hooks/affine-async-hooks';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import type { ReactElement } from 'react';
|
||||
import { lazy, Suspense, useCallback } from 'react';
|
||||
|
||||
@@ -14,7 +19,6 @@ import {
|
||||
openSignOutModalAtom,
|
||||
} from '../atoms';
|
||||
import { PaymentDisableModal } from '../components/affine/payment-disable';
|
||||
import { useCurrentWorkspace } from '../hooks/current/use-current-workspace';
|
||||
import { useNavigateHelper } from '../hooks/use-navigate-helper';
|
||||
import { signOutCloud } from '../utils/cloud-utils';
|
||||
|
||||
@@ -56,33 +60,43 @@ const SignOutModal = lazy(() =>
|
||||
);
|
||||
|
||||
export const Setting = () => {
|
||||
const [currentWorkspace] = useCurrentWorkspace();
|
||||
const [{ open, workspaceId, activeTab }, setOpenSettingModalAtom] =
|
||||
const currentWorkspace = useAtomValue(waitForCurrentWorkspaceAtom);
|
||||
const [{ open, workspaceMetadata, activeTab }, setOpenSettingModalAtom] =
|
||||
useAtom(openSettingModalAtom);
|
||||
assertExists(currentWorkspace);
|
||||
|
||||
const onSettingClick = useCallback(
|
||||
({
|
||||
activeTab,
|
||||
workspaceId,
|
||||
}: Pick<SettingAtom, 'activeTab' | 'workspaceId'>) => {
|
||||
setOpenSettingModalAtom(prev => ({ ...prev, activeTab, workspaceId }));
|
||||
workspaceMetadata,
|
||||
}: Pick<SettingAtom, 'activeTab' | 'workspaceMetadata'>) => {
|
||||
setOpenSettingModalAtom(prev => ({
|
||||
...prev,
|
||||
activeTab,
|
||||
workspaceMetadata,
|
||||
}));
|
||||
},
|
||||
[setOpenSettingModalAtom]
|
||||
);
|
||||
|
||||
const onOpenChange = useCallback(
|
||||
(open: boolean) => {
|
||||
setOpenSettingModalAtom(prev => ({ ...prev, open }));
|
||||
},
|
||||
[setOpenSettingModalAtom]
|
||||
);
|
||||
|
||||
if (!open) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<SettingModal
|
||||
open={open}
|
||||
activeTab={activeTab}
|
||||
workspaceId={workspaceId}
|
||||
workspaceMetadata={workspaceMetadata}
|
||||
onSettingClick={onSettingClick}
|
||||
onOpenChange={useCallback(
|
||||
(open: boolean) => {
|
||||
setOpenSettingModalAtom(prev => ({ ...prev, open }));
|
||||
},
|
||||
[setOpenSettingModalAtom]
|
||||
)}
|
||||
onOpenChange={onOpenChange}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -128,7 +142,7 @@ export const AuthModal = (): ReactElement => {
|
||||
};
|
||||
|
||||
export function CurrentWorkspaceModals() {
|
||||
const [currentWorkspace] = useCurrentWorkspace();
|
||||
const currentWorkspace = useAtomValue(waitForCurrentWorkspaceAtom);
|
||||
const [openDisableCloudAlertModal, setOpenDisableCloudAlertModal] = useAtom(
|
||||
openDisableCloudAlertModalAtom
|
||||
);
|
||||
@@ -152,14 +166,25 @@ export function CurrentWorkspaceModals() {
|
||||
}
|
||||
|
||||
export const SignOutConfirmModal = () => {
|
||||
const { jumpToIndex } = useNavigateHelper();
|
||||
const { openPage } = useNavigateHelper();
|
||||
const [open, setOpen] = useAtom(openSignOutModalAtom);
|
||||
const currentWorkspace = useAtomValue(currentWorkspaceAtom);
|
||||
const workspaceList = useAtomValue(workspaceListAtom);
|
||||
|
||||
const onConfirm = useAsyncCallback(async () => {
|
||||
setOpen(false);
|
||||
await signOutCloud();
|
||||
jumpToIndex();
|
||||
}, [jumpToIndex, setOpen]);
|
||||
|
||||
// if current workspace is affine cloud, switch to local workspace
|
||||
if (currentWorkspace?.flavour === WorkspaceFlavour.AFFINE_CLOUD) {
|
||||
const localWorkspace = workspaceList.find(
|
||||
w => w.flavour === WorkspaceFlavour.LOCAL
|
||||
);
|
||||
if (localWorkspace) {
|
||||
openPage(localWorkspace.id, WorkspaceSubPath.ALL);
|
||||
}
|
||||
}
|
||||
}, [currentWorkspace?.flavour, openPage, setOpen, workspaceList]);
|
||||
|
||||
return (
|
||||
<SignOutModal open={open} onOpenChange={setOpen} onConfirm={onConfirm} />
|
||||
|
||||
@@ -2,7 +2,7 @@ import '@toeverything/hooks/use-affine-ipc-renderer';
|
||||
|
||||
import { pushNotificationAtom } from '@affine/component/notification-center';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import { refreshRootMetadataAtom } from '@affine/workspace/atom';
|
||||
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace';
|
||||
import { useAsyncCallback } from '@toeverything/hooks/affine-async-hooks';
|
||||
import { useAtom, useSetAtom } from 'jotai';
|
||||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||
@@ -22,14 +22,15 @@ const SessionDefence = (props: PropsWithChildren) => {
|
||||
const prevSession = useRef<ReturnType<typeof useSession>>();
|
||||
const [sessionInAtom, setSession] = useAtom(sessionAtom);
|
||||
const pushNotification = useSetAtom(pushNotificationAtom);
|
||||
const refreshMetadata = useSetAtom(refreshRootMetadataAtom);
|
||||
const onceSignedInEvents = useOnceSignedInEvents();
|
||||
const t = useAFFiNEI18N();
|
||||
|
||||
const refreshAfterSignedInEvents = useAsyncCallback(async () => {
|
||||
await onceSignedInEvents();
|
||||
refreshMetadata();
|
||||
}, [onceSignedInEvents, refreshMetadata]);
|
||||
new BroadcastChannel(
|
||||
CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY
|
||||
).postMessage(1);
|
||||
}, [onceSignedInEvents]);
|
||||
|
||||
useEffect(() => {
|
||||
if (sessionInAtom !== session && session.status === 'authenticated') {
|
||||
|
||||
Reference in New Issue
Block a user