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.

feat: refactor provider logic

QiShaoXuane8431122c9a2
diff --git a/packages/app/src/components/workspace-modal/index.tsx b/packages/app/src/components/workspace-modal/index.tsx
index 3621d22e47..08372b52ba 100644
--- a/packages/app/src/components/workspace-modal/index.tsx
+++ b/packages/app/src/components/workspace-modal/index.tsx
@@ -55,7 +55,7 @@ export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
router.replace(`/workspace/${item.id}`);
onClose();
}}
- active={item.id === currentWorkspace.room}
+ active={item.id === currentWorkspace?.room}
key={index}
>
<span style={{ width: '100px' }}>
diff --git a/packages/app/src/hooks/use-ensure-workspace.ts b/packages/app/src/hooks/use-ensure-workspace.ts
index 205443aeed..03d50f26fa 100644
--- a/packages/app/src/hooks/use-ensure-workspace.ts
+++ b/packages/app/src/hooks/use-ensure-workspace.ts
@@ -1,14 +1,11 @@
import { useState, useEffect } from 'react';
import { useAppState } from '@/providers/app-state-provider';
import { useRouter } from 'next/router';
-const defaultOutLineWorkspaceId = 'affine';
-// 'local-first-' + '85b4ca0b9081421d903bbc2501ea280f';
// It is a fully effective hook
// Cause it not just ensure workspace loaded, but also have router change.
export const useEnsureWorkspace = () => {
const [workspaceLoaded, setWorkspaceLoaded] = useState(false);
const { workspaceList, loadWorkspace, user } = useAppState();
- console.log('workspaceList: ', workspaceList);
const router = useRouter();
// const defaultOutLineWorkspaceId = '99ce7eb7';
@@ -36,9 +33,8 @@ export const useEnsureWorkspace = () => {
// return;
// }
- const workspaceId = user
- ? (router.query.workspaceId as string) || workspaceList[0]?.id
- : (router.query.workspaceId as string) || defaultOutLineWorkspaceId;
+ const workspaceId =
+ (router.query.workspaceId as string) || workspaceList[0]?.id;
loadWorkspace(workspaceId).finally(() => {
setWorkspaceLoaded(true);
diff --git a/packages/app/src/providers/app-state-provider/Provider.tsx b/packages/app/src/providers/app-state-provider/Provider.tsx
index 2c85e560e7..e0a833d812 100644
--- a/packages/app/src/providers/app-state-provider/Provider.tsx
+++ b/packages/app/src/providers/app-state-provider/Provider.tsx
@@ -22,34 +22,29 @@ export const AppStateProvider = ({
const [appState, setAppState] = useState<AppStateValue>({} as AppStateValue);
useEffect(() => {
- const init = async () => {
+ const initState = async () => {
const dataCenter = await getDataCenter();
+ // Ensure datacenter has at least one workspace
if (dataCenter.workspaces.length === 0) {
await createDefaultWorkspace(dataCenter);
}
- const currentWorkspace = await dataCenter.loadWorkspace(
- dataCenter.workspaces[0].id
- );
- const currentMetaWorkSpace = dataCenter.workspaces.find(item => {
- return item.id === currentWorkspace.room;
- });
setAppState({
dataCenter,
user: (await dataCenter.getUserInfo()) || null,
workspaceList: dataCenter.workspaces,
- currentWorkspaceId: dataCenter.workspaces[0].id,
- currentWorkspace,
- pageList: currentWorkspace.meta.pageMetas as PageMeta[],
+ currentWorkspaceId: '',
+ currentWorkspace: null,
+ pageList: [],
currentPage: null,
editor: null,
synced: true,
- currentMetaWorkSpace: currentMetaWorkSpace ?? null,
+ currentMetaWorkSpace: null,
});
};
- init();
+ initState();
}, []);
useEffect(() => {
@@ -94,6 +89,8 @@ export const AppStateProvider = ({
const loadWorkspace = useRef<AppStateFunction['loadWorkspace']>();
loadWorkspace.current = async (workspaceId: string) => {
+ console.log('loadWorkspace');
+
const { dataCenter, workspaceList, currentWorkspaceId, currentWorkspace } =
appState;
if (!workspaceList.find(v => v.id === workspaceId)) {
@@ -113,7 +110,7 @@ export const AppStateProvider = ({
currentWorkspace: workspace,
currentWorkspaceId: workspaceId,
currentMetaWorkSpace: currentMetaWorkSpace ?? null,
- pageList: currentWorkspace.meta.pageMetas as PageMeta[],
+ pageList: currentWorkspace?.meta.pageMetas as PageMeta[],
currentPage: null,
editor: null,
});
diff --git a/packages/app/src/providers/app-state-provider/interface.ts b/packages/app/src/providers/app-state-provider/interface.ts
index 4e5ec6a9b9..ca178781f5 100644
--- a/packages/app/src/providers/app-state-provider/interface.ts
+++ b/packages/app/src/providers/app-state-provider/interface.ts
@@ -19,7 +19,7 @@ export type AppStateValue = {
dataCenter: DataCenter;
user: User | null;
workspaceList: WorkspaceInfo[];
- currentWorkspace: StoreWorkspace;
+ currentWorkspace: StoreWorkspace | null;
currentMetaWorkSpace: WorkspaceInfo | null;
currentWorkspaceId: string;
pageList: PageMeta[];