| 1 |
import classnames from 'classnames' |
| 2 |
import { Icon } from '@wordpress/icons' |
| 3 |
import { __, sprintf } from '@wordpress/i18n' |
| 4 |
import { useEffect } from '@wordpress/element' |
| 5 |
import { alert, download } from './icons/' |
| 6 |
import { useUserStore } from '../state/User' |
| 7 |
import { User as UserApi } from '../api/User' |
| 8 |
import { growthArrow } from './icons' |
| 9 |
|
| 10 |
export const ImportCounter = () => { |
| 11 |
const remainingImports = useUserStore((state) => state.remainingImports) |
| 12 |
const allowedImports = useUserStore((state) => state.allowedImports) |
| 13 |
const status = remainingImports() > 0 ? 'has-imports' : 'no-imports' |
| 14 |
const backgroundColor = |
| 15 |
status === 'has-imports' |
| 16 |
? 'bg-extendify-main hover:bg-extendify-main-dark' |
| 17 |
: 'bg-extendify-alert' |
| 18 |
const icon = status === 'has-imports' ? download : alert |
| 19 |
|
| 20 |
useEffect(() => { |
| 21 |
if (!allowedImports) { |
| 22 |
const fallback = 5 |
| 23 |
UserApi.allowedImports() |
| 24 |
.then((allowedImports) => { |
| 25 |
allowedImports = /^[1-9]\d*$/.test(allowedImports) |
| 26 |
? allowedImports |
| 27 |
: fallback |
| 28 |
useUserStore.setState({ allowedImports }) |
| 29 |
}) |
| 30 |
.catch(() => |
| 31 |
useUserStore.setState({ allowedImports: fallback }), |
| 32 |
) |
| 33 |
} |
| 34 |
}, [allowedImports]) |
| 35 |
|
| 36 |
if (!allowedImports) { |
| 37 |
return null |
| 38 |
} |
| 39 |
|
| 40 |
return ( |
| 41 |
<a |
| 42 |
target="_blank" |
| 43 |
rel="noreferrer" |
| 44 |
className={classnames( |
| 45 |
backgroundColor, |
| 46 |
'hidden sm:flex w-full no-underline button-focus text-sm justify-between py-3 px-4 text-white rounded', |
| 47 |
)} |
| 48 |
href={`https://www.extendify.com/pricing/?utm_source=${encodeURIComponent( |
| 49 |
window.extendifyData.sdk_partner, |
| 50 |
)}&utm_medium=library&utm_campaign=import-counter&utm_content=get-more&utm_term=${status}`}> |
| 51 |
<div className="flex items-center space-x-2 no-underline text-xs"> |
| 52 |
<Icon icon={icon} size={14} /> |
| 53 |
<span> |
| 54 |
{sprintf(__('%s Imports', 'extendify'), remainingImports())} |
| 55 |
</span> |
| 56 |
</div> |
| 57 |
<span className="text-white text-sm no-underline font-medium outline-none flex items-center"> |
| 58 |
{__('Get more', 'extendify')} |
| 59 |
<Icon icon={growthArrow} size={24} className="-mr-1.5" /> |
| 60 |
</span> |
| 61 |
</a> |
| 62 |
) |
| 63 |
} |
| 64 |
|