PluginProbe
Extendify / 1.16.1
Extendify v1.16.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 0.7.0 All 126 releases
extendify / src / Library / components / sidebar / CategoryControl.jsx

CategoryControl.jsx in Extendify 1.16.1, at src/Library/components/sidebar/CategoryControl.jsx

110 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { PanelBody, PanelRow, Spinner } from '@wordpress/components';
2 import { useEffect } from '@wordpress/element';
3 import { __ } from '@wordpress/i18n';
4 import classNames from 'classnames';
5 import { useCategories } from '@library/hooks/useCategories';
6 import { useCacheStore } from '@library/state/cache';
7 import { useSiteSettingsStore } from '@library/state/site';
8
9 export const CategoryControl = () => {
10 const { category, siteType, 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 // Don't steal focus if no site type is selected
21 const focus = (slug) =>
22 siteType?.name &&
23 document.querySelector(`#extendify-library-category-${slug}`)?.focus();
24 // Wait for categories to be available
25 if (!categories?.length) return;
26 if (category) {
27 // If category is all, focus all
28 if (category === 'all') return focus('all');
29 // If category is already set, make sure it's a valid category
30 if (categories?.find(({ slug }) => slug === category)) {
31 return focus(category);
32 }
33 }
34 setCategory('all');
35 focus('all');
36 }, [category, setCategory, categories, siteType?.name]);
37
38 return (
39 <PanelBody
40 title={__('Design Type', 'extendify-local')}
41 className="ext-type-control p-0"
42 initialOpen={!!siteType?.name}>
43 <PanelRow>
44 <CategoryList
45 categories={categories}
46 errorCount={errorCount}
47 current={category}
48 setCurrent={setCategory}
49 />
50 </PanelRow>
51 </PanelBody>
52 );
53 };
54
55 const CategoryList = ({ categories, errorCount, current, setCurrent }) => {
56 const classes = (slug) =>
57 classNames(
58 'text-sm w-full text-left rtl:text-right px-3 py-1 mb-0.5 block cursor-pointer rounded',
59 {
60 'bg-design-main text-design-text': current === slug,
61 'bg-transparent text-gray-900 hover:bg-gray-100': current !== slug,
62 },
63 );
64 // If we have categories, return early no matter the error
65 if (categories?.length) {
66 return (
67 <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">
68 <li className="m-0 p-0">
69 <button
70 type="button"
71 id="extendify-library-category-all"
72 onClick={() => setCurrent('all')}
73 className={classes('all')}>
74 {__('All', 'extendify-local')}
75 </button>
76 </li>
77 {categories.map(({ slug, id, name }) => {
78 return (
79 <li key={id} className="m-0 p-0">
80 <button
81 type="button"
82 id={`extendify-library-category-${slug}`}
83 onClick={() => setCurrent(slug)}
84 className={classes(slug)}>
85 {name}
86 </button>
87 </li>
88 );
89 })}
90 </ul>
91 );
92 }
93
94 if (errorCount > 1) {
95 return (
96 <div className="-mt-1 flex w-full flex-col items-center justify-center gap-2 border-t border-gray-300 p-2">
97 <span>{__('Retrying...', 'extendify-local')}</span>
98 <Spinner />
99 </div>
100 );
101 }
102
103 return (
104 <div className="-mt-1 flex w-full justify-center border-t border-gray-300 p-2">
105 <span className="sr-only">{__('Fetching...', 'extendify-local')}</span>
106 <Spinner />
107 </div>
108 );
109 };
110