| 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 '@library/api/General' |
| 6 |
import { useTestGroup } from '@library/hooks/useTestGroup' |
| 7 |
import { useGlobalStore } from '@library/state/GlobalState' |
| 8 |
import { useUserStore } from '@library/state/User' |
| 9 |
import { openModal } from '@library/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-text2', ['A', 'B'], 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 __('Add template', 'extendify') |
| 43 |
case 'B': |
| 44 |
return __('Design Library', 'extendify') |
| 45 |
} |
| 46 |
} |
| 47 |
setLibraryButtonText(text()) |
| 48 |
}, [buttonText, uuid]) |
| 49 |
|
| 50 |
useEffect(() => { |
| 51 |
if (open) { |
| 52 |
setShowTooltip(false) |
| 53 |
once.current = true |
| 54 |
} |
| 55 |
if (!loggedIn && hasPendingNewImports && hasImported) { |
| 56 |
once.current || setShowTooltip(true) |
| 57 |
once.current = true |
| 58 |
} |
| 59 |
}, [loggedIn, hasPendingNewImports, hasImported, open]) |
| 60 |
|
| 61 |
return ( |
| 62 |
<> |
| 63 |
<MainButton buttonRef={buttonRef} text={libraryButtonText} /> |
| 64 |
{showTooltip && ( |
| 65 |
<NewImportsPopover |
| 66 |
anchorRef={buttonRef} |
| 67 |
onClick={async () => { |
| 68 |
await General.ping('mb-tooltip-pressed') |
| 69 |
openModal('main-button-tooltip') |
| 70 |
}} |
| 71 |
onPressX={handleTooltipClose} |
| 72 |
/> |
| 73 |
)} |
| 74 |
</> |
| 75 |
) |
| 76 |
} |
| 77 |
const MainButton = ({ buttonRef, text }) => { |
| 78 |
return ( |
| 79 |
<div className="extendify"> |
| 80 |
<Button |
| 81 |
isPrimary |
| 82 |
ref={buttonRef} |
| 83 |
className="h-8 xs:h-9 px-1 min-w-0 xs:pl-2 xs:pr-3 sm:ml-2" |
| 84 |
onClick={() => openModal('main-button')} |
| 85 |
id="extendify-templates-inserter-btn" |
| 86 |
icon={ |
| 87 |
<Icon |
| 88 |
icon={brandMark} |
| 89 |
size={24} |
| 90 |
style={{ marginRight: 0 }} |
| 91 |
/> |
| 92 |
}> |
| 93 |
<span className="hidden xs:inline ml-1">{text}</span> |
| 94 |
</Button> |
| 95 |
</div> |
| 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 |
|