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

118 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-02-04 08:07:31 +01:00
import React from 'react';
import classNames from 'classnames';
import { Avatar, AvatarSize } from '../Avatar';
import { SessionIcon, SessionIconSize, SessionIconType } from './icon';
2020-07-21 06:43:46 +02:00
import { Constants } from '../../session';
2021-01-21 07:47:26 +01:00
import { DefaultTheme } from 'styled-components';
import { PubKey } from '../../session/types';
import autoBind from 'auto-bind';
2020-02-04 08:07:31 +01:00
export interface ContactType {
id: string;
selected: boolean;
authorProfileName: string;
authorPhoneNumber: string;
authorName: string;
authorAvatarPath: string;
checkmarked: boolean;
existingMember: boolean;
2020-02-04 08:07:31 +01:00
}
interface Props {
member: ContactType;
index: number; // index in the list
isSelected: boolean;
// this bool is used to make a zombie appear with less opacity than a normal member
isZombie?: boolean;
onSelect?: any;
onUnselect?: any;
theme: DefaultTheme;
2020-02-04 08:07:31 +01:00
}
class SessionMemberListItemInner extends React.Component<Props> {
2020-02-04 08:07:31 +01:00
public static defaultProps = {
isSelected: false,
};
constructor(props: any) {
super(props);
autoBind(this);
2020-02-04 08:07:31 +01:00
}
public render() {
const { isSelected, member, isZombie } = this.props;
2020-02-04 08:07:31 +01:00
2021-04-22 10:03:58 +02:00
const name = member.authorProfileName || PubKey.shorten(member.authorPhoneNumber);
2020-02-04 08:07:31 +01:00
return (
<div
className={classNames(
`session-member-item-${this.props.index}`,
'session-member-item',
isSelected && 'selected',
isZombie && 'zombie'
)}
onClick={this.handleSelectionAction}
role="button"
>
<div className="session-member-item__info">
2021-04-22 10:03:58 +02:00
<span className="session-member-item__avatar">{this.renderAvatar()}</span>
<span className="session-member-item__name">{name}</span>
2020-02-04 08:07:31 +01:00
</div>
2021-04-22 10:03:58 +02:00
<span className={classNames('session-member-item__checkmark', isSelected && 'selected')}>
<SessionIcon
iconType={SessionIconType.Check}
iconSize={SessionIconSize.Medium}
2020-07-21 06:43:46 +02:00
iconColor={Constants.UI.COLORS.GREEN}
theme={this.props.theme}
/>
</span>
</div>
2020-02-04 08:07:31 +01:00
);
}
private renderAvatar() {
2020-09-15 07:07:22 +02:00
const {
authorAvatarPath,
authorName,
authorPhoneNumber,
authorProfileName,
} = this.props.member;
const userName = authorName || authorProfileName || authorPhoneNumber;
2020-02-04 08:07:31 +01:00
return (
<Avatar
2020-09-15 07:07:22 +02:00
avatarPath={authorAvatarPath}
name={userName}
size={AvatarSize.XS}
2020-09-15 07:07:22 +02:00
pubkey={authorPhoneNumber}
2020-02-04 08:07:31 +01:00
/>
);
}
private handleSelectionAction() {
if (this.props.isSelected) {
this.unselectMember();
return;
2020-02-04 08:07:31 +01:00
}
this.selectMember();
}
private selectMember() {
if (this.props.onSelect) {
this.props.onSelect(this.props.member);
}
2020-02-04 08:07:31 +01:00
}
private unselectMember() {
if (this.props.onUnselect) {
this.props.onUnselect(this.props.member);
2020-02-04 08:07:31 +01:00
}
}
2020-02-04 08:07:31 +01:00
}
export const SessionMemberListItem = SessionMemberListItemInner;