| 1 |
import { Tooltip } from '@wordpress/components'; |
| 2 |
import { useMemo, useState } from '@wordpress/element'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
import { ErrorMessage } from '@agent/components/ErrorMessage'; |
| 5 |
import { useThemeVariations } from '@agent/hooks/useThemeVariations'; |
| 6 |
import { useVariationOverride } from '@agent/hooks/useVariationOverride'; |
| 7 |
|
| 8 |
export const SelectThemeVariation = ({ onConfirm, onCancel }) => { |
| 9 |
const [css, setCss] = useState(''); |
| 10 |
const [selected, setSelected] = useState(null); |
| 11 |
const [duotoneTheme, setDuotoneTheme] = useState(null); |
| 12 |
const { undoChange } = useVariationOverride({ css, duotoneTheme }); |
| 13 |
const { variations, isLoading } = useThemeVariations(); |
| 14 |
const noVariations = !variations || variations.length === 0; |
| 15 |
const shuffled = useMemo( |
| 16 |
() => (variations ? variations.sort(() => Math.random() - 0.5) : []), |
| 17 |
[variations], |
| 18 |
); |
| 19 |
|
| 20 |
const handleConfirm = () => { |
| 21 |
if (!selected) return; |
| 22 |
onConfirm({ |
| 23 |
data: { variation: variations.find((v) => v.title === selected) }, |
| 24 |
}); |
| 25 |
}; |
| 26 |
|
| 27 |
const handleCancel = () => { |
| 28 |
undoChange(); |
| 29 |
onCancel(); |
| 30 |
}; |
| 31 |
if (isLoading) { |
| 32 |
return ( |
| 33 |
<div className="min-h-24 p-2 text-center text-sm"> |
| 34 |
{__('Loading variations...', 'extendify-local')} |
| 35 |
</div> |
| 36 |
); |
| 37 |
} |
| 38 |
if (noVariations) { |
| 39 |
return ( |
| 40 |
<ErrorMessage> |
| 41 |
<div className="font-semibold"> |
| 42 |
{__('No variations found', 'extendify-local')} |
| 43 |
</div> |
| 44 |
<div className=""> |
| 45 |
{__( |
| 46 |
'We were unable to find any variations for your theme.', |
| 47 |
'extendify-local', |
| 48 |
)} |
| 49 |
</div> |
| 50 |
</ErrorMessage> |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return ( |
| 55 |
<div className="mb-4 ml-10 mr-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50 rtl:ml-2 rtl:mr-10"> |
| 56 |
<div className="rounded-lg border-b border-gray-300 bg-white"> |
| 57 |
<div className="grid grid-cols-2 gap-2 p-3"> |
| 58 |
{shuffled?.slice(0, 10)?.map(({ title, css, settings }) => ( |
| 59 |
<Tooltip key={title} text={title} placement="top"> |
| 60 |
<button |
| 61 |
style={{ backgroundColor: getColor(settings, 'background') }} |
| 62 |
type="button" |
| 63 |
className={`relative flex w-full items-center justify-center overflow-hidden rounded-lg border border-gray-300 bg-none p-2 text-center text-sm ${ |
| 64 |
selected === title ? 'ring-wp ring-design-main' : '' |
| 65 |
}`} |
| 66 |
onClick={() => { |
| 67 |
setSelected(title); |
| 68 |
setCss(css); |
| 69 |
setDuotoneTheme(settings?.color?.duotone?.theme); |
| 70 |
}}> |
| 71 |
<div className="flex max-w-fit items-center justify-center -space-x-4 rounded-lg rtl:space-x-reverse"> |
| 72 |
{getColors(settings)?.map((color, i) => ( |
| 73 |
<div |
| 74 |
key={title + color + i} |
| 75 |
style={{ backgroundColor: color }} |
| 76 |
className="size-6 flex-shrink-0 overflow-visible rounded-full border border-white md:size-7"></div> |
| 77 |
))} |
| 78 |
</div> |
| 79 |
</button> |
| 80 |
</Tooltip> |
| 81 |
))} |
| 82 |
</div> |
| 83 |
</div> |
| 84 |
<div className="flex justify-start gap-2 p-3"> |
| 85 |
<button |
| 86 |
type="button" |
| 87 |
className="w-full rounded border border-gray-300 bg-white p-2 text-sm text-gray-700" |
| 88 |
onClick={handleCancel}> |
| 89 |
{__('Cancel', 'extendify-local')} |
| 90 |
</button> |
| 91 |
<button |
| 92 |
type="button" |
| 93 |
className="w-full rounded border border-design-main bg-design-main p-2 text-sm text-white" |
| 94 |
disabled={!selected} |
| 95 |
onClick={handleConfirm}> |
| 96 |
{__('Save', 'extendify-local')} |
| 97 |
</button> |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
); |
| 101 |
}; |
| 102 |
|
| 103 |
const getColor = (settings, colorName) => { |
| 104 |
return settings?.color?.palette?.theme.find((item) => item.slug === colorName) |
| 105 |
?.color; |
| 106 |
}; |
| 107 |
|
| 108 |
const getColors = (settings) => { |
| 109 |
return settings?.color?.palette?.theme |
| 110 |
?.filter((item) => item.slug !== 'background') |
| 111 |
?.reduce((acc, item) => { |
| 112 |
acc.push(item.color); |
| 113 |
return acc; |
| 114 |
}, []); |
| 115 |
}; |
| 116 |
|