PluginProbe
Extendify / 0.6.0
Extendify v0.6.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.6.0, at src/components/MainButtons.js

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