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}`);
onClose();
}}
active={item.id === currentWorkspace.room}
active={item.id === currentWorkspace?.room}
key={index}
>
<span style={{ width: '100px' }}>

View File

@ -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);

View File

@ -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,
});

View File

@ -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[];