Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions packages/react/src/views/AttachmentPreview/AttachmentPreview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React, { useContext, useState, useRef, useEffect } from 'react';
import { css } from '@emotion/react';
import { Box, Icon, Button, Input, Modal } from '@embeddedchat/ui-elements';
import {
Box,
Icon,
Button,
Input,
Modal,
useTheme,
} from '@embeddedchat/ui-elements';
import useAttachmentWindowStore from '../../store/attachmentwindow';
import CheckPreviewType from './CheckPreviewType';
import RCContext from '../../context/RCInstance';
import { useMessageStore, useMemberStore } from '../../store';
import useSettingsStore from '../../store/settingsStore';
import getAttachmentPreviewStyles from './AttachmentPreview.styles';
import { parseEmoji } from '../../lib/emoji';
import MembersList from '../Mentions/MembersList';
Expand All @@ -13,6 +21,7 @@ import useSearchMentionUser from '../../hooks/useSearchMentionUser';

const AttachmentPreview = () => {
const { RCInstance, ECOptions } = useContext(RCContext);
const { theme } = useTheme();
const styles = getAttachmentPreviewStyles();

const toggle = useAttachmentWindowStore((state) => state.toggle);
Expand All @@ -25,6 +34,7 @@ const AttachmentPreview = () => {
const [mentionIndex, setMentionIndex] = useState(-1);
const [startReadMentionUser, setStartReadMentionUser] = useState(false);
const [keyPressed, setKeyPressed] = useState(null);
const [isMsgLong, setIsMsgLong] = useState(false);

const [fileName, setFileName] = useState(data?.name);

Expand All @@ -46,12 +56,6 @@ const AttachmentPreview = () => {
setShowMembersList
);

const handleFileDescription = (e) => {
const description = e.target.value;
messageRef.current.value = parseEmoji(description);
searchMentionUser(description);
};

const submit = async () => {
setIsPending(true);
await RCInstance.sendAttachment(
Expand All @@ -67,6 +71,27 @@ const AttachmentPreview = () => {
}
};

const msgMaxLength = useSettingsStore((state) => state.messageLimit);
const descAboveMaxLengthMsg = `Cannot upload file, description is over the ${msgMaxLength} character limit`;

const checkIfMsgLong = (description) => {
if (description.length > msgMaxLength) {
setIsMsgLong(true);
return;
}
submit();
};

const handleFileDescription = (e) => {
const description = e.target.value;
messageRef.current.value = parseEmoji(description);
if (isMsgLong) {
if (description.length <= msgMaxLength) {
setIsMsgLong(false);
}
}
searchMentionUser(description);
};
useEffect(() => {
const keyHandler = (e) => {
if (e.key === 'Enter') {
Expand Down Expand Up @@ -170,10 +195,29 @@ const AttachmentPreview = () => {
onChange={(e) => {
handleFileDescription(e);
}}
css={styles.input}
css={css`
${styles.input};
border-color: ${isMsgLong
? theme.colors.destructive
: null};
color: ${isMsgLong ? theme.colors.destructive : null};
`}
placeholder="Description"
ref={messageRef}
/>
<Box>
{isMsgLong && (
<Box
css={css`
color: ${theme.colors.destructive};
font-size: 12px;
margin-top: 5px;
`}
>
{descAboveMaxLengthMsg}
</Box>
)}
</Box>
</Box>
</Box>
</Box>
Expand All @@ -191,7 +235,7 @@ const AttachmentPreview = () => {
<Button
disabled={isPending}
onClick={() => {
submit();
checkIfMsgLong(messageRef.current.value);
}}
>
{isPending ? 'Sending...' : 'Send'}
Expand Down
Loading