-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from 10up/fix/make-has-supported-meta-value-c…
…ontext-aware Fix: Make `useIsSupportedMetaField` hook context aware
- Loading branch information
Showing
3 changed files
with
18 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,9 @@ | |
"default": "" | ||
} | ||
}, | ||
"usesContext": [ | ||
"postId", | ||
"postType" | ||
], | ||
"editorScript": "file:./index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { useEntityRecord } from '@wordpress/core-data'; | ||
import { usePost } from '../use-post'; | ||
|
||
export const useIsSupportedMetaField = (metaKey) => { | ||
return useSelect( | ||
(select) => { | ||
const meta = select(editorStore).getCurrentPostAttribute('meta'); | ||
const supportedMetaKeys = Object.keys(meta || {}); | ||
const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey); | ||
return [!!isSupportedMetaField]; | ||
}, | ||
[metaKey], | ||
); | ||
const { postId, postType } = usePost(); | ||
const { record } = useEntityRecord('postType', postType, postId); | ||
const { meta } = record || {}; | ||
const supportedMetaKeys = Object.keys(meta || {}); | ||
const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey); | ||
return [!!isSupportedMetaField]; | ||
}; |