| 1 |
import { useEffect, useCallback } from '@wordpress/element' |
| 2 |
import { General as GeneralApi } from '@library/api/General' |
| 3 |
import MainWindow from '@library/pages/MainWindow' |
| 4 |
import { useGlobalStore } from '@library/state/GlobalState' |
| 5 |
import { useTemplatesStore } from '@library/state/Templates' |
| 6 |
import { useUserStoreReady } from '@library/state/User' |
| 7 |
import '@library/utility-control' |
| 8 |
import { useTaxonomyStore } from './state/Taxonomies' |
| 9 |
|
| 10 |
export default function ExtendifyLibrary({ show = false }) { |
| 11 |
const { open, setReady, setOpen } = useGlobalStore() |
| 12 |
const showLibrary = useCallback(() => setOpen(true), [setOpen]) |
| 13 |
const hideLibrary = useCallback(() => setOpen(false), [setOpen]) |
| 14 |
const { initTemplateData } = useTemplatesStore() |
| 15 |
const fetchTaxonomies = useTaxonomyStore((state) => state.fetchTaxonomies) |
| 16 |
|
| 17 |
// When the uuid of the user comes back from the database, we can |
| 18 |
// assume that the state object is ready. This is important to check |
| 19 |
// as the library may be "open" when loaded, but not ready. |
| 20 |
const userHasHydrated = useUserStoreReady() |
| 21 |
const taxonomiesReady = useTemplatesStore( |
| 22 |
(state) => Object.keys(state.taxonomyDefaultState).length > 0, |
| 23 |
) |
| 24 |
|
| 25 |
useEffect(() => { |
| 26 |
if (!open) return |
| 27 |
fetchTaxonomies().then(() => { |
| 28 |
useTemplatesStore.getState().setupDefaultTaxonomies() |
| 29 |
}) |
| 30 |
}, [open, fetchTaxonomies]) |
| 31 |
|
| 32 |
useEffect(() => { |
| 33 |
if (userHasHydrated && taxonomiesReady) { |
| 34 |
initTemplateData() |
| 35 |
setReady(true) |
| 36 |
} |
| 37 |
}, [userHasHydrated, taxonomiesReady, initTemplateData, setReady]) |
| 38 |
|
| 39 |
useEffect(() => { |
| 40 |
const search = new URLSearchParams(window.location.search) |
| 41 |
if (show || search.has('ext-open')) { |
| 42 |
setOpen(true) |
| 43 |
} |
| 44 |
}, [show, setOpen]) |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
if ( |
| 48 |
window?.location?.pathname?.includes('post-new.php') && |
| 49 |
window.extendifyData.openOnNewPage === '1' |
| 50 |
) { |
| 51 |
setOpen(true) |
| 52 |
} |
| 53 |
}, [setOpen]) |
| 54 |
|
| 55 |
useEffect(() => { |
| 56 |
GeneralApi.metaData().then((data) => { |
| 57 |
useGlobalStore.setState({ |
| 58 |
metaData: data, |
| 59 |
}) |
| 60 |
}) |
| 61 |
}, []) |
| 62 |
|
| 63 |
// Let the visibility to be controlled from outside the application |
| 64 |
// e.g. from the main button in the toolbar. |
| 65 |
useEffect(() => { |
| 66 |
window.addEventListener('extendify::open-library', showLibrary) |
| 67 |
window.addEventListener('extendify::close-library', hideLibrary) |
| 68 |
return () => { |
| 69 |
window.removeEventListener('extendify::open-library', showLibrary) |
| 70 |
window.removeEventListener('extendify::close-library', hideLibrary) |
| 71 |
} |
| 72 |
}, [hideLibrary, showLibrary]) |
| 73 |
|
| 74 |
return <MainWindow /> |
| 75 |
} |
| 76 |
|