The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/lib/createSidebarItems.ts

57 lines
1.5 KiB

import type { IconProps } from '@rocket.chat/fuselage';
import type { ReactElement } from 'react';
export type Item = {
i18nLabel: string;
href?: string;
icon?: IconProps['name'];
tag?: 'Alpha' | 'Beta';
permissionGranted?: () => boolean;
pathSection?: string;
pathGroup?: string;
name?: string;
externalUrl?: boolean;
badge?: () => ReactElement;
};
export type SidebarDivider = { divider: boolean; i18nLabel: string };
export type SidebarItem = Item | SidebarDivider;
export const isSidebarItem = (item: SidebarItem): item is Item => !('divider' in item);
export const createSidebarItems = (
initialItems: SidebarItem[] = [],
): {
registerSidebarItem: (item: SidebarItem) => void;
unregisterSidebarItem: (i18nLabel: SidebarItem['i18nLabel']) => void;
getSidebarItems: () => SidebarItem[];
subscribeToSidebarItems: (callback: () => void) => () => void;
} => {
const items = initialItems;
let updateCb: () => void = () => undefined;
const getSidebarItems = (): SidebarItem[] => items;
const subscribeToSidebarItems = (cb: () => void): (() => void) => {
updateCb = cb;
return (): void => {
updateCb = (): void => undefined;
};
};
const registerSidebarItem = (item: SidebarItem): void => {
items.push(item);
updateCb();
};
const unregisterSidebarItem = (i18nLabel: SidebarItem['i18nLabel']): void => {
const index = items.findIndex((item) => item.i18nLabel === i18nLabel);
delete items[index];
updateCb();
};
return {
registerSidebarItem,
unregisterSidebarItem,
getSidebarItems,
subscribeToSidebarItems,
};
};