-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.ts
28 lines (24 loc) · 965 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useSelect } from '@wordpress/data';
// @ts-ignore-next-line - The type definitions for the editor package are incomplete.
import { store as editorStore } from '@wordpress/editor';
import { usePostContext } from '../../components/post-context/context';
export function usePost() {
const {
postId: blockContextPostId,
postType: blockContextPostType,
isEditable: blockContextIsEditable,
} = usePostContext();
const { globalPostId, globalPostType } = useSelect(
(select) => ({
globalPostId: select(editorStore).getCurrentPostId(),
globalPostType: select(editorStore).getCurrentPostType(),
}),
[],
);
const hasBlockContext = !!blockContextPostId && !!blockContextPostType;
return {
postId: (blockContextPostId || globalPostId) as number | null | undefined | string,
postType: (blockContextPostType || globalPostType) as string,
isEditable: (hasBlockContext ? blockContextIsEditable : true) as boolean | null | undefined,
};
}