import { pluralize } from '../../../util';
import { FragmentShip } from './fragment-lib';
import { Text, Flex, BoxProps, Icon } from '../../../general';
type InlineStatusProps = {
id: string;
text: string;
link?: string;
buttons?: any[];
} & BoxProps;
const createdChatRegex = /created the chat/;
const joinedChatRegex = /joined the chat/;
const leftChatRegex = /left the chat/;
const parseCreatedJoinedLeftChat = (id: string, text: string) => {
let patp: string = '';
let status: string = '';
if (createdChatRegex.test(text)) {
status = 'created the chat';
patp = text.replace(status, '').trim();
} else if (joinedChatRegex.test(text)) {
status = 'joined the chat';
patp = text.replace(status, '').trim();
} else if (leftChatRegex.test(text)) {
status = 'left the chat';
patp = text.replace(status, '').trim();
}
if (!patp) return null;
return (
{/* TODO popup passport card on click */}
{patp}
{status}
);
};
const disappearingMessageRegex = /set disappearing messages to/;
const disappearingMessageOffRegex = /Messages now last forever/;
const dayRegex = /~d\d+/;
const hourRegex = /h\d+/;
const minRegex = /~m\d+/;
const parseDisappearingMessage = (id: string, text: string) => {
if (disappearingMessageRegex.test(text)) {
// check if the format is ~d30 or ~h24 or ~m30
const days = text.match(dayRegex)?.[0].replace('~d', '');
const hours = text.match(hourRegex)?.[0].replace('~h', '');
const minutes = text.match(minRegex)?.[0].replace('~m', '');
let timeString = '';
if (days) {
timeString = `${days} ${pluralize('day', parseInt(days))}`;
}
if (hours) {
timeString = `${hours} ${pluralize('hour', parseInt(hours))}`;
}
if (minutes) {
timeString = `${minutes} ${pluralize('minute', parseInt(minutes))}`;
}
return (
Disappearing messages set to {timeString}
);
} else if (disappearingMessageOffRegex.test(text)) {
return (
Messages now last forever
);
}
return null;
};
export const InlineStatus = ({ id, text }: InlineStatusProps) => {
let innerContent = (
{text}
);
innerContent = parseDisappearingMessage(id, text) || innerContent;
innerContent = parseCreatedJoinedLeftChat(id, text) || innerContent;
return (
{innerContent}
);
};
// TODO regex parser for wallet txn
// const walletStatusRegex = /%realm-wallet/;
// export function shortenedTxn(address: string) {
// return `${address.slice(0, 6)}...${address.slice(-4)}`;
// }
// const parseWalletTxn = (text: string) => {
// if (walletStatusRegex.test(text)) {
// // extra all the data from the text between the brackets
// // %realm-wallet [from=~novdus-fidlys-dozzod-hostyv] [to=~sicnum-rocwen] [tx=0x3230549cfc72c1a9e674f8a296ba94c1dea3845928e0f02f0295f6631a5aa9ec] [link=https://goerli.etherscan.io/tx/0x3230549cfc72c1a9e674f8a296ba94c1dea3845928e0f02f0295f6631a5aa9ec]
// const from = text.match(/\[from=~[a-z0-9-]+\]/)?.[0].slice(6, -1);
// const to = text.match(/\[to=~[a-z0-9-]+\]/)?.[0].slice(4, -1);
// const tx = text.match(/\[tx=0x[a-z0-9]+\]/)?.[0].slice(4, -1);
// const link = text
// .match(/\[link=https:\/\/[a-z0-9.\/]+\]/)?.[0]
// .slice(6, -1);
// innerContent = (
//
//
//
// New txn {from} to {to} in {shortenedTxn(tx?.toString())}{' '}
//
// {
// link && window.open(link.toString(), '_blank');
// }}
// />
//
// );
// }
// }