feat: undelivered UI

This commit is contained in:
cha0s 2020-07-25 08:18:21 -05:00
parent 6fadb45b33
commit 4be2f0f4b2
4 changed files with 28 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import './chat--message.scss';
import classnames from 'classnames'; import classnames from 'classnames';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React, {useEffect, useState} from 'react';
import ReactMarkdown from 'react-markdown'; import ReactMarkdown from 'react-markdown';
import {useSelector} from 'react-redux'; import {useSelector} from 'react-redux';
@ -12,12 +12,18 @@ export default function ChatMessage(props) {
const { const {
message: { message: {
owner, owner,
pending,
message, message,
timestamp, timestamp,
rejected, rejected,
}, },
isShort, isShort,
} = props; } = props;
const [undelivered, setUndelivered] = useState(false);
useEffect(() => {
const handle = setTimeout(() => pending && setUndelivered(true), 500);
return () => clearTimeout(handle);
});
const username = useSelector((state) => usernameSelector(state, owner)); const username = useSelector((state) => usernameSelector(state, owner));
const dtf = new Intl.DateTimeFormat(undefined, { const dtf = new Intl.DateTimeFormat(undefined, {
hour: '2-digit', hour: '2-digit',
@ -34,7 +40,15 @@ export default function ChatMessage(props) {
); );
return ( return (
<div <div
className={classnames('chat--message', {rejected, short: isShort, system: -1 === owner})} className={classnames(
'chat--message',
{
rejected,
short: isShort,
system: -1 === owner,
undelivered,
},
)}
> >
{ {
!isShort && ( !isShort && (
@ -58,6 +72,7 @@ ChatMessage.propTypes = {
isShort: PropTypes.bool.isRequired, isShort: PropTypes.bool.isRequired,
message: PropTypes.shape({ message: PropTypes.shape({
message: PropTypes.string, message: PropTypes.string,
pending: PropTypes.bool,
rejected: PropTypes.bool, rejected: PropTypes.bool,
timestamp: PropTypes.number, timestamp: PropTypes.number,
owner: PropTypes.number, owner: PropTypes.number,

View File

@ -3,12 +3,16 @@
.chat--message { .chat--message {
margin-top: 0.75rem; margin-top: 0.75rem;
padding: 0.25rem 1rem; padding: 0.25rem 1rem;
transition: opacity 0.5s;
&.rejected { &.rejected {
background-color: rgba(200, 0, 0, 0.2); background-color: rgba(200, 0, 0, 0.2);
} }
&.system { &.system {
background-color: rgba(200, 0, 0, 0.2); background-color: rgba(200, 0, 0, 0.2);
} }
&.undelivered {
opacity: 0.3;
}
} }
.chat--message, .chat--message * { .chat--message, .chat--message * {

View File

@ -29,6 +29,7 @@ export default function ChatSubmitMessage() {
channel, channel,
message: chunk, message: chunk,
owner: '/r/anonymous' === channel ? 0 : user.id, owner: '/r/anonymous' === channel ? 0 : user.id,
pending: true,
timestamp: Date.now(), timestamp: Date.now(),
uuid: uuidv4(), uuid: uuidv4(),
})); }));

View File

@ -42,7 +42,12 @@ const slice = createSlice({
channels[channel].messages.push(uuid); channels[channel].messages.push(uuid);
}, },
confirmMessage: ({channels, messages}, {payload: {previous, current, timestamp}}) => { confirmMessage: ({channels, messages}, {payload: {previous, current, timestamp}}) => {
messages[current] = {...messages[previous], timestamp, uuid: current}; const {pending, ...previousMessage} = messages[previous];
messages[current] = {
...previousMessage,
timestamp,
uuid: current,
};
delete messages[previous]; delete messages[previous];
const {[messages[current].channel]: channel} = channels; const {[messages[current].channel]: channel} = channels;
const index = channel.messages.findIndex((uuid) => uuid === previous); const index = channel.messages.findIndex((uuid) => uuid === previous);