session-desktop/ts/components/session/LeftPaneSectionHeader.tsx

145 lines
3.3 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classNames from 'classnames';
import { SessionButton } from './SessionButton';
2020-05-07 05:46:36 +02:00
import { SessionIcon, SessionIconSize, SessionIconType } from './icon';
2020-03-29 22:38:43 +02:00
import {
NotificationCountSize,
SessionNotificationCount,
} from './SessionNotificationCount';
const Tab = ({
isSelected,
label,
onSelect,
type,
}: {
isSelected: boolean;
label: string;
onSelect?: (event: number) => void;
type: number;
}) => {
const handleClick = onSelect
? () => {
onSelect(type);
}
: undefined;
return (
<h1
className={classNames(
'module-left-pane__title',
isSelected ? 'active' : null
)}
onClick={handleClick}
role="button"
>
{label}
</h1>
);
};
interface Props {
onTabSelected: any;
selectedTab: number;
labels: Array<string>;
notificationCount?: number;
buttonLabel?: string;
2020-05-07 05:46:36 +02:00
buttonIcon?: SessionIconType;
buttonClicked?: any;
}
interface State {
selectedTab: number;
}
export class LeftPaneSectionHeader extends React.Component<Props, State> {
constructor(props: any) {
super(props);
this.state = { selectedTab: 0 };
}
public render() {
return this.renderTabs();
}
private renderTabs() {
const { selectedTab } = this.state;
const {
labels,
buttonLabel,
2020-05-07 05:46:36 +02:00
buttonIcon,
buttonClicked,
notificationCount,
} = this.props;
2020-05-07 05:46:36 +02:00
const hasButton = buttonLabel || buttonIcon;
const children = [];
//loop to create children
for (let i = 0; i < labels.length; i++) {
children.push(
<Tab
label={labels[i]}
type={i}
isSelected={selectedTab === i}
onSelect={this.handleTabSelect}
key={i}
/>
);
}
2020-05-07 05:46:36 +02:00
if (hasButton && !notificationCount) {
const buttonContent = buttonIcon ? (
<SessionIcon iconType={buttonIcon} iconSize={SessionIconSize.Small} />
) : (
buttonLabel
);
2020-05-07 05:46:36 +02:00
const button = (
<SessionButton onClick={buttonClicked} key="compose" disabled={false}>
{buttonContent}
</SessionButton>
);
children.push(button);
2020-02-12 05:50:23 +01:00
} else if (buttonLabel && notificationCount && notificationCount > 0) {
children.push(
<div className="contact-notification-section">
<SessionButton
text={buttonLabel}
onClick={buttonClicked}
key="compose"
disabled={false}
/>
2020-03-27 06:22:41 +01:00
<SessionNotificationCount
count={notificationCount}
2020-03-29 22:38:43 +02:00
size={NotificationCountSize.ON_HEADER}
2020-02-12 05:50:23 +01:00
onClick={this.props.buttonClicked}
key="notification-count" // we can only have one of those here
2020-03-27 06:22:41 +01:00
/>
2020-02-12 05:50:23 +01:00
</div>
);
} else if (notificationCount && notificationCount > 0) {
children.push(
2020-03-27 06:22:41 +01:00
<SessionNotificationCount
count={notificationCount}
2020-03-29 22:38:43 +02:00
size={NotificationCountSize.ON_HEADER}
2020-01-07 03:24:44 +01:00
onClick={this.props.buttonClicked}
key="notificationCount"
2020-03-27 06:22:41 +01:00
/>
);
}
2020-05-11 02:37:40 +02:00
// Create the parent and add the children
return <div className="module-left-pane__header">{children}</div>;
}
private readonly handleTabSelect = (tabType: number): void => {
this.setState({
selectedTab: tabType,
});
if (this.props.onTabSelected) {
this.props.onTabSelected(tabType);
}
};
}