mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
31 lines
796 B
TypeScript
31 lines
796 B
TypeScript
import type { Page, Workspace } from '@blocksuite/store';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export function useBlockSuiteWorkspacePage(
|
|
blockSuiteWorkspace: Workspace,
|
|
pageId: string | null
|
|
): Page | null {
|
|
const [page, setPage] = useState(() => {
|
|
if (pageId === null) {
|
|
return null;
|
|
}
|
|
return blockSuiteWorkspace.getPage(pageId);
|
|
});
|
|
useEffect(() => {
|
|
if (pageId) {
|
|
setPage(blockSuiteWorkspace.getPage(pageId));
|
|
}
|
|
}, [blockSuiteWorkspace, pageId]);
|
|
useEffect(() => {
|
|
const disposable = blockSuiteWorkspace.slots.pageAdded.on(id => {
|
|
if (pageId === id) {
|
|
setPage(blockSuiteWorkspace.getPage(id));
|
|
}
|
|
});
|
|
return () => {
|
|
disposable.dispose();
|
|
};
|
|
}, [blockSuiteWorkspace, pageId]);
|
|
return page;
|
|
}
|