| 1 |
import { useCategories } from '@library/hooks/useCategories'; |
| 2 |
import { useCacheStore } from '@library/state/cache'; |
| 3 |
import { useSiteSettingsStore } from '@library/state/site'; |
| 4 |
import { PanelBody, PanelRow, Spinner } from '@wordpress/components'; |
| 5 |
import { useEffect } from '@wordpress/element'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
import classNames from 'classnames'; |
| 8 |
|
| 9 |
export const CategoryControl = () => { |
| 10 |
const { category, setCategory } = useSiteSettingsStore(); |
| 11 |
const { data, isLoading, errorCount } = useCategories(); |
| 12 |
const { categories, setCategories } = useCacheStore(); |
| 13 |
|
| 14 |
useEffect(() => { |
| 15 |
if (isLoading || errorCount) return; |
| 16 |
setCategories(data); |
| 17 |
}, [data, isLoading, setCategories, errorCount]); |
| 18 |
|
| 19 |
useEffect(() => { |
| 20 |
if (!categories?.length || category) return; |
| 21 |
setCategory('all'); |
| 22 |
}, [category, setCategory, categories]); |
| 23 |
|
| 24 |
return ( |
| 25 |
<PanelBody |
| 26 |
title={__('Design Type', 'extendify-local')} |
| 27 |
className="ext-type-control p-0 border-0 [&_.components-panel\_\_body-title]:-mx-5 [&_.components-panel\_\_body-title]:mt-0 [&_.components-panel\_\_body-title]:mb-0.5" |
| 28 |
initialOpen={true} |
| 29 |
> |
| 30 |
<PanelRow> |
| 31 |
<CategoryList |
| 32 |
categories={categories} |
| 33 |
errorCount={errorCount} |
| 34 |
current={category} |
| 35 |
setCurrent={setCategory} |
| 36 |
/> |
| 37 |
</PanelRow> |
| 38 |
</PanelBody> |
| 39 |
); |
| 40 |
}; |
| 41 |
|
| 42 |
const CategoryList = ({ categories, errorCount, current, setCurrent }) => { |
| 43 |
const classes = (slug) => |
| 44 |
classNames( |
| 45 |
'text-sm w-full text-left rtl:text-right px-3 py-1 mb-0.5 block rounded', |
| 46 |
{ |
| 47 |
'bg-design-main text-design-text': current === slug, |
| 48 |
'bg-transparent text-gray-900 hover:bg-gray-100': current !== slug, |
| 49 |
}, |
| 50 |
); |
| 51 |
// If we have categories, return early no matter the error |
| 52 |
if (categories?.length) { |
| 53 |
return ( |
| 54 |
<ul className="m-0 -mt-1.5 max-h-half w-full overflow-y-auto rounded-b border border-gray-300 px-1 py-2"> |
| 55 |
<li className="m-0 p-0"> |
| 56 |
<button |
| 57 |
type="button" |
| 58 |
id="extendify-library-category-all" |
| 59 |
onClick={() => setCurrent('all')} |
| 60 |
className={classes('all')} |
| 61 |
> |
| 62 |
{__('All', 'extendify-local')} |
| 63 |
</button> |
| 64 |
</li> |
| 65 |
{categories.map(({ slug, id, name }) => { |
| 66 |
return ( |
| 67 |
<li key={id} className="m-0 p-0"> |
| 68 |
<button |
| 69 |
type="button" |
| 70 |
id={`extendify-library-category-${slug}`} |
| 71 |
onClick={() => setCurrent(slug)} |
| 72 |
className={classes(slug)} |
| 73 |
> |
| 74 |
{name} |
| 75 |
</button> |
| 76 |
</li> |
| 77 |
); |
| 78 |
})} |
| 79 |
</ul> |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
if (errorCount > 1) { |
| 84 |
return ( |
| 85 |
<div className="-mt-1 flex w-full flex-col items-center justify-center gap-2 border-t border-gray-300 p-2"> |
| 86 |
<span>{__('Retrying...', 'extendify-local')}</span> |
| 87 |
<Spinner /> |
| 88 |
</div> |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
return ( |
| 93 |
<div className="-mt-1 flex w-full justify-center border-t border-gray-300 p-2"> |
| 94 |
<span className="sr-only">{__('Fetching...', 'extendify-local')}</span> |
| 95 |
<Spinner /> |
| 96 |
</div> |
| 97 |
); |
| 98 |
}; |
| 99 |
|