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 / components / MainButtons.js

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

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