PluginProbe
Extendify / 0.5.0
Extendify v0.5.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / ExtendifyLibrary.js

ExtendifyLibrary.js in Extendify 0.5.0, at src/ExtendifyLibrary.js

67 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useCallback } from '@wordpress/element'
2 import { General as GeneralApi } from '@extendify/api/General'
3 import MainWindow from '@extendify/pages/MainWindow'
4 import { useGlobalStore } from '@extendify/state/GlobalState'
5 import { useTemplatesStore } from '@extendify/state/Templates'
6 import { useUserStore } from '@extendify/state/User'
7 import '@extendify/utility-control'
8 import { useTaxonomyStore } from './state/Taxonomies'
9
10 export default function ExtendifyLibrary({ show = false }) {
11 const open = useGlobalStore((state) => state.open)
12 const setReady = useGlobalStore((state) => state.setReady)
13 const setOpen = useGlobalStore((state) => state.setOpen)
14 const showLibrary = useCallback(() => setOpen(true), [setOpen])
15 const hideLibrary = useCallback(() => setOpen(false), [setOpen])
16 const initTemplateData = useTemplatesStore(
17 (state) => state.initTemplateData,
18 )
19 const fetchTaxonomies = useTaxonomyStore((state) => state.fetchTaxonomies)
20
21 // When the uuid of the user comes back from the database, we can
22 // assume that the state object is ready. This is important to check
23 // as the library may be "open" when loaded, but not ready.
24 const userHasHydrated = useUserStore((state) => state._hasHydrated)
25 const taxonomiesReady = useTemplatesStore(
26 (state) => Object.keys(state.taxonomyDefaultState).length > 0,
27 )
28
29 useEffect(() => {
30 if (!open) return
31 fetchTaxonomies().then(() => {
32 useTemplatesStore.getState().setupDefaultTaxonomies()
33 })
34 }, [open, fetchTaxonomies])
35
36 useEffect(() => {
37 if (userHasHydrated && taxonomiesReady) {
38 initTemplateData()
39 setReady(true)
40 }
41 }, [userHasHydrated, taxonomiesReady, initTemplateData, setReady])
42
43 useEffect(() => {
44 show && setOpen(true)
45 }, [show, setOpen])
46
47 useEffect(() => {
48 GeneralApi.metaData().then((data) => {
49 useGlobalStore.setState({
50 metaData: data,
51 })
52 })
53 }, [])
54
55 // Let the visibility to be controlled from outside the application
56 useEffect(() => {
57 window.addEventListener('extendify::open-library', showLibrary)
58 window.addEventListener('extendify::close-library', hideLibrary)
59 return () => {
60 window.removeEventListener('extendify::open-library', showLibrary)
61 window.removeEventListener('extendify::close-library', hideLibrary)
62 }
63 }, [hideLibrary, showLibrary])
64
65 return <MainWindow />
66 }
67