diff --git a/packages/app/src/components/logout-modal/index.tsx b/packages/app/src/components/logout-modal/index.tsx
index 19b6b04d93..d710b2e451 100644
--- a/packages/app/src/components/logout-modal/index.tsx
+++ b/packages/app/src/components/logout-modal/index.tsx
@@ -51,26 +51,49 @@ export const LogoutModal = ({ open, onClose }: LoginModalProps) => {
)}
{t('Retain local cached data')}
-
-
-
-
+ {blobDataSynced ? (
+
+
+
+
+ ) : (
+
+
+
+
+ )}
diff --git a/packages/app/src/components/workspace-modal/index.tsx b/packages/app/src/components/workspace-modal/index.tsx
index 1363763c36..4a6e78e90d 100644
--- a/packages/app/src/components/workspace-modal/index.tsx
+++ b/packages/app/src/components/workspace-modal/index.tsx
@@ -36,7 +36,7 @@ interface WorkspaceModalProps {
export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
const [createWorkspaceOpen, setCreateWorkspaceOpen] = useState(false);
- const { workspaceList, logout } = useAppState();
+ const { logout, dataCenter } = useAppState();
const router = useRouter();
const { t } = useTranslation();
const [loginOpen, setLoginOpen] = useState(false);
@@ -80,7 +80,7 @@ export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
- {workspaceList.map((item, index) => {
+ {dataCenter.workspaces.map((item, index) => {
return (
{
onClose={async wait => {
if (!wait) {
await logout();
- router.replace(`/workspace`);
+ if (dataCenter.workspaces.length === 0) {
+ router.push(`/workspace`);
+ } else {
+ router.push(`/workspace/${dataCenter.workspaces[0].id}`);
+ }
}
setLogoutOpen(false);
}}
diff --git a/packages/app/src/components/workspace-setting/general/delete/Delete.tsx b/packages/app/src/components/workspace-setting/general/delete/Delete.tsx
index 3fcab8c158..af2570f813 100644
--- a/packages/app/src/components/workspace-setting/general/delete/Delete.tsx
+++ b/packages/app/src/components/workspace-setting/general/delete/Delete.tsx
@@ -1,3 +1,4 @@
+import { useRouter } from 'next/router';
import Modal from '@/ui/modal';
import Input from '@/ui/input';
import {
@@ -11,7 +12,6 @@ import {
import { useState } from 'react';
import { ModalCloseButton } from '@/ui/modal';
import { Button } from '@/ui/button';
-import { useRouter } from 'next/router';
import { WorkspaceUnit } from '@affine/datacenter';
import { Trans, useTranslation } from '@affine/i18n';
diff --git a/packages/app/src/providers/app-state-provider/Provider.tsx b/packages/app/src/providers/app-state-provider/Provider.tsx
index 5dd18094ad..b8601e9eac 100644
--- a/packages/app/src/providers/app-state-provider/Provider.tsx
+++ b/packages/app/src/providers/app-state-provider/Provider.tsx
@@ -24,6 +24,7 @@ export const AppStateProvider = ({
}: PropsWithChildren) => {
const [appState, setAppState] = useState({} as AppStateValue);
const [blobState, setBlobState] = useState(false);
+ const [userInfo, setUser] = useState({} as User);
useEffect(() => {
const initState = async () => {
const dataCenter = await getDataCenter();
@@ -31,10 +32,9 @@ export const AppStateProvider = ({
if (dataCenter.workspaces.length === 0) {
await createDefaultWorkspace(dataCenter);
}
-
+ setUser((await dataCenter.getUserInfo()) || null);
setAppState({
dataCenter,
- user: (await dataCenter.getUserInfo()) || null,
workspaceList: dataCenter.workspaces,
currentWorkspace: null,
pageList: [],
@@ -97,7 +97,7 @@ export const AppStateProvider = ({
const loadWorkspace: AppStateFunction['loadWorkspace'] =
useRef() as AppStateFunction['loadWorkspace'];
loadWorkspace.current = async (workspaceId: string) => {
- const { dataCenter, workspaceList, currentWorkspace, user } = appState;
+ const { dataCenter, workspaceList, currentWorkspace } = appState;
if (!workspaceList.find(v => v.id.toString() === workspaceId)) {
return null;
}
@@ -112,7 +112,7 @@ export const AppStateProvider = ({
isOwner = true;
} else {
// We must ensure workspace.owner exists, then ensure id same.
- isOwner = workspace?.owner && user?.id === workspace.owner.id;
+ isOwner = workspace?.owner && userInfo?.id === workspace.owner.id;
}
const pageList =
@@ -166,22 +166,13 @@ export const AppStateProvider = ({
if (!user) {
throw new Error('User info not found');
}
- setAppState({
- ...appState,
- workspaceList: dataCenter.workspaces,
- user,
- });
+ setUser(user);
return user;
};
-
const logout = async () => {
const { dataCenter } = appState;
await dataCenter.logout();
- setAppState({
- ...appState,
- workspaceList: dataCenter.workspaces,
- user: null,
- });
+ setUser(null);
};
return (
@@ -194,6 +185,7 @@ export const AppStateProvider = ({
login,
logout,
blobDataSynced: blobState,
+ user: userInfo,
}}
>
{children}
diff --git a/packages/app/src/providers/app-state-provider/interface.ts b/packages/app/src/providers/app-state-provider/interface.ts
index a1ea400229..ee960a0a9e 100644
--- a/packages/app/src/providers/app-state-provider/interface.ts
+++ b/packages/app/src/providers/app-state-provider/interface.ts
@@ -16,7 +16,7 @@ export interface PageMeta extends StorePageMeta {
export type AppStateValue = {
dataCenter: DataCenter;
- user: User | null;
+ user?: User | null;
workspaceList: WorkspaceUnit[];
currentWorkspace: WorkspaceUnit | null;
pageList: PageMeta[];
diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts
index 354d08a407..8749db5fa6 100644
--- a/packages/data-center/src/provider/affine/affine.ts
+++ b/packages/data-center/src/provider/affine/affine.ts
@@ -430,7 +430,7 @@ export class AffineProvider extends BaseProvider {
token.clear();
this._channel?.disconnect();
this._wsMap.forEach(ws => ws.disconnect());
- this._workspaces.clear();
+ this._workspaces.clear(false);
storage.removeItem('token');
}
diff --git a/packages/data-center/src/workspace-unit-collection.ts b/packages/data-center/src/workspace-unit-collection.ts
index c44ef3feb0..d5a5b0b925 100644
--- a/packages/data-center/src/workspace-unit-collection.ts
+++ b/packages/data-center/src/workspace-unit-collection.ts
@@ -8,8 +8,8 @@ export interface WorkspaceUnitCollectionScope {
get: (workspaceId: string) => WorkspaceUnit | undefined;
list: () => WorkspaceUnit[];
add: (workspace: WorkspaceUnit | WorkspaceUnit[]) => void;
- remove: (workspaceId: string | string[]) => boolean;
- clear: () => void;
+ remove: (workspaceId: string | string[], isUpdate?: boolean) => boolean;
+ clear: (isUpdate?: boolean) => void;
update: (
workspaceId: string,
workspaceUnit: UpdateWorkspaceUnitParams
@@ -85,7 +85,7 @@ export class WorkspaceUnitCollection {
]);
};
- const remove = (workspaceId: string | string[]) => {
+ const remove = (workspaceId: string | string[], isUpdate = true) => {
const workspaceIds = Array.isArray(workspaceId)
? workspaceId
: [workspaceId];
@@ -111,18 +111,19 @@ export class WorkspaceUnitCollection {
if (!workspaceUnits.length) {
return false;
}
-
- this._events.emit('change', [
- {
- deleted: workspaceUnits,
- } as WorkspaceUnitCollectionChangeEvent,
- ]);
+ if (isUpdate) {
+ this._events.emit('change', [
+ {
+ deleted: workspaceUnits,
+ } as WorkspaceUnitCollectionChangeEvent,
+ ]);
+ }
return true;
};
- const clear = () => {
- remove(Array.from(scopedWorkspaceIds));
+ const clear = (isUpdate = true) => {
+ remove(Array.from(scopedWorkspaceIds), isUpdate);
};
const update = (workspaceId: string, meta: UpdateWorkspaceUnitParams) => {