2022-09-12 13:46:19 -07:00
|
|
|
import classNames from 'clsx';
|
2022-10-17 08:50:33 -07:00
|
|
|
import React, { MutableRefObject, useEffect, useState } from 'react';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-10-17 09:23:03 -07:00
|
|
|
import { Stack } from 'soapbox/components/ui';
|
|
|
|
// import UploadProgress from 'soapbox/components/upload-progress';
|
|
|
|
// import UploadButton from 'soapbox/features/compose/components/upload_button';
|
2022-11-09 09:24:44 -08:00
|
|
|
import { IChat, useChatActions } from 'soapbox/queries/chats';
|
2022-10-17 09:23:03 -07:00
|
|
|
// import { truncateFilename } from 'soapbox/utils/media';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-10-04 16:00:00 -07:00
|
|
|
import ChatComposer from './chat-composer';
|
2022-09-12 13:46:19 -07:00
|
|
|
import ChatMessageList from './chat-message-list';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-10-17 09:23:03 -07:00
|
|
|
// const fileKeyGen = (): number => Math.floor((Math.random() * 0x10000));
|
2022-09-12 13:46:19 -07:00
|
|
|
|
|
|
|
interface ChatInterface {
|
2022-08-18 09:52:04 -07:00
|
|
|
chat: IChat,
|
2022-10-17 08:50:33 -07:00
|
|
|
inputRef?: MutableRefObject<HTMLTextAreaElement | null>,
|
2022-09-12 13:46:19 -07:00
|
|
|
className?: string,
|
2022-04-12 09:52:56 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:19 -07:00
|
|
|
/**
|
|
|
|
* Chat UI with just the messages and textarea.
|
|
|
|
* Reused between floating desktop chats and fullscreen/mobile chats.
|
|
|
|
*/
|
2022-11-03 10:55:33 -07:00
|
|
|
const Chat: React.FC<ChatInterface> = ({ chat, inputRef, className }) => {
|
2022-09-28 13:20:59 -07:00
|
|
|
const { createChatMessage, acceptChat } = useChatActions(chat.id);
|
2022-09-12 13:46:19 -07:00
|
|
|
|
|
|
|
const [content, setContent] = useState<string>('');
|
|
|
|
const [attachment, setAttachment] = useState<any>(undefined);
|
2022-10-17 09:23:03 -07:00
|
|
|
// const [isUploading, setIsUploading] = useState(false);
|
|
|
|
// const [uploadProgress, setUploadProgress] = useState(0);
|
|
|
|
// const [resetFileKey, setResetFileKey] = useState<number>(fileKeyGen());
|
2022-09-12 13:46:19 -07:00
|
|
|
const [hasErrorSubmittingMessage, setErrorSubmittingMessage] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const isSubmitDisabled = content.length === 0 && !attachment;
|
|
|
|
|
2022-11-09 09:24:44 -08:00
|
|
|
const submitMessage = () => {
|
|
|
|
createChatMessage.mutate({ chatId: chat.id, content }, {
|
|
|
|
onSuccess: () => {
|
|
|
|
setErrorSubmittingMessage(false);
|
|
|
|
},
|
2022-11-11 05:01:17 -08:00
|
|
|
onError: (error, _variables, context: any) => {
|
|
|
|
console.log(context);
|
|
|
|
|
|
|
|
setContent(context.prevContent as string);
|
|
|
|
setErrorSubmittingMessage(true);
|
|
|
|
},
|
2022-11-09 09:24:44 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
clearState();
|
|
|
|
};
|
2022-09-12 13:46:19 -07:00
|
|
|
|
|
|
|
const clearState = () => {
|
|
|
|
setContent('');
|
|
|
|
setAttachment(undefined);
|
2022-10-17 09:23:03 -07:00
|
|
|
// setIsUploading(false);
|
|
|
|
// setUploadProgress(0);
|
|
|
|
// setResetFileKey(fileKeyGen());
|
|
|
|
// setErrorSubmittingMessage(false);
|
2022-09-12 13:46:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const sendMessage = () => {
|
2022-11-09 09:24:44 -08:00
|
|
|
if (!isSubmitDisabled && !createChatMessage.isLoading) {
|
|
|
|
submitMessage();
|
2022-10-17 09:23:03 -07:00
|
|
|
|
2022-09-12 13:46:19 -07:00
|
|
|
if (!chat.accepted) {
|
|
|
|
acceptChat.mutate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertLine = () => setContent(content + '\n');
|
|
|
|
|
|
|
|
const handleKeyDown: React.KeyboardEventHandler = (event) => {
|
|
|
|
markRead();
|
|
|
|
|
|
|
|
if (event.key === 'Enter' && event.shiftKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
insertLine();
|
|
|
|
} else if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
sendMessage();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleContentChange: React.ChangeEventHandler<HTMLTextAreaElement> = (event) => {
|
|
|
|
setContent(event.target.value);
|
|
|
|
};
|
|
|
|
|
2022-10-17 09:23:03 -07:00
|
|
|
// const handlePaste: React.ClipboardEventHandler<HTMLTextAreaElement> = (e) => {
|
|
|
|
// if (isSubmitDisabled && e.clipboardData && e.clipboardData.files.length === 1) {
|
|
|
|
// handleFiles(e.clipboardData.files);
|
|
|
|
// }
|
|
|
|
// };
|
2022-09-12 13:46:19 -07:00
|
|
|
|
|
|
|
const markRead = () => {
|
|
|
|
// markAsRead.mutate();
|
|
|
|
// dispatch(markChatRead(chatId));
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseOver = () => markRead();
|
|
|
|
|
2022-10-17 09:23:03 -07:00
|
|
|
// const handleRemoveFile = () => {
|
|
|
|
// setAttachment(undefined);
|
|
|
|
// setResetFileKey(fileKeyGen());
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const onUploadProgress = (e: ProgressEvent) => {
|
|
|
|
// const { loaded, total } = e;
|
|
|
|
// setUploadProgress(loaded / total);
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const handleFiles = (files: FileList) => {
|
|
|
|
// setIsUploading(true);
|
|
|
|
|
|
|
|
// const data = new FormData();
|
|
|
|
// data.append('file', files[0]);
|
|
|
|
|
|
|
|
// dispatch(uploadMedia(data, onUploadProgress)).then((response: any) => {
|
|
|
|
// setAttachment(response.data);
|
|
|
|
// setIsUploading(false);
|
|
|
|
// }).catch(() => {
|
|
|
|
// setIsUploading(false);
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const renderAttachment = () => {
|
|
|
|
// if (!attachment) return null;
|
|
|
|
|
|
|
|
// return (
|
|
|
|
// <div className='chat-box__attachment'>
|
|
|
|
// <div className='chat-box__filename'>
|
|
|
|
// {truncateFilename(attachment.preview_url, 20)}
|
|
|
|
// </div>
|
|
|
|
// <div className='chat-box__remove-attachment'>
|
|
|
|
// <IconButton
|
|
|
|
// src={require('@tabler/icons/x.svg')}
|
|
|
|
// onClick={handleRemoveFile}
|
|
|
|
// />
|
|
|
|
// </div>
|
|
|
|
// </div>
|
|
|
|
// );
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const renderActionButton = () => {
|
|
|
|
// return canSubmit() ? (
|
|
|
|
// <IconButton
|
|
|
|
// src={require('@tabler/icons/send.svg')}
|
|
|
|
// title={intl.formatMessage(messages.send)}
|
|
|
|
// onClick={sendMessage}
|
|
|
|
// />
|
|
|
|
// ) : (
|
|
|
|
// <UploadButton onSelectFile={handleFiles} resetFileKey={resetFileKey} />
|
|
|
|
// );
|
|
|
|
// };
|
2022-09-12 13:46:19 -07:00
|
|
|
|
2022-10-17 08:50:33 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (inputRef?.current) {
|
|
|
|
inputRef.current.focus();
|
|
|
|
}
|
|
|
|
}, [chat.id, inputRef?.current]);
|
|
|
|
|
2022-04-12 09:52:56 -07:00
|
|
|
return (
|
2022-09-12 13:46:19 -07:00
|
|
|
<Stack className={classNames('overflow-hidden flex flex-grow', className)} onMouseOver={handleMouseOver}>
|
|
|
|
<div className='flex-grow h-full overflow-hidden flex justify-center'>
|
2022-11-03 10:55:33 -07:00
|
|
|
<ChatMessageList chat={chat} />
|
2022-09-12 13:46:19 -07:00
|
|
|
</div>
|
|
|
|
|
2022-10-04 16:00:00 -07:00
|
|
|
<ChatComposer
|
|
|
|
ref={inputRef}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
value={content}
|
|
|
|
onChange={handleContentChange}
|
|
|
|
onSubmit={sendMessage}
|
2022-10-17 09:23:03 -07:00
|
|
|
hasErrorSubmittingMessage={hasErrorSubmittingMessage}
|
2022-10-04 16:00:00 -07:00
|
|
|
/>
|
2022-09-12 13:46:19 -07:00
|
|
|
</Stack>
|
|
|
|
// {renderAttachment()}
|
|
|
|
// {isUploading && (
|
|
|
|
// <UploadProgress progress={uploadProgress * 100} />
|
|
|
|
// )}
|
|
|
|
// <div className='chat-box__actions simple_form'>
|
|
|
|
// <div className='chat-box__send'>
|
|
|
|
// {renderActionButton()}
|
|
|
|
// </div>
|
|
|
|
// <textarea
|
|
|
|
// rows={1}
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// onPaste={handlePaste}
|
|
|
|
// value={content}
|
|
|
|
// ref={setInputRef}
|
|
|
|
// />
|
|
|
|
// </div>
|
|
|
|
// </div>
|
2022-04-12 09:52:56 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Chat;
|