| 1 |
/** |
| 2 |
* WordPress Dependencies |
| 3 |
*/ |
| 4 |
import { isEmpty } from "lodash"; |
| 5 |
import { __ } from "@wordpress/i18n"; |
| 6 |
import { useEffect } from "react"; |
| 7 |
import { tablebergIcons } from "./icons"; |
| 8 |
import { |
| 9 |
SearchControl, |
| 10 |
MenuGroup, |
| 11 |
MenuItem, |
| 12 |
PanelBody, |
| 13 |
} from "@wordpress/components"; |
| 14 |
|
| 15 |
export interface IconsLibrarySidebarProps { |
| 16 |
subCategoryFilter: string; |
| 17 |
search: string; |
| 18 |
setSubCategoryFilter: (subCategoryFilter: string) => void; |
| 19 |
setSearch: (search: string) => void; |
| 20 |
mainCategoryFilter: string; |
| 21 |
setMainCategoryFilter: (mainCategoryFilter: string) => void; |
| 22 |
} |
| 23 |
|
| 24 |
function Sidebar(props: IconsLibrarySidebarProps) { |
| 25 |
const { |
| 26 |
search, |
| 27 |
setSearch, |
| 28 |
subCategoryFilter, |
| 29 |
setSubCategoryFilter, |
| 30 |
setMainCategoryFilter, |
| 31 |
} = props; |
| 32 |
|
| 33 |
const preparedIconPacks = tablebergIcons.map(iconPack => { |
| 34 |
const categories = iconPack?.categories; |
| 35 |
const allCategories: Array<{ |
| 36 |
name: string; |
| 37 |
title: string; |
| 38 |
count: number; |
| 39 |
}> = categories?.map(category => { |
| 40 |
const categoryName = category?.name; |
| 41 |
const categoryIcons = iconPack?.icons.filter(icon => { |
| 42 |
return icon?.categories?.includes(categoryName); |
| 43 |
}); |
| 44 |
return { ...category, count: categoryIcons.length }; |
| 45 |
}); |
| 46 |
allCategories.unshift({ |
| 47 |
name: "all-" + iconPack?.type, |
| 48 |
title: __("All", "tableberg"), |
| 49 |
count: iconPack?.icons.length, |
| 50 |
}); |
| 51 |
return { ...iconPack, categories: allCategories }; |
| 52 |
}); |
| 53 |
|
| 54 |
useEffect(() => { |
| 55 |
setSubCategoryFilter(preparedIconPacks[0]?.categories?.[0]?.name); |
| 56 |
}, []); |
| 57 |
|
| 58 |
return ( |
| 59 |
<div className="tableberg_icons_library_sidebar"> |
| 60 |
<SearchControl |
| 61 |
value={search} |
| 62 |
onChange={newValue => { |
| 63 |
setSearch(newValue); |
| 64 |
}} |
| 65 |
placeholder={__("Search Icon", "tableberg")} |
| 66 |
/> |
| 67 |
|
| 68 |
{!isEmpty(preparedIconPacks) && ( |
| 69 |
<MenuGroup className="tableberg_icons_library_sidebar_item_group"> |
| 70 |
{preparedIconPacks.map((iconPack, index) => { |
| 71 |
return ( |
| 72 |
<PanelBody |
| 73 |
title={iconPack?.title} |
| 74 |
initialOpen={index === 0} |
| 75 |
> |
| 76 |
{iconPack?.categories.map(category => { |
| 77 |
return ( |
| 78 |
<MenuItem |
| 79 |
key={category?.name} |
| 80 |
className="tableberg_icons_library_sidebar_item" |
| 81 |
// @ts-ignore |
| 82 |
isPressed={ |
| 83 |
subCategoryFilter === |
| 84 |
category?.name |
| 85 |
} |
| 86 |
onClick={() => { |
| 87 |
setSubCategoryFilter( |
| 88 |
category?.name |
| 89 |
); |
| 90 |
setMainCategoryFilter( |
| 91 |
iconPack?.type |
| 92 |
); |
| 93 |
}} |
| 94 |
> |
| 95 |
<span>{category?.title}</span> |
| 96 |
<span>{category?.count}</span> |
| 97 |
</MenuItem> |
| 98 |
); |
| 99 |
})} |
| 100 |
</PanelBody> |
| 101 |
); |
| 102 |
})} |
| 103 |
</MenuGroup> |
| 104 |
)} |
| 105 |
</div> |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
export default Sidebar; |
| 110 |
|