import { Avatar, Box, Dialog, DialogActions, DialogContent, DialogTitle, Grid2 as Grid, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, Theme, Typography } from "@mui/material"; import { MoreVert, Person, Delete as DeleteIcon, } from "@mui/icons-material"; import moment from "moment-timezone"; import { useGlobalState, useNoteAPI, useSnackbar } from "providers"; import React, { useState } from "react"; import { pond } from "protobuf-ts/pond"; import { red } from "@mui/material/colors"; import { makeStyles } from "@mui/styles"; import CancelSubmit from "common/CancelSubmit"; import { getThemeType } from "theme/themeType"; interface Props { index: number; chat: pond.Chat; attachments?: pond.FileReference[]; removeNoteMethod: (index: number) => void; } const useStyles = makeStyles((theme: Theme) => ({ red: { color: red["500"] }, oddGrid: { backgroundColor: theme.palette.background.paper, padding: theme.spacing(1) }, evenGrid: { backgroundColor: getThemeType() === "light" ? theme.palette.background.default : undefined, padding: theme.spacing(1) }, userAvatar: { width: 36, height: 36, marginLeft: theme.spacing(1), marginRight: theme.spacing(2), } })) /** * Takes in a note and renders it * handles deleting of the note * @param props Note object */ export default function ChatMessage(props: Props) { const classes = useStyles(); const [{ user }] = useGlobalState(); const noteAPI = useNoteAPI(); const { openSnack } = useSnackbar(); const [dialog, setDialog] = useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const avatar = props.chat.profile?.avatar const name = props.chat.profile?.name const timestamp = props.chat.note?.settings?.timestamp ? props.chat.note?.settings?.timestamp : undefined const generatePermissions = () => { let perms: pond.Permission[] = [] if (Array.isArray(props.chat.permissions)) { perms = props.chat.permissions.map((perm: unknown) => { const mappedPerm = pond.Permission[perm as keyof typeof pond.Permission]; return mappedPerm; // Return null for invalid keys }).filter(Boolean); // Optionally filter out any null values } else { console.error("data.permissions is not an array or does not exist"); perms = []; } return perms } const permissions = generatePermissions() const deleteMessage = () => { if (!props.chat?.note?.settings) return setAnchorEl(null); noteAPI .removeNote(props.chat.note.settings.key) .then(_resp => { openSnack("Message Deleted"); props.removeNoteMethod(props.index); }) .catch(_err => { openSnack("Failed to delete Message"); }); closeDialog(); }; const openDialog = () => { setDialog(true); }; const closeDialog = () => { setDialog(false); }; const messageMenu = () => { return ( setAnchorEl(null)} keepMounted > Delete Message ); }; const deleteDialog = () => { return ( Delete Message? Are you sure you wish to delete this message ); }; return ( {avatar ? ( {!avatar && name} ) : ( )} {name} {moment.unix(timestamp ? timestamp/1000: 0) .tz(user.settings.timezone ? user.settings.timezone : "Canada/Central") .format("MMMM Do YYYY, h:mm:ss a")} {props.chat.note?.settings?.content} {/*load files and display attached images below the message */} {/* delete button */} {permissions.includes(pond.Permission.PERMISSION_WRITE) ? ( ) => setAnchorEl(event.currentTarget) }> ) : ( // // // // )} {deleteDialog()} {messageMenu()} ); }