mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-27 03:42:08 +03:00
feat: refactor provider logic
This commit is contained in:
parent
33400f1c5a
commit
e8431122c9
@ -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' }}>
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
});
|
||||
|
@ -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[];
|
||||
|
Loading…
Reference in New Issue
Block a user