adjust chat read status to new windows

This commit is contained in:
HF 2021-04-29 04:32:43 +02:00
parent f120fdfdc2
commit 3ce2da7709
6 changed files with 52 additions and 46 deletions

View File

@ -224,6 +224,7 @@ export function receiveChatMessage(
channel: number,
user: number,
isPing: boolean,
isRead: boolean,
): Action {
return {
type: 'RECEIVE_CHAT_MESSAGE',
@ -233,6 +234,7 @@ export function receiveChatMessage(
channel,
user,
isPing,
isRead,
};
}

View File

@ -64,6 +64,7 @@ export type Action =
channel: number,
user: number,
isPing: boolean,
isRead: boolean,
}
| { type: 'RECEIVE_CHAT_HISTORY', cid: number, history: Array }
| { type: 'OPEN_CHAT_CHANNEL', cid: number }

View File

@ -63,14 +63,16 @@ function init() {
) => {
const state = store.getState();
const { nameRegExp } = state.user;
const isRead = Object.values(state.windows.args).find(((args) => args.chatChannel === channelId));
const isPing = (nameRegExp && text.match(nameRegExp));
store.dispatch(receiveChatMessage(
name,
text,
country,
Number(channelId),
channelId,
userId,
isPing,
!!isRead,
));
});
ProtocolClient.on('changedMe', () => {

View File

@ -25,6 +25,8 @@ const Window = ({ id }) => {
const startMove = useCallback((event) => {
event.preventDefault();
dispatch(focusWindow());
let {
clientX: startX,
clientY: startY,
@ -50,6 +52,8 @@ const Window = ({ id }) => {
const startResize = useCallback((event) => {
event.preventDefault();
dispatch(focusWindow());
let {
clientX: startX,
clientY: startY,
@ -111,7 +115,7 @@ const Window = ({ id }) => {
return (
<div
className={`window ${windowType}`}
onMouseDown={() => dispatch(focusWindow(id))}
onClick={() => dispatch(focusWindow(id))}
style={{
left: xPos,
top: yPos,

View File

@ -18,17 +18,12 @@ export type ChatReadState = {
// booleans if channel is unread
// {cid: unread, ...}
unread: Object,
// currently open chat channels can contain duplications
// just used to keep track of what channels we are seeing in
// windows to decide if readTS gets changed,
chatChannels: Array,
};
const initialState: ChatReadState = {
mute: [],
readTs: {},
unread: {},
chatChannels: [],
};
@ -59,38 +54,6 @@ export default function chatRead(
};
}
case 'OPEN_CHAT_CHANNEL': {
const { cid } = action;
return {
...state,
chatChannels: [
...state.chatChannels,
cid,
],
readTs: {
...state.readTs,
[cid]: Date.now() + TIME_DIFF_THREASHOLD,
},
unread: {
...state.unread,
[cid]: false,
},
};
}
case 'CLOSE_CHAT_CHANNEL': {
const { cid } = action;
const chatChannels = [...state.chatChannels];
const pos = chatChannels.indexOf(cid);
if (pos !== -1) {
chatChannels.splice(pos, 1);
}
return {
...state,
chatChannels,
};
}
case 'ADD_CHAT_CHANNEL': {
const [cid] = Object.keys(action.channel);
return {
@ -123,19 +86,19 @@ export default function chatRead(
}
case 'RECEIVE_CHAT_MESSAGE': {
const { channel: cid } = action;
const { chatChannels } = state;
const readTs = chatChannels.includes(cid)
const { channel: cid, isRead } = action;
const readTs = isRead
? {
...state.readTs,
// 15s treshold for desync
[cid]: Date.now() + TIME_DIFF_THREASHOLD,
} : state.readTs;
const unread = chatChannels.includes(cid)
? {
const unread = isRead
? state.unread
: {
...state.unread,
[cid]: true,
} : state.unread;
};
return {
...state,
readTs,
@ -143,6 +106,39 @@ export default function chatRead(
};
}
case 'OPEN_WINDOW': {
const cid = action.args.chatChannel;
if (!cid) {
return state;
}
return {
...state,
readTs: {
...state.readTs,
[cid]: Date.now() + TIME_DIFF_THREASHOLD,
},
unread: {
...state.unread,
[cid]: false,
},
};
}
case 'SET_CHAT_CHANNEL': {
const { cid } = action;
return {
...state,
readTs: {
...state.readTs,
[cid]: Date.now() + TIME_DIFF_THREASHOLD,
},
unread: {
...state.unread,
[cid]: false,
},
};
}
case 'MUTE_CHAT_CHANNEL': {
const { cid } = action;
return {

View File

@ -166,7 +166,8 @@ class ProtocolClient extends EventEmitter {
case 5: {
// chat message
const [name, text, country, channelId, userId] = data;
this.emit('chatMessage', name, text, country, channelId, userId);
this.emit('chatMessage',
name, text, country, Number(channelId), userId);
return;
}