feat: refactor provider logic

This commit is contained in:
QiShaoXuan
2023-01-10 19:41:19 +08:00
parent 33400f1c5a
commit e8431122c9
4 changed files with 14 additions and 21 deletions

View File

@@ -55,7 +55,7 @@ export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => {
router.replace(`/workspace/${item.id}`); router.replace(`/workspace/${item.id}`);
onClose(); onClose();
}} }}
active={item.id === currentWorkspace.room} active={item.id === currentWorkspace?.room}
key={index} key={index}
> >
<span style={{ width: '100px' }}> <span style={{ width: '100px' }}>

View File

@@ -1,14 +1,11 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { useAppState } from '@/providers/app-state-provider'; import { useAppState } from '@/providers/app-state-provider';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
const defaultOutLineWorkspaceId = 'affine';
// 'local-first-' + '85b4ca0b9081421d903bbc2501ea280f';
// It is a fully effective hook // It is a fully effective hook
// Cause it not just ensure workspace loaded, but also have router change. // Cause it not just ensure workspace loaded, but also have router change.
export const useEnsureWorkspace = () => { export const useEnsureWorkspace = () => {
const [workspaceLoaded, setWorkspaceLoaded] = useState(false); const [workspaceLoaded, setWorkspaceLoaded] = useState(false);
const { workspaceList, loadWorkspace, user } = useAppState(); const { workspaceList, loadWorkspace, user } = useAppState();
console.log('workspaceList: ', workspaceList);
const router = useRouter(); const router = useRouter();
// const defaultOutLineWorkspaceId = '99ce7eb7'; // const defaultOutLineWorkspaceId = '99ce7eb7';
@@ -36,9 +33,8 @@ export const useEnsureWorkspace = () => {
// return; // return;
// } // }
const workspaceId = user const workspaceId =
? (router.query.workspaceId as string) || workspaceList[0]?.id (router.query.workspaceId as string) || workspaceList[0]?.id;
: (router.query.workspaceId as string) || defaultOutLineWorkspaceId;
loadWorkspace(workspaceId).finally(() => { loadWorkspace(workspaceId).finally(() => {
setWorkspaceLoaded(true); setWorkspaceLoaded(true);

View File

@@ -22,34 +22,29 @@ export const AppStateProvider = ({
const [appState, setAppState] = useState<AppStateValue>({} as AppStateValue); const [appState, setAppState] = useState<AppStateValue>({} as AppStateValue);
useEffect(() => { useEffect(() => {
const init = async () => { const initState = async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
// Ensure datacenter has at least one workspace
if (dataCenter.workspaces.length === 0) { if (dataCenter.workspaces.length === 0) {
await createDefaultWorkspace(dataCenter); await createDefaultWorkspace(dataCenter);
} }
const currentWorkspace = await dataCenter.loadWorkspace(
dataCenter.workspaces[0].id
);
const currentMetaWorkSpace = dataCenter.workspaces.find(item => {
return item.id === currentWorkspace.room;
});
setAppState({ setAppState({
dataCenter, dataCenter,
user: (await dataCenter.getUserInfo()) || null, user: (await dataCenter.getUserInfo()) || null,
workspaceList: dataCenter.workspaces, workspaceList: dataCenter.workspaces,
currentWorkspaceId: dataCenter.workspaces[0].id, currentWorkspaceId: '',
currentWorkspace, currentWorkspace: null,
pageList: currentWorkspace.meta.pageMetas as PageMeta[], pageList: [],
currentPage: null, currentPage: null,
editor: null, editor: null,
synced: true, synced: true,
currentMetaWorkSpace: currentMetaWorkSpace ?? null, currentMetaWorkSpace: null,
}); });
}; };
init(); initState();
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -94,6 +89,8 @@ export const AppStateProvider = ({
const loadWorkspace = useRef<AppStateFunction['loadWorkspace']>(); const loadWorkspace = useRef<AppStateFunction['loadWorkspace']>();
loadWorkspace.current = async (workspaceId: string) => { loadWorkspace.current = async (workspaceId: string) => {
console.log('loadWorkspace');
const { dataCenter, workspaceList, currentWorkspaceId, currentWorkspace } = const { dataCenter, workspaceList, currentWorkspaceId, currentWorkspace } =
appState; appState;
if (!workspaceList.find(v => v.id === workspaceId)) { if (!workspaceList.find(v => v.id === workspaceId)) {
@@ -113,7 +110,7 @@ export const AppStateProvider = ({
currentWorkspace: workspace, currentWorkspace: workspace,
currentWorkspaceId: workspaceId, currentWorkspaceId: workspaceId,
currentMetaWorkSpace: currentMetaWorkSpace ?? null, currentMetaWorkSpace: currentMetaWorkSpace ?? null,
pageList: currentWorkspace.meta.pageMetas as PageMeta[], pageList: currentWorkspace?.meta.pageMetas as PageMeta[],
currentPage: null, currentPage: null,
editor: null, editor: null,
}); });

View File

@@ -19,7 +19,7 @@ export type AppStateValue = {
dataCenter: DataCenter; dataCenter: DataCenter;
user: User | null; user: User | null;
workspaceList: WorkspaceInfo[]; workspaceList: WorkspaceInfo[];
currentWorkspace: StoreWorkspace; currentWorkspace: StoreWorkspace | null;
currentMetaWorkSpace: WorkspaceInfo | null; currentMetaWorkSpace: WorkspaceInfo | null;
currentWorkspaceId: string; currentWorkspaceId: string;
pageList: PageMeta[]; pageList: PageMeta[];