import React, { ReactNode } from 'react';
import { IconSrc, Icons } from 'folds';
import { MatrixEvent } from 'matrix-js-sdk';
import { Trans } from 'react-i18next';
import { IMemberContent, Membership } from '../../types/matrix/room';
import { getMxIdLocalPart } from '../utils/matrix';
import { isMembershipChanged } from '../utils/room';
export type ParsedResult = {
icon: IconSrc;
body: ReactNode;
};
export type MemberEventParser = (mEvent: MatrixEvent) => ParsedResult;
const B = ;
export const useMemberEventParser = (): MemberEventParser => {
const parseMemberEvent: MemberEventParser = (mEvent) => {
const content = mEvent.getContent();
const prevContent = mEvent.getPrevContent() as IMemberContent;
const senderId = mEvent.getSender();
const userId = mEvent.getStateKey();
const reason = typeof content.reason === 'string' ? content.reason : undefined;
if (!senderId || !userId)
return {
icon: Icons.User,
body: ,
};
const senderName = getMxIdLocalPart(senderId);
const userName =
typeof content.displayname === 'string'
? content.displayname || getMxIdLocalPart(userId)
: getMxIdLocalPart(userId);
if (isMembershipChanged(mEvent)) {
if (content.membership === Membership.Invite) {
if (prevContent.membership === Membership.Knock) {
return {
icon: Icons.ArrowGoRightPlus,
body: (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
return {
icon: Icons.ArrowGoRightPlus,
body: (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
if (content.membership === Membership.Knock) {
return {
icon: Icons.ArrowGoRightPlus,
body: (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
if (content.membership === Membership.Join) {
return {
icon: Icons.ArrowGoRight,
body: (
),
};
}
if (content.membership === Membership.Leave) {
if (prevContent.membership === Membership.Invite) {
return {
icon: Icons.ArrowGoRightCross,
body:
senderId === userId ? (
<>
{reason ? ` ${reason}` : ''}
>
) : (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
if (prevContent.membership === Membership.Knock) {
return {
icon: Icons.ArrowGoRightCross,
body:
senderId === userId ? (
<>
{reason ? ` ${reason}` : ''}
>
) : (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
if (prevContent.membership === Membership.Ban) {
return {
icon: Icons.ArrowGoLeft,
body: (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
return {
icon: Icons.ArrowGoLeft,
body:
senderId === userId ? (
<>
{reason ? ` ${reason}` : ''}
>
) : (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
if (content.membership === Membership.Ban) {
return {
icon: Icons.ArrowGoLeft,
body: (
<>
{reason ? ` ${reason}` : ''}
>
),
};
}
}
if (content.displayname !== prevContent.displayname) {
const prevUserName =
typeof prevContent.displayname === 'string'
? prevContent.displayname || getMxIdLocalPart(userId)
: getMxIdLocalPart(userId);
return {
icon: Icons.Mention,
body:
typeof content.displayname === 'string' ? (
) : (
),
};
}
if (content.avatar_url !== prevContent.avatar_url) {
return {
icon: Icons.User,
body:
content.avatar_url && typeof content.avatar_url === 'string' ? (
) : (
),
};
}
return {
icon: Icons.User,
body: ,
};
};
return parseMemberEvent;
};