Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions apps/meteor/app/ui-utils/client/lib/AccountBox.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { IUActionButtonWhen, IUIActionButton } from '@rocket.chat/apps-engine/definition/ui/IUIActionButtonDescriptor';
import { IUIActionButton, IUActionButtonWhen } from '@rocket.chat/apps-engine/definition/ui/IUIActionButtonDescriptor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
import { Meteor } from 'meteor/meteor';

import { SideNav } from './SideNav';
import { applyDropdownActionButtonFilters } from '../../../ui-message/client/actionButtons/lib/applyButtonFilters';

export interface IAccountBoxItem extends Omit<IUIActionButton, 'when'> {
export interface IAppAccountBoxItem extends IUIActionButton {
name: string;
icon?: string;
href?: string;
sideNav?: string;
isAppButtonItem?: boolean;
subItems?: [IAccountBoxItem];
subItems?: [IAppAccountBoxItem];
when?: Omit<IUActionButtonWhen, 'roomTypes' | 'messageActionContext'>;
}

type AccountBoxItem = {
name: string;
icon: string;
href: string;
sideNav?: string;
condition: () => boolean;
};

export const isAppAccountBoxItem = (item: IAppAccountBoxItem | AccountBoxItem): item is IAppAccountBoxItem => 'isAppButtonItem' in item;

export class AccountBoxBase {
private items = new ReactiveVar([]);
private items = new ReactiveVar<IAppAccountBoxItem[]>([]);

private status = 0;

Expand Down Expand Up @@ -48,25 +58,31 @@ export class AccountBoxBase {
this.status = 0;
}

public async addItem(newItem: IAccountBoxItem): Promise<void> {
public async addItem(newItem: IAppAccountBoxItem): Promise<void> {
Tracker.nonreactive(() => {
const actual = this.items.get();
actual.push(newItem as never);
actual.push(newItem);
this.items.set(actual);
});
}

public async deleteItem(item: IAccountBoxItem): Promise<void> {
public async deleteItem(item: IAppAccountBoxItem): Promise<void> {
Tracker.nonreactive(() => {
const actual = this.items.get();
const itemIndex = actual.findIndex((actualItem: IAccountBoxItem) => actualItem.appId === item.appId);
const itemIndex = actual.findIndex((actualItem: IAppAccountBoxItem) => actualItem.appId === item.appId);
actual.splice(itemIndex, 1);
this.items.set(actual);
});
}

public getItems(): IAccountBoxItem[] {
return this.items.get().filter((item: IAccountBoxItem) => applyDropdownActionButtonFilters(item));
public getItems(): (IAppAccountBoxItem | AccountBoxItem)[] {
return this.items.get().filter((item: IAppAccountBoxItem | AccountBoxItem) => {
if ('condition' in item) {
return item.condition();
}

return applyDropdownActionButtonFilters(item);
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/client/sidebar/header/UserDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, { ReactElement } from 'react';

import { triggerActionButtonAction } from '../../../app/ui-message/client/ActionManager';
import { AccountBox, SideNav } from '../../../app/ui-utils/client';
import { IAccountBoxItem } from '../../../app/ui-utils/client/lib/AccountBox';
import { IAppAccountBoxItem, isAppAccountBoxItem } from '../../../app/ui-utils/client/lib/AccountBox';
import { userStatus } from '../../../app/user-status/client';
import { callbacks } from '../../../lib/callbacks';
import MarkdownText from '../../components/MarkdownText';
Expand Down Expand Up @@ -105,7 +105,7 @@ const UserDropdown = ({ user, onClose }: UserDropdownProps): ReactElement => {

const accountBoxItems = useReactiveValue(getItems);

const appBoxItems = (): IAccountBoxItem[] => accountBoxItems.filter((item) => item.isAppButtonItem);
const appBoxItems = (): IAppAccountBoxItem[] => accountBoxItems.filter((item): item is IAppAccountBoxItem => isAppAccountBoxItem(item));

return (
<Box display='flex' flexDirection='column' w={!isMobile ? '244px' : undefined}>
Expand Down Expand Up @@ -176,7 +176,7 @@ const UserDropdown = ({ user, onClose }: UserDropdownProps): ReactElement => {
<Option.Divider />
{showAdmin && <Option icon={'customize'} label={t('Administration')} onClick={handleAdmin}></Option>}
{accountBoxItems
.filter((item) => !item.isAppButtonItem)
.filter((item) => !isAppAccountBoxItem(item))
.map((item, i) => {
const action = (): void => {
if (item.href) {
Expand Down