PromucFlow_constructor/app/client/src/sagas/WebsocketSagas/handleSocketEvent.ts

94 lines
3.0 KiB
TypeScript
Raw Normal View History

import { put, select } from "redux-saga/effects";
2021-06-09 14:32:17 +00:00
import { SOCKET_EVENTS } from "./constants";
import { APP_COLLAB_EVENTS } from "constants/AppCollabConstants";
2021-06-09 14:32:17 +00:00
import {
newCommentEvent,
newCommentThreadEvent,
updateCommentThreadEvent,
updateCommentEvent,
incrementThreadUnreadCount,
2021-07-28 07:02:11 +00:00
decrementThreadUnreadCount,
2021-06-09 14:32:17 +00:00
} from "actions/commentActions";
import { newNotificationEvent } from "actions/notificationActions";
import { getCurrentUser } from "selectors/usersSelectors";
import { getCurrentApplication } from "selectors/applicationSelectors";
2021-07-28 07:02:11 +00:00
import { commentThreadsSelector } from "selectors/commentsSelectors";
import { AppState } from "reducers";
import { CommentThread } from "entities/Comments/CommentsInterfaces";
import { collabListAppEditorsEvent } from "../../actions/appCollabActions";
2021-06-09 14:32:17 +00:00
export default function* handleSocketEvent(event: any) {
const currentUser = yield select(getCurrentUser);
const currentApplication = yield select(getCurrentApplication);
2021-06-09 14:32:17 +00:00
switch (event.type) {
// comments
case SOCKET_EVENTS.INSERT_COMMENT_THREAD: {
yield put(newCommentThreadEvent(event.payload[0]));
const { thread } = event.payload[0];
const isForCurrentApplication =
thread?.applicationId === currentApplication.id;
const isCreatedByMe = thread?.authorUsername === currentUser.username;
if (!isCreatedByMe && isForCurrentApplication)
yield put(incrementThreadUnreadCount());
2021-06-09 14:32:17 +00:00
return;
}
case SOCKET_EVENTS.INSERT_COMMENT: {
yield put(newCommentEvent(event.payload[0]));
return;
}
2021-07-28 07:02:11 +00:00
case SOCKET_EVENTS.REPLACE_COMMENT_THREAD:
2021-06-09 14:32:17 +00:00
case SOCKET_EVENTS.UPDATE_COMMENT_THREAD: {
2021-07-28 07:02:11 +00:00
const { thread } = event.payload[0];
const threadInStore: CommentThread = yield select((state: AppState) =>
commentThreadsSelector(thread?._id)(state),
);
const isThreadInStoreViewed = threadInStore?.isViewed;
const isNowResolved =
!threadInStore?.resolvedState?.active && thread?.resolvedState?.active;
2021-07-28 07:02:11 +00:00
const isThreadFromEventViewed = thread?.viewedByUsers?.includes(
currentUser?.username,
);
yield put(
updateCommentThreadEvent({
...thread,
isViewed: isThreadFromEventViewed || thread?.resolvedState?.active, // resolved threads can't be unread
2021-07-28 07:02:11 +00:00
}),
);
if (isThreadInStoreViewed && !isThreadFromEventViewed) {
yield put(incrementThreadUnreadCount());
} else if (
!isThreadInStoreViewed &&
(isThreadFromEventViewed || isNowResolved)
) {
2021-07-28 07:02:11 +00:00
yield put(decrementThreadUnreadCount());
}
2021-06-09 14:32:17 +00:00
return;
}
case SOCKET_EVENTS.UPDATE_COMMENT: {
yield put(updateCommentEvent(event.payload[0].comment));
return;
}
// notifications
case SOCKET_EVENTS.INSERT_NOTIFICATION: {
yield put(newNotificationEvent(event.payload[0].notification));
return;
}
// Collab V2 - Realtime Editing
case APP_COLLAB_EVENTS.LIST_ONLINE_APP_EDITORS: {
yield put(collabListAppEditorsEvent(event.payload[0]));
return;
}
2021-06-09 14:32:17 +00:00
}
}