import React, { useMemo } from 'react';
import { css } from '@rocket.chat/css-in-js';
import { Box, Scrollable, Button, Icon } from '@rocket.chat/fuselage';
import { useTranslation } from '../../contexts/TranslationContext';
import { useRoutePath } from '../../contexts/RouterContext';
export const createSidebarItems = (initialItems = []) => {
const items = initialItems;
let updateCb = () => {};
const itemsSubscription = {
subscribe: (cb) => {
updateCb = cb;
return () => {
updateCb = () => {};
};
},
getCurrentValue: () => items,
};
const registerSidebarItem = (item) => {
items.push(item);
updateCb();
};
const unregisterSidebarItem = (label) => {
const index = items.findIndex(({ i18nLabel }) => i18nLabel === label);
delete items[index];
updateCb();
};
return { registerSidebarItem, unregisterSidebarItem, itemsSubscription };
};
const Sidebar = ({ children, ...props }) =>
{children}
;
const Content = ({ children, ...props }) =>
{children}
;
const Header = ({ title, onClose, children, ...props }) =>
{(title || onClose) &&
{title && {title}}
{onClose && }
}
{children}
;
const GenericItem = ({ href, active, children, ...props }) =>
{children}
;
const NavigationItem = ({ permissionGranted, pathGroup, pathSection, icon, label, currentPath }) => {
const params = useMemo(() => ({ group: pathGroup }), [pathGroup]);
const path = useRoutePath(pathSection, params);
const isActive = path === currentPath || false;
if (permissionGranted && !permissionGranted()) { return null; }
return
{icon && }
{label}
;
};
const ItemsAssembler = ({ items, currentPath }) => {
const t = useTranslation();
return items.map(({
href,
pathSection,
i18nLabel,
name,
icon,
permissionGranted,
pathGroup,
}) => );
};
Sidebar.Content = Content;
Sidebar.Header = Header;
Sidebar.GenericItem = React.memo(GenericItem);
Sidebar.NavigationItem = React.memo(NavigationItem);
Sidebar.ItemsAssembler = React.memo(ItemsAssembler);
export default Sidebar;