PluginProbe
Extendify / 0.4.0
Extendify v0.4.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 / components / MainButtons.js

MainButtons.js in Extendify 0.4.0, at src/components/MainButtons.js

92 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Button } from '@wordpress/components'
2 import { useState, useEffect, useRef } from '@wordpress/element'
3 import { Icon } from '@wordpress/icons'
4 import { brandMark } from './icons'
5 import { __ } from '@wordpress/i18n'
6 import { openModal } from '../util/general'
7 import { useUserStore } from '../state/User'
8 import { useGlobalStore } from '../state/GlobalState'
9 import { General } from '../api/General'
10 import { NewImportsPopover } from './popovers/NewImportsPopover'
11
12 export const MainButton = () => {
13 const [showTooltip, setShowTooltip] = useState(false)
14 const once = useRef(false)
15 const buttonRef = useRef()
16 const loggedIn = useUserStore((state) => state.apiKey.length)
17 const hasImported = useUserStore((state) => state.imports > 0)
18 const open = useGlobalStore((state) => state.open)
19 const hasPendingNewImports = useUserStore(
20 (state) => state.allowedImports === 0,
21 )
22
23 const handleTooltipClose = async () => {
24 await General.ping('mb-tooltip-closed')
25 setShowTooltip(false)
26 // If they close the tooltip, we can set the allowed imports
27 // to -1 and when it opens it will fetch and update. Meanwhile,
28 // -1 will be ignored by the this component.
29 useUserStore.setState({
30 allowedImports: -1,
31 })
32 }
33
34 useEffect(() => {
35 if (open) {
36 setShowTooltip(false)
37 once.current = true
38 }
39 if (!loggedIn && hasPendingNewImports && hasImported) {
40 once.current || setShowTooltip(true)
41 once.current = true
42 }
43 }, [loggedIn, hasPendingNewImports, hasImported, open])
44
45 return (
46 <>
47 <Button
48 isPrimary
49 ref={buttonRef}
50 style={{ padding: '12px' }}
51 onClick={() => openModal('main-button')}
52 id="extendify-templates-inserter-btn"
53 icon={
54 <Icon
55 style={{ marginRight: '4px' }}
56 icon={brandMark}
57 size={24}
58 />
59 }>
60 {__('Library', 'extendify')}
61 </Button>
62
63 {showTooltip && (
64 <NewImportsPopover
65 anchorRef={buttonRef}
66 onClick={async () => {
67 await General.ping('mb-tooltip-pressed')
68 openModal('main-button-tooltip')
69 }}
70 onPressX={handleTooltipClose}
71 />
72 )}
73 </>
74 )
75 }
76
77 export const CtaButton = () => {
78 return (
79 <Button
80 id="extendify-cta-button"
81 style={{
82 margin: '1rem 1rem 0',
83 width: 'calc(100% - 2rem)',
84 justifyContent: ' center',
85 }}
86 onClick={() => openModal('patterns-cta')}
87 isSecondary>
88 {__('Discover patterns in Extendify Library', 'extendify')}
89 </Button>
90 )
91 }
92