fix: appState dependency
This commit is contained in:
@@ -38,7 +38,7 @@ export const useEnsureWorkspace = () => {
|
|||||||
// }
|
// }
|
||||||
const workspaceId =
|
const workspaceId =
|
||||||
(router.query.workspaceId as string) || workspaceList[0]?.id;
|
(router.query.workspaceId as string) || workspaceList[0]?.id;
|
||||||
loadWorkspace(workspaceId).finally(() => {
|
loadWorkspace.current(workspaceId).finally(() => {
|
||||||
setWorkspaceLoaded(true);
|
setWorkspaceLoaded(true);
|
||||||
setActiveWorkspaceId(activeWorkspaceId);
|
setActiveWorkspaceId(activeWorkspaceId);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ type AppStateContextProps = PropsWithChildren<Record<string, unknown>>;
|
|||||||
export const AppState = createContext<AppStateContext>({} as AppStateContext);
|
export const AppState = createContext<AppStateContext>({} as AppStateContext);
|
||||||
|
|
||||||
export const useAppState = () => useContext(AppState);
|
export const useAppState = () => useContext(AppState);
|
||||||
let syncChangeDisposable: Disposable | undefined = undefined;
|
|
||||||
export const AppStateProvider = ({
|
export const AppStateProvider = ({
|
||||||
children,
|
children,
|
||||||
}: PropsWithChildren<AppStateContextProps>) => {
|
}: PropsWithChildren<AppStateContextProps>) => {
|
||||||
const [appState, setAppState] = useState<AppStateValue>({} as AppStateValue);
|
const [appState, setAppState] = useState<AppStateValue>({} as AppStateValue);
|
||||||
|
const [blobState, setBlobState] = useState(false);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initState = async () => {
|
const initState = async () => {
|
||||||
const dataCenter = await getDataCenter();
|
const dataCenter = await getDataCenter();
|
||||||
@@ -94,7 +94,8 @@ export const AppStateProvider = ({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadWorkspace = useRef<AppStateFunction['loadWorkspace']>();
|
const loadWorkspace: AppStateFunction['loadWorkspace'] =
|
||||||
|
useRef() as AppStateFunction['loadWorkspace'];
|
||||||
loadWorkspace.current = async (workspaceId: string) => {
|
loadWorkspace.current = async (workspaceId: string) => {
|
||||||
const { dataCenter, workspaceList, currentWorkspace, user } = appState;
|
const { dataCenter, workspaceList, currentWorkspace, user } = appState;
|
||||||
if (!workspaceList.find(v => v.id.toString() === workspaceId)) {
|
if (!workspaceList.find(v => v.id.toString() === workspaceId)) {
|
||||||
@@ -103,7 +104,7 @@ export const AppStateProvider = ({
|
|||||||
if (workspaceId === currentWorkspace?.id) {
|
if (workspaceId === currentWorkspace?.id) {
|
||||||
return currentWorkspace;
|
return currentWorkspace;
|
||||||
}
|
}
|
||||||
syncChangeDisposable?.dispose();
|
|
||||||
const workspace = (await dataCenter.loadWorkspace(workspaceId)) ?? null;
|
const workspace = (await dataCenter.loadWorkspace(workspaceId)) ?? null;
|
||||||
let isOwner;
|
let isOwner;
|
||||||
if (workspace?.provider === 'local') {
|
if (workspace?.provider === 'local') {
|
||||||
@@ -113,13 +114,7 @@ export const AppStateProvider = ({
|
|||||||
// We must ensure workspace.owner exists, then ensure id same.
|
// We must ensure workspace.owner exists, then ensure id same.
|
||||||
isOwner = workspace?.owner && user?.id === workspace.owner.id;
|
isOwner = workspace?.owner && user?.id === workspace.owner.id;
|
||||||
}
|
}
|
||||||
const blobStorage = await workspace?.blocksuiteWorkspace?.blobs;
|
|
||||||
syncChangeDisposable = blobStorage?.signals.onBlobSyncStateChange.on(() => {
|
|
||||||
setAppState({
|
|
||||||
...appState,
|
|
||||||
blobDataSynced: blobStorage?.uploading,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const pageList =
|
const pageList =
|
||||||
(workspace?.blocksuiteWorkspace?.meta.pageMetas as PageMeta[]) ?? [];
|
(workspace?.blocksuiteWorkspace?.meta.pageMetas as PageMeta[]) ?? [];
|
||||||
setAppState({
|
setAppState({
|
||||||
@@ -129,12 +124,31 @@ export const AppStateProvider = ({
|
|||||||
currentPage: null,
|
currentPage: null,
|
||||||
editor: null,
|
editor: null,
|
||||||
isOwner,
|
isOwner,
|
||||||
blobDataSynced: blobStorage?.uploading,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return workspace;
|
return workspace;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let syncChangeDisposable: Disposable | undefined;
|
||||||
|
const currentWorkspace = appState.currentWorkspace;
|
||||||
|
if (!currentWorkspace) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const getBlobStorage = async () => {
|
||||||
|
const blobStorage = await currentWorkspace?.blocksuiteWorkspace?.blobs;
|
||||||
|
syncChangeDisposable = blobStorage?.signals.onBlobSyncStateChange.on(
|
||||||
|
() => {
|
||||||
|
setBlobState(blobStorage?.uploading);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
getBlobStorage();
|
||||||
|
return () => {
|
||||||
|
syncChangeDisposable?.dispose();
|
||||||
|
};
|
||||||
|
}, [appState.currentWorkspace]);
|
||||||
|
|
||||||
const setEditor: AppStateFunction['setEditor'] =
|
const setEditor: AppStateFunction['setEditor'] =
|
||||||
useRef() as AppStateFunction['setEditor'];
|
useRef() as AppStateFunction['setEditor'];
|
||||||
setEditor.current = editor => {
|
setEditor.current = editor => {
|
||||||
@@ -176,9 +190,10 @@ export const AppStateProvider = ({
|
|||||||
...appState,
|
...appState,
|
||||||
setEditor,
|
setEditor,
|
||||||
loadPage: loadPage.current,
|
loadPage: loadPage.current,
|
||||||
loadWorkspace: loadWorkspace.current,
|
loadWorkspace: loadWorkspace,
|
||||||
login,
|
login,
|
||||||
logout,
|
logout,
|
||||||
|
blobDataSynced: blobState,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ export type AppStateValue = {
|
|||||||
export type AppStateFunction = {
|
export type AppStateFunction = {
|
||||||
setEditor: MutableRefObject<(page: EditorContainer) => void>;
|
setEditor: MutableRefObject<(page: EditorContainer) => void>;
|
||||||
|
|
||||||
loadWorkspace: (workspaceId: string) => Promise<WorkspaceUnit | null>;
|
loadWorkspace: MutableRefObject<
|
||||||
|
(workspaceId: string) => Promise<WorkspaceUnit | null>
|
||||||
|
>;
|
||||||
loadPage: (pageId: string) => void;
|
loadPage: (pageId: string) => void;
|
||||||
|
|
||||||
login: () => Promise<User>;
|
login: () => Promise<User>;
|
||||||
|
|||||||
Reference in New Issue
Block a user