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

View File

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

View File

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

View File

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

View File

@ -18,17 +18,12 @@ export type ChatReadState = {
// booleans if channel is unread // booleans if channel is unread
// {cid: unread, ...} // {cid: unread, ...}
unread: Object, 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 = { const initialState: ChatReadState = {
mute: [], mute: [],
readTs: {}, readTs: {},
unread: {}, 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': { case 'ADD_CHAT_CHANNEL': {
const [cid] = Object.keys(action.channel); const [cid] = Object.keys(action.channel);
return { return {
@ -123,19 +86,19 @@ export default function chatRead(
} }
case 'RECEIVE_CHAT_MESSAGE': { case 'RECEIVE_CHAT_MESSAGE': {
const { channel: cid } = action; const { channel: cid, isRead } = action;
const { chatChannels } = state; const readTs = isRead
const readTs = chatChannels.includes(cid)
? { ? {
...state.readTs, ...state.readTs,
// 15s treshold for desync // 15s treshold for desync
[cid]: Date.now() + TIME_DIFF_THREASHOLD, [cid]: Date.now() + TIME_DIFF_THREASHOLD,
} : state.readTs; } : state.readTs;
const unread = chatChannels.includes(cid) const unread = isRead
? { ? state.unread
: {
...state.unread, ...state.unread,
[cid]: true, [cid]: true,
} : state.unread; };
return { return {
...state, ...state,
readTs, 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': { case 'MUTE_CHAT_CHANNEL': {
const { cid } = action; const { cid } = action;
return { return {

View File

@ -166,7 +166,8 @@ class ProtocolClient extends EventEmitter {
case 5: { case 5: {
// chat message // chat message
const [name, text, country, channelId, userId] = data; 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; return;
} }