forked from Kenya-DK/quantframe-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMessage.tsx
More file actions
59 lines (55 loc) · 2.09 KB
/
ChatMessage.tsx
File metadata and controls
59 lines (55 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Alert, Avatar, Collapse, Group, Stack, Text, Tooltip } from "@mantine/core";
import { WFMarketTypes } from "$types/index";
import { WFMThumbnail } from "@api/index";
import classes from "./ChatMessage.module.css";
import { useEffect, useState } from "react";
import dayjs from "dayjs";
import calendar from "dayjs/plugin/calendar";
dayjs.extend(calendar);
import { decodeHtmlEntities } from "@utils/helper";
export type ChatMessageProps = {
user: WFMarketTypes.User | undefined;
sender: boolean;
msg: WFMarketTypes.ChatMessage;
};
export const ChatMessage = ({ user, msg, sender }: ChatMessageProps) => {
const position = sender ? "right" : "left";
const [msgDate, setMsgDate] = useState("");
const [opened, setOpen] = useState(false);
useEffect(() => {
const date = new Date(msg.send_date);
// If message is older than 48 hours show date
if (dayjs().diff(date, "h") > 48) setMsgDate(dayjs(date).format("MMMM D, YYYY h:mm A"));
else setMsgDate(dayjs(date).calendar());
return () => {};
}, [msg.send_date]);
return (
<Group align="flex-end" style={{ width: "100%" }} data-position={position} classNames={classes}>
<Stack p={0} gap={2} style={{ maxWidth: "80%" }} align={position === "right" ? "flex-end" : "flex-start"}>
<Group data-position={position} align="flex-end" gap="xs">
<Tooltip label={user?.ingame_name} position="right">
<Avatar src={WFMThumbnail(user?.avatar || "")} radius="xl" hidden={position === "right" ? true : false} />
</Tooltip>
<Stack p={0} gap={0} m={0}>
<Group data-position={position} gap={3} align="center">
<Alert
radius="lg"
py={8}
onClick={() => {
setOpen((o) => !o);
}}
>
{decodeHtmlEntities(msg.raw_message)}
</Alert>
</Group>
</Stack>
</Group>
<Collapse in={opened} px="xs">
<Text size="xs" c="dimmed">
{msgDate}
</Text>
</Collapse>
</Stack>
</Group>
);
};