ExtendifyLibrary.js (1437B)
1 import { useEffect, useCallback } from '@wordpress/element' 2 import { useGlobalStore } from './state/GlobalState' 3 import { useUserStore } from './state/User' 4 import MainWindow from './pages/parts/MainWindow' 5 6 export default function ExtendifyLibrary({ show = false }) { 7 const setOpen = useGlobalStore(state => state.setOpen) 8 const showLibrary = useCallback(() => setOpen(true), [setOpen]) 9 const hideLibrary = useCallback(() => { 10 setOpen(false) 11 }, [setOpen]) 12 13 useEffect(() => { 14 show && setOpen(true) 15 }, [show, setOpen]) 16 17 // Here for legacy reasons, we're checking if they have the old key stored 18 useEffect(() => { 19 if (window.localStorage.getItem('etfy_library__key')) { 20 useUserStore.setState({ 21 apiKey: 'any-key-will-work-during-beta', 22 }) 23 } 24 return () => window.localStorage.removeItem('etfy_library__key') 25 }, []) 26 27 // Let the visibility to be controlled from outside the application 28 useEffect(() => { 29 window.addEventListener('extendify-sdk::open-library', showLibrary) 30 window.addEventListener('extendify-sdk::close-library', hideLibrary) 31 return () => { 32 window.removeEventListener('extendify-sdk::open-library', showLibrary) 33 window.removeEventListener('extendify-sdk::close-library', hideLibrary) 34 } 35 }, [hideLibrary, showLibrary]) 36 37 return <MainWindow/> 38 }