2021-11-21 11:00:21 +02:00
|
|
|
/* eslint-disable jsx-a11y/no-static-element-interactions */
|
|
|
|
/* eslint-disable jsx-a11y/click-events-have-key-events */
|
2021-08-04 12:52:59 +03:00
|
|
|
/* eslint-disable react/prop-types */
|
2021-12-03 15:02:10 +02:00
|
|
|
import React, {
|
|
|
|
useState, useEffect, useLayoutEffect, useCallback, useRef,
|
|
|
|
} from 'react';
|
2021-08-04 12:52:59 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2021-08-31 16:13:31 +03:00
|
|
|
import './RoomViewContent.scss';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
import EventEmitter from 'events';
|
2021-08-04 12:52:59 +03:00
|
|
|
import dateFormat from 'dateformat';
|
|
|
|
|
|
|
|
import initMatrix from '../../../client/initMatrix';
|
|
|
|
import cons from '../../../client/state/cons';
|
2021-12-03 15:02:10 +02:00
|
|
|
import navigation from '../../../client/state/navigation';
|
2021-11-21 11:00:21 +02:00
|
|
|
import { openProfileViewer } from '../../../client/action/navigation';
|
2021-12-03 15:02:10 +02:00
|
|
|
import {
|
2021-12-08 10:07:25 +02:00
|
|
|
diffMinutes, isInSameDay, Throttle, getScrollInfo,
|
2021-12-03 15:02:10 +02:00
|
|
|
} from '../../../util/common';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
|
|
|
import Divider from '../../atoms/divider/Divider';
|
2021-12-03 15:02:10 +02:00
|
|
|
import ScrollView from '../../atoms/scroll/ScrollView';
|
2021-11-20 09:59:32 +02:00
|
|
|
import { Message, PlaceholderMessage } from '../../molecules/message/Message';
|
2021-08-31 16:13:31 +03:00
|
|
|
import RoomIntro from '../../molecules/room-intro/RoomIntro';
|
2021-08-04 12:52:59 +03:00
|
|
|
import TimelineChange from '../../molecules/message/TimelineChange';
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
import { useStore } from '../../hooks/useStore';
|
2021-12-04 11:55:14 +02:00
|
|
|
import { useForceUpdate } from '../../hooks/useForceUpdate';
|
2021-11-20 09:59:32 +02:00
|
|
|
import { parseTimelineChange } from './common';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-04 11:55:14 +02:00
|
|
|
const DEFAULT_MAX_EVENTS = 50;
|
|
|
|
const PAG_LIMIT = 30;
|
2021-08-04 12:52:59 +03:00
|
|
|
const MAX_MSG_DIFF_MINUTES = 5;
|
2021-12-03 15:02:10 +02:00
|
|
|
const PLACEHOLDER_COUNT = 2;
|
|
|
|
const PLACEHOLDERS_HEIGHT = 96 * PLACEHOLDER_COUNT;
|
|
|
|
const SCROLL_TRIGGER_POS = PLACEHOLDERS_HEIGHT * 4;
|
|
|
|
|
|
|
|
const SMALLEST_MSG_HEIGHT = 32;
|
|
|
|
const PAGES_COUNT = 4;
|
|
|
|
|
|
|
|
function loadingMsgPlaceholders(key, count = 2) {
|
|
|
|
const pl = [];
|
|
|
|
const genPlaceholders = () => {
|
|
|
|
for (let i = 0; i < count; i += 1) {
|
|
|
|
pl.push(<PlaceholderMessage key={`placeholder-${i}${key}`} />);
|
|
|
|
}
|
|
|
|
return pl;
|
|
|
|
};
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-08-15 11:29:09 +03:00
|
|
|
return (
|
2021-11-18 10:02:12 +02:00
|
|
|
<React.Fragment key={`placeholder-container${key}`}>
|
2021-12-03 15:02:10 +02:00
|
|
|
{genPlaceholders()}
|
2021-11-18 10:02:12 +02:00
|
|
|
</React.Fragment>
|
2021-08-15 11:29:09 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:13:31 +03:00
|
|
|
function genRoomIntro(mEvent, roomTimeline) {
|
2021-08-17 14:21:22 +03:00
|
|
|
const mx = initMatrix.matrixClient;
|
2021-08-15 11:29:09 +03:00
|
|
|
const roomTopic = roomTimeline.room.currentState.getStateEvents('m.room.topic')[0]?.getContent().topic;
|
2021-08-17 14:21:22 +03:00
|
|
|
const isDM = initMatrix.roomList.directs.has(roomTimeline.roomId);
|
|
|
|
let avatarSrc = roomTimeline.room.getAvatarUrl(mx.baseUrl, 80, 80, 'crop');
|
|
|
|
avatarSrc = isDM ? roomTimeline.room.getAvatarFallbackMember()?.getAvatarUrl(mx.baseUrl, 80, 80, 'crop') : avatarSrc;
|
2021-08-15 11:29:09 +03:00
|
|
|
return (
|
2021-08-31 16:13:31 +03:00
|
|
|
<RoomIntro
|
|
|
|
key={mEvent ? mEvent.getId() : 'room-intro'}
|
2021-08-17 14:34:21 +03:00
|
|
|
roomId={roomTimeline.roomId}
|
2021-08-17 14:21:22 +03:00
|
|
|
avatarSrc={avatarSrc}
|
2021-08-15 11:29:09 +03:00
|
|
|
name={roomTimeline.room.name}
|
|
|
|
heading={`Welcome to ${roomTimeline.room.name}`}
|
2021-08-31 16:13:31 +03:00
|
|
|
desc={`This is the beginning of ${roomTimeline.room.name} room.${typeof roomTopic !== 'undefined' ? (` Topic: ${roomTopic}`) : ''}`}
|
2021-08-15 11:29:09 +03:00
|
|
|
time={mEvent ? `Created at ${dateFormat(mEvent.getDate(), 'dd mmmm yyyy, hh:MM TT')}` : null}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
function handleOnClickCapture(e) {
|
2021-12-13 11:44:57 +02:00
|
|
|
const { target, nativeEvent } = e;
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
const userId = target.getAttribute('data-mx-pill');
|
2021-12-13 11:44:57 +02:00
|
|
|
if (userId) {
|
|
|
|
const roomId = navigation.selectedRoomId;
|
|
|
|
openProfileViewer(userId, roomId);
|
|
|
|
}
|
2021-11-20 09:59:32 +02:00
|
|
|
|
2021-12-13 11:44:57 +02:00
|
|
|
const spoiler = nativeEvent.path.find((el) => el?.hasAttribute?.('data-mx-spoiler'));
|
|
|
|
if (spoiler) {
|
|
|
|
spoiler.classList.toggle('data-mx-spoiler--visible');
|
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-11-20 09:59:32 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
function renderEvent(roomTimeline, mEvent, prevMEvent, isFocus = false) {
|
2021-12-13 07:33:48 +02:00
|
|
|
const isBodyOnly = (prevMEvent !== null
|
|
|
|
&& prevMEvent.getType() !== 'm.room.member'
|
|
|
|
&& prevMEvent.getType() !== 'm.room.create'
|
2021-12-03 15:02:10 +02:00
|
|
|
&& diffMinutes(mEvent.getDate(), prevMEvent.getDate()) <= MAX_MSG_DIFF_MINUTES
|
|
|
|
&& prevMEvent.getSender() === mEvent.getSender()
|
|
|
|
);
|
2021-12-08 10:07:25 +02:00
|
|
|
const mDate = mEvent.getDate();
|
|
|
|
const isToday = isInSameDay(mDate, new Date());
|
|
|
|
|
|
|
|
const time = dateFormat(mDate, isToday ? 'hh:MM TT' : 'dd/mm/yyyy');
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
if (mEvent.getType() === 'm.room.member') {
|
|
|
|
const timelineChange = parseTimelineChange(mEvent);
|
2021-12-07 17:34:07 +02:00
|
|
|
if (timelineChange === null) return <div key={mEvent.getId()} />;
|
2021-12-03 15:02:10 +02:00
|
|
|
return (
|
|
|
|
<TimelineChange
|
|
|
|
key={mEvent.getId()}
|
|
|
|
variant={timelineChange.variant}
|
|
|
|
content={timelineChange.content}
|
2021-12-08 10:07:25 +02:00
|
|
|
time={time}
|
2021-12-03 15:02:10 +02:00
|
|
|
/>
|
|
|
|
);
|
2021-08-04 12:52:59 +03:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
return (
|
|
|
|
<Message
|
|
|
|
key={mEvent.getId()}
|
|
|
|
mEvent={mEvent}
|
|
|
|
isBodyOnly={isBodyOnly}
|
|
|
|
roomTimeline={roomTimeline}
|
|
|
|
focus={isFocus}
|
2021-12-08 10:07:25 +02:00
|
|
|
time={time}
|
2021-12-03 15:02:10 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TimelineScroll extends EventEmitter {
|
|
|
|
constructor(target) {
|
|
|
|
super();
|
|
|
|
if (target === null) {
|
|
|
|
throw new Error('Can not initialize TimelineScroll, target HTMLElement in null');
|
2021-08-04 12:52:59 +03:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
this.scroll = target;
|
|
|
|
|
|
|
|
this.backwards = false;
|
|
|
|
this.inTopHalf = false;
|
2021-12-04 11:55:14 +02:00
|
|
|
this.maxEvents = DEFAULT_MAX_EVENTS;
|
2021-12-03 15:02:10 +02:00
|
|
|
|
|
|
|
this.isScrollable = false;
|
|
|
|
this.top = 0;
|
|
|
|
this.bottom = 0;
|
|
|
|
this.height = 0;
|
|
|
|
this.viewHeight = 0;
|
|
|
|
|
|
|
|
this.topMsg = null;
|
|
|
|
this.bottomMsg = null;
|
|
|
|
this.diff = 0;
|
2021-08-04 12:52:59 +03:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
scrollToBottom() {
|
|
|
|
const scrollInfo = getScrollInfo(this.scroll);
|
|
|
|
const maxScrollTop = scrollInfo.height - scrollInfo.viewHeight;
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
this._scrollTo(scrollInfo, maxScrollTop);
|
|
|
|
}
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
// restore scroll using previous calc by this._updateTopBottomMsg() and this._calcDiff.
|
|
|
|
tryRestoringScroll() {
|
|
|
|
const scrollInfo = getScrollInfo(this.scroll);
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
let scrollTop = 0;
|
|
|
|
const ot = this.inTopHalf ? this.topMsg?.offsetTop : this.bottomMsg?.offsetTop;
|
2021-12-07 17:34:07 +02:00
|
|
|
if (!ot) scrollTop = Math.round(this.height - this.viewHeight);
|
2021-12-03 15:02:10 +02:00
|
|
|
else scrollTop = ot - this.diff;
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
this._scrollTo(scrollInfo, scrollTop);
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToIndex(index, offset = 0) {
|
|
|
|
const scrollInfo = getScrollInfo(this.scroll);
|
|
|
|
const msgs = this.scroll.lastElementChild.lastElementChild.children;
|
|
|
|
const offsetTop = msgs[index]?.offsetTop;
|
|
|
|
|
|
|
|
if (offsetTop === undefined) return;
|
|
|
|
// if msg is already in visible are we don't need to scroll to that
|
|
|
|
if (offsetTop > scrollInfo.top && offsetTop < (scrollInfo.top + scrollInfo.viewHeight)) return;
|
|
|
|
const to = offsetTop - offset;
|
|
|
|
|
|
|
|
this._scrollTo(scrollInfo, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
_scrollTo(scrollInfo, scrollTop) {
|
|
|
|
this.scroll.scrollTop = scrollTop;
|
|
|
|
|
|
|
|
// browser emit 'onscroll' event only if the 'element.scrollTop' value changes.
|
|
|
|
// so here we flag that the upcoming 'onscroll' event is
|
|
|
|
// emitted as side effect of assigning 'this.scroll.scrollTop' above
|
|
|
|
// only if it's changes.
|
|
|
|
// by doing so we prevent this._updateCalc() from calc again.
|
|
|
|
if (scrollTop !== this.top) {
|
|
|
|
this.scrolledByCode = true;
|
2021-11-18 10:02:12 +02:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
const sInfo = { ...scrollInfo };
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
const maxScrollTop = scrollInfo.height - scrollInfo.viewHeight;
|
|
|
|
|
|
|
|
sInfo.top = (scrollTop > maxScrollTop) ? maxScrollTop : scrollTop;
|
|
|
|
this._updateCalc(sInfo);
|
|
|
|
}
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
// we maintain reference of top and bottom messages
|
|
|
|
// to restore the scroll position when
|
|
|
|
// messages gets removed from either end and added to other.
|
|
|
|
_updateTopBottomMsg() {
|
|
|
|
const msgs = this.scroll.lastElementChild.lastElementChild.children;
|
|
|
|
const lMsgIndex = msgs.length - 1;
|
|
|
|
|
|
|
|
this.topMsg = msgs[0]?.className === 'ph-msg'
|
|
|
|
? msgs[PLACEHOLDER_COUNT]
|
|
|
|
: msgs[0];
|
|
|
|
this.bottomMsg = msgs[lMsgIndex]?.className === 'ph-msg'
|
|
|
|
? msgs[lMsgIndex - PLACEHOLDER_COUNT]
|
|
|
|
: msgs[lMsgIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// we calculate the difference between first/last message and current scrollTop.
|
|
|
|
// if we are going above we calc diff between first and scrollTop
|
|
|
|
// else otherwise.
|
|
|
|
// NOTE: This will help to restore the scroll when msgs get's removed
|
|
|
|
// from one end and added to other end
|
|
|
|
_calcDiff(scrollInfo) {
|
|
|
|
if (!this.topMsg || !this.bottomMsg) return 0;
|
|
|
|
if (this.inTopHalf) {
|
|
|
|
return this.topMsg.offsetTop - scrollInfo.top;
|
2021-08-04 12:52:59 +03:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
return this.bottomMsg.offsetTop - scrollInfo.top;
|
|
|
|
}
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-12-04 16:04:22 +02:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2021-12-03 15:02:10 +02:00
|
|
|
_calcMaxEvents(scrollInfo) {
|
|
|
|
return Math.round(scrollInfo.viewHeight / SMALLEST_MSG_HEIGHT) * PAGES_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateCalc(scrollInfo) {
|
|
|
|
const halfViewHeight = Math.round(scrollInfo.viewHeight / 2);
|
|
|
|
const scrollMiddle = scrollInfo.top + halfViewHeight;
|
|
|
|
const lastMiddle = this.top + halfViewHeight;
|
|
|
|
|
|
|
|
this.backwards = scrollMiddle < lastMiddle;
|
|
|
|
this.inTopHalf = scrollMiddle < scrollInfo.height / 2;
|
|
|
|
|
|
|
|
this.isScrollable = scrollInfo.isScrollable;
|
|
|
|
this.top = scrollInfo.top;
|
|
|
|
this.bottom = scrollInfo.height - (scrollInfo.top + scrollInfo.viewHeight);
|
|
|
|
this.height = scrollInfo.height;
|
|
|
|
|
|
|
|
// only calculate maxEvents if viewHeight change
|
|
|
|
if (this.viewHeight !== scrollInfo.viewHeight) {
|
|
|
|
this.maxEvents = this._calcMaxEvents(scrollInfo);
|
|
|
|
this.viewHeight = scrollInfo.viewHeight;
|
2021-08-04 12:52:59 +03:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
|
|
|
|
this._updateTopBottomMsg();
|
|
|
|
this.diff = this._calcDiff(scrollInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
calcScroll() {
|
|
|
|
if (this.scrolledByCode) {
|
|
|
|
this.scrolledByCode = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scrollInfo = getScrollInfo(this.scroll);
|
|
|
|
this._updateCalc(scrollInfo);
|
|
|
|
|
|
|
|
this.emit('scroll', this.backwards);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let timelineScroll = null;
|
2021-12-07 17:34:07 +02:00
|
|
|
let jumpToItemIndex = -1;
|
2021-12-03 15:02:10 +02:00
|
|
|
const throttle = new Throttle();
|
2021-12-04 11:55:14 +02:00
|
|
|
const limit = {
|
|
|
|
from: 0,
|
|
|
|
getMaxEvents() {
|
|
|
|
return timelineScroll?.maxEvents ?? DEFAULT_MAX_EVENTS;
|
|
|
|
},
|
|
|
|
getEndIndex() {
|
|
|
|
return this.from + this.getMaxEvents();
|
|
|
|
},
|
|
|
|
calcNextFrom(backwards, tLength) {
|
|
|
|
let newFrom = backwards ? this.from - PAG_LIMIT : this.from + PAG_LIMIT;
|
|
|
|
if (!backwards && newFrom + this.getMaxEvents() > tLength) {
|
|
|
|
newFrom = tLength - this.getMaxEvents();
|
|
|
|
}
|
|
|
|
if (newFrom < 0) newFrom = 0;
|
|
|
|
return newFrom;
|
|
|
|
},
|
|
|
|
setFrom(from) {
|
|
|
|
if (from < 0) {
|
|
|
|
this.from = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.from = from;
|
|
|
|
},
|
|
|
|
};
|
2021-12-03 15:02:10 +02:00
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
function useTimeline(roomTimeline, eventId, readEventStore) {
|
2021-12-03 15:02:10 +02:00
|
|
|
const [timelineInfo, setTimelineInfo] = useState(null);
|
|
|
|
|
|
|
|
const setEventTimeline = async (eId) => {
|
|
|
|
if (typeof eId === 'string') {
|
|
|
|
const isLoaded = await roomTimeline.loadEventTimeline(eId);
|
|
|
|
if (isLoaded) return;
|
|
|
|
// if eventTimeline failed to load,
|
|
|
|
// we will load live timeline as fallback.
|
|
|
|
}
|
|
|
|
roomTimeline.loadLiveTimeline();
|
2021-11-18 10:02:12 +02:00
|
|
|
};
|
|
|
|
|
2021-08-04 12:52:59 +03:00
|
|
|
useEffect(() => {
|
2021-12-07 17:34:07 +02:00
|
|
|
const initTimeline = (eId) => {
|
|
|
|
// NOTICE: eId can be id of readUpto, reply or specific event.
|
|
|
|
// readUpTo: when user click jump to unread message button.
|
|
|
|
// reply: when user click reply from timeline.
|
|
|
|
// specific event when user open a link of event. behave same as ^^^^
|
|
|
|
const readUpToId = roomTimeline.getReadUpToEventId();
|
|
|
|
let focusEventIndex = -1;
|
|
|
|
const isSpecificEvent = eId && eId !== readUpToId;
|
|
|
|
|
|
|
|
if (isSpecificEvent) {
|
|
|
|
focusEventIndex = roomTimeline.getEventIndex(eId);
|
|
|
|
} else if (!readEventStore.getItem()) {
|
|
|
|
// either opening live timeline or jump to unread.
|
|
|
|
focusEventIndex = roomTimeline.getUnreadEventIndex(readUpToId);
|
|
|
|
if (roomTimeline.hasEventInTimeline(readUpToId)) {
|
|
|
|
readEventStore.setItem(roomTimeline.findEventByIdInTimelineSet(readUpToId));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
focusEventIndex = roomTimeline.getUnreadEventIndex(readEventStore.getItem().getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusEventIndex > -1) {
|
|
|
|
limit.setFrom(focusEventIndex - Math.round(limit.getMaxEvents() / 2));
|
|
|
|
} else {
|
|
|
|
limit.setFrom(roomTimeline.timeline.length - limit.getMaxEvents());
|
|
|
|
}
|
|
|
|
setTimelineInfo({ focusEventId: isSpecificEvent ? eId : null });
|
|
|
|
};
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
roomTimeline.on(cons.events.roomTimeline.READY, initTimeline);
|
|
|
|
setEventTimeline(eventId);
|
2021-11-18 10:02:12 +02:00
|
|
|
return () => {
|
2021-12-03 15:02:10 +02:00
|
|
|
roomTimeline.removeListener(cons.events.roomTimeline.READY, initTimeline);
|
|
|
|
roomTimeline.removeInternalListeners();
|
2021-12-04 11:55:14 +02:00
|
|
|
limit.setFrom(0);
|
2021-11-18 10:02:12 +02:00
|
|
|
};
|
2021-12-03 15:02:10 +02:00
|
|
|
}, [roomTimeline, eventId]);
|
|
|
|
|
|
|
|
return timelineInfo;
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
function usePaginate(roomTimeline, readEventStore, forceUpdateLimit) {
|
2021-12-03 15:02:10 +02:00
|
|
|
const [info, setInfo] = useState(null);
|
2021-08-04 12:52:59 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-12-09 08:30:19 +02:00
|
|
|
const handleOnPagination = (backwards, loaded) => {
|
2021-12-04 11:55:14 +02:00
|
|
|
if (loaded === 0) return;
|
2021-12-07 17:34:07 +02:00
|
|
|
if (!readEventStore.getItem()) {
|
|
|
|
const readUpToId = roomTimeline.getReadUpToEventId();
|
|
|
|
readEventStore.setItem(roomTimeline.findEventByIdInTimelineSet(readUpToId));
|
|
|
|
}
|
2021-12-04 11:55:14 +02:00
|
|
|
limit.setFrom(limit.calcNextFrom(backwards, roomTimeline.timeline.length));
|
2021-12-09 08:30:19 +02:00
|
|
|
setTimeout(() => setInfo({
|
2021-12-03 15:02:10 +02:00
|
|
|
backwards,
|
|
|
|
loaded,
|
2021-12-09 08:30:19 +02:00
|
|
|
}));
|
2021-12-03 15:02:10 +02:00
|
|
|
};
|
|
|
|
roomTimeline.on(cons.events.roomTimeline.PAGINATED, handleOnPagination);
|
|
|
|
return () => {
|
|
|
|
roomTimeline.on(cons.events.roomTimeline.PAGINATED, handleOnPagination);
|
|
|
|
};
|
|
|
|
}, [roomTimeline]);
|
|
|
|
|
2021-12-09 08:30:19 +02:00
|
|
|
const autoPaginate = useCallback(async () => {
|
2021-12-03 15:02:10 +02:00
|
|
|
if (roomTimeline.isOngoingPagination) return;
|
2021-12-04 11:55:14 +02:00
|
|
|
const tLength = roomTimeline.timeline.length;
|
|
|
|
|
|
|
|
if (timelineScroll.bottom < SCROLL_TRIGGER_POS) {
|
|
|
|
if (limit.getEndIndex() < tLength) {
|
|
|
|
// paginate from memory
|
|
|
|
limit.setFrom(limit.calcNextFrom(false, tLength));
|
|
|
|
forceUpdateLimit();
|
|
|
|
} else if (roomTimeline.canPaginateForward()) {
|
|
|
|
// paginate from server.
|
2021-12-09 08:30:19 +02:00
|
|
|
await roomTimeline.paginateTimeline(false, PAG_LIMIT);
|
2021-12-04 11:55:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-12-04 11:55:14 +02:00
|
|
|
if (timelineScroll.top < SCROLL_TRIGGER_POS) {
|
|
|
|
if (limit.from > 0) {
|
|
|
|
// paginate from memory
|
|
|
|
limit.setFrom(limit.calcNextFrom(true, tLength));
|
|
|
|
forceUpdateLimit();
|
|
|
|
} else if (roomTimeline.canPaginateBackward()) {
|
|
|
|
// paginate from server.
|
2021-12-09 08:30:19 +02:00
|
|
|
await roomTimeline.paginateTimeline(true, PAG_LIMIT);
|
2021-12-04 11:55:14 +02:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
|
|
|
}, [roomTimeline]);
|
2021-12-04 11:55:14 +02:00
|
|
|
|
|
|
|
return [info, autoPaginate];
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
function useHandleScroll(roomTimeline, autoPaginate, readEventStore, forceUpdateLimit) {
|
|
|
|
const handleScroll = useCallback(() => {
|
2021-12-03 15:02:10 +02:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
// emit event to toggle scrollToBottom button visibility
|
2021-12-04 11:55:14 +02:00
|
|
|
const isAtBottom = (
|
2021-12-07 17:34:07 +02:00
|
|
|
timelineScroll.bottom < 16 && !roomTimeline.canPaginateForward()
|
|
|
|
&& limit.getEndIndex() >= roomTimeline.timeline.length
|
2021-12-04 11:55:14 +02:00
|
|
|
);
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.emit(cons.events.roomTimeline.AT_BOTTOM, isAtBottom);
|
|
|
|
if (isAtBottom && readEventStore.getItem()) {
|
|
|
|
requestAnimationFrame(() => roomTimeline.markAllAsRead());
|
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
});
|
|
|
|
autoPaginate();
|
|
|
|
}, [roomTimeline]);
|
2021-12-07 17:34:07 +02:00
|
|
|
|
|
|
|
const handleScrollToLive = useCallback(() => {
|
|
|
|
if (readEventStore.getItem()) {
|
|
|
|
requestAnimationFrame(() => roomTimeline.markAllAsRead());
|
|
|
|
}
|
|
|
|
if (roomTimeline.isServingLiveTimeline()) {
|
|
|
|
limit.setFrom(roomTimeline.timeline.length - limit.getMaxEvents());
|
|
|
|
timelineScroll.scrollToBottom();
|
|
|
|
forceUpdateLimit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
roomTimeline.loadLiveTimeline();
|
|
|
|
}, [roomTimeline]);
|
|
|
|
|
|
|
|
return [handleScroll, handleScrollToLive];
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
function useEventArrive(roomTimeline, readEventStore) {
|
|
|
|
const myUserId = initMatrix.matrixClient.getUserId();
|
2021-12-03 15:02:10 +02:00
|
|
|
const [newEvent, setEvent] = useState(null);
|
|
|
|
useEffect(() => {
|
2021-12-07 17:34:07 +02:00
|
|
|
const sendReadReceipt = (event) => {
|
|
|
|
if (event.isSending()) return;
|
|
|
|
if (myUserId === event.getSender()) {
|
|
|
|
roomTimeline.markAllAsRead();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const readUpToEvent = readEventStore.getItem();
|
|
|
|
const readUpToId = roomTimeline.getReadUpToEventId();
|
|
|
|
|
|
|
|
// if user doesn't have focus on app don't mark messages as read.
|
|
|
|
if (document.visibilityState === 'hidden' || timelineScroll.bottom >= 16) {
|
|
|
|
if (readUpToEvent === readUpToId) return;
|
|
|
|
readEventStore.setItem(roomTimeline.findEventByIdInTimelineSet(readUpToId));
|
|
|
|
return;
|
|
|
|
}
|
2021-12-08 17:53:18 +02:00
|
|
|
const isUnreadMsg = readUpToEvent?.getId() === readUpToId;
|
|
|
|
if (!isUnreadMsg) {
|
|
|
|
roomTimeline.markAllAsRead();
|
|
|
|
}
|
|
|
|
const { timeline } = roomTimeline;
|
|
|
|
const unreadMsgIsLast = timeline[timeline.length - 2].getId() === readUpToEvent?.getId();
|
|
|
|
if (unreadMsgIsLast) {
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.markAllAsRead();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
const handleEvent = (event) => {
|
2021-12-04 11:55:14 +02:00
|
|
|
const tLength = roomTimeline.timeline.length;
|
2021-12-08 17:53:18 +02:00
|
|
|
const isUserViewingLive = (
|
|
|
|
roomTimeline.isServingLiveTimeline()
|
2021-12-07 17:34:07 +02:00
|
|
|
&& limit.getEndIndex() >= tLength - 1
|
2021-12-08 17:53:18 +02:00
|
|
|
&& timelineScroll.bottom < SCROLL_TRIGGER_POS
|
|
|
|
);
|
|
|
|
if (isUserViewingLive) {
|
2021-12-04 11:55:14 +02:00
|
|
|
limit.setFrom(tLength - limit.getMaxEvents());
|
2021-12-07 17:34:07 +02:00
|
|
|
sendReadReceipt(event);
|
|
|
|
setEvent(event);
|
2021-12-08 17:53:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const isRelates = (event.getType() === 'm.reaction' || event.getRelation()?.rel_type === 'm.replace');
|
|
|
|
if (isRelates) {
|
|
|
|
setEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const isUserDitchedLive = (
|
|
|
|
roomTimeline.isServingLiveTimeline()
|
|
|
|
&& limit.getEndIndex() >= tLength - 1
|
|
|
|
);
|
|
|
|
if (isUserDitchedLive) {
|
|
|
|
// This stateUpdate will help to put the
|
|
|
|
// loading msg placeholder at bottom
|
|
|
|
setEvent(event);
|
2021-12-04 11:55:14 +02:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
};
|
2021-12-07 17:34:07 +02:00
|
|
|
|
|
|
|
const handleEventRedact = (event) => setEvent(event);
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
roomTimeline.on(cons.events.roomTimeline.EVENT, handleEvent);
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.on(cons.events.roomTimeline.EVENT_REDACTED, handleEventRedact);
|
2021-08-04 12:52:59 +03:00
|
|
|
return () => {
|
2021-12-03 15:02:10 +02:00
|
|
|
roomTimeline.removeListener(cons.events.roomTimeline.EVENT, handleEvent);
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.removeListener(cons.events.roomTimeline.EVENT_REDACTED, handleEventRedact);
|
2021-08-04 12:52:59 +03:00
|
|
|
};
|
2021-12-03 15:02:10 +02:00
|
|
|
}, [roomTimeline]);
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-04 16:04:22 +02:00
|
|
|
useEffect(() => {
|
2021-12-03 15:02:10 +02:00
|
|
|
if (!roomTimeline.initialized) return;
|
2021-12-07 17:34:07 +02:00
|
|
|
if (timelineScroll.bottom < 16
|
|
|
|
&& !roomTimeline.canPaginateForward()
|
|
|
|
&& document.visibilityState === 'visible') {
|
2021-12-03 15:02:10 +02:00
|
|
|
timelineScroll.scrollToBottom();
|
|
|
|
}
|
|
|
|
}, [newEvent, roomTimeline]);
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
function RoomViewContent({ eventId, roomTimeline }) {
|
2021-12-03 15:02:10 +02:00
|
|
|
const timelineSVRef = useRef(null);
|
|
|
|
const readEventStore = useStore(roomTimeline);
|
2021-12-07 17:34:07 +02:00
|
|
|
const timelineInfo = useTimeline(roomTimeline, eventId, readEventStore);
|
2021-12-04 16:04:22 +02:00
|
|
|
const [onLimitUpdate, forceUpdateLimit] = useForceUpdate();
|
2021-12-07 17:34:07 +02:00
|
|
|
const [paginateInfo, autoPaginate] = usePaginate(roomTimeline, readEventStore, forceUpdateLimit);
|
|
|
|
const [handleScroll, handleScrollToLive] = useHandleScroll(
|
|
|
|
roomTimeline, autoPaginate, readEventStore, forceUpdateLimit,
|
|
|
|
);
|
|
|
|
useEventArrive(roomTimeline, readEventStore);
|
2021-12-03 15:02:10 +02:00
|
|
|
const { timeline } = roomTimeline;
|
|
|
|
|
2021-08-04 12:52:59 +03:00
|
|
|
useLayoutEffect(() => {
|
2021-12-03 15:02:10 +02:00
|
|
|
if (!roomTimeline.initialized) {
|
|
|
|
timelineScroll = new TimelineScroll(timelineSVRef.current);
|
2021-11-18 10:02:12 +02:00
|
|
|
}
|
2021-12-04 16:04:22 +02:00
|
|
|
});
|
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
// when active timeline changes
|
2021-12-04 16:04:22 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!roomTimeline.initialized) return undefined;
|
2021-12-03 15:02:10 +02:00
|
|
|
|
|
|
|
if (timeline.length > 0) {
|
2021-12-07 17:34:07 +02:00
|
|
|
if (jumpToItemIndex === -1) {
|
|
|
|
timelineScroll.scrollToBottom();
|
|
|
|
} else {
|
|
|
|
timelineScroll.scrollToIndex(jumpToItemIndex, 80);
|
|
|
|
}
|
|
|
|
if (timelineScroll.bottom < 16 && !roomTimeline.canPaginateForward()) {
|
2021-12-10 08:15:43 +02:00
|
|
|
const readUpToId = roomTimeline.getReadUpToEventId();
|
|
|
|
if (readEventStore.getItem()?.getId() === readUpToId || readUpToId === null) {
|
2021-12-07 17:34:07 +02:00
|
|
|
requestAnimationFrame(() => roomTimeline.markAllAsRead());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jumpToItemIndex = -1;
|
2021-11-18 10:02:12 +02:00
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
autoPaginate();
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
timelineScroll.on('scroll', handleScroll);
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.on(cons.events.roomTimeline.SCROLL_TO_LIVE, handleScrollToLive);
|
2021-12-03 15:02:10 +02:00
|
|
|
return () => {
|
|
|
|
if (timelineSVRef.current === null) return;
|
|
|
|
timelineScroll.removeListener('scroll', handleScroll);
|
2021-12-07 17:34:07 +02:00
|
|
|
roomTimeline.removeListener(cons.events.roomTimeline.SCROLL_TO_LIVE, handleScrollToLive);
|
2021-12-03 15:02:10 +02:00
|
|
|
};
|
|
|
|
}, [timelineInfo]);
|
2021-11-21 11:00:21 +02:00
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
// when paginating from server
|
2021-12-04 16:04:22 +02:00
|
|
|
useEffect(() => {
|
2021-12-03 15:02:10 +02:00
|
|
|
if (!roomTimeline.initialized) return;
|
|
|
|
timelineScroll.tryRestoringScroll();
|
|
|
|
autoPaginate();
|
|
|
|
}, [paginateInfo]);
|
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
// when paginating locally
|
2021-12-04 16:04:22 +02:00
|
|
|
useEffect(() => {
|
2021-12-04 11:55:14 +02:00
|
|
|
if (!roomTimeline.initialized) return;
|
|
|
|
timelineScroll.tryRestoringScroll();
|
|
|
|
}, [onLimitUpdate]);
|
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
const handleTimelineScroll = (event) => {
|
|
|
|
const { target } = event;
|
|
|
|
if (!target) return;
|
2021-12-04 11:55:14 +02:00
|
|
|
throttle._(() => timelineScroll?.calcScroll(), 400)(target);
|
2021-11-21 11:00:21 +02:00
|
|
|
};
|
|
|
|
|
2021-11-18 10:02:12 +02:00
|
|
|
const renderTimeline = () => {
|
|
|
|
const tl = [];
|
2021-12-03 15:02:10 +02:00
|
|
|
|
2021-12-07 17:34:07 +02:00
|
|
|
let itemCountIndex = 0;
|
|
|
|
jumpToItemIndex = -1;
|
|
|
|
const readEvent = readEventStore.getItem();
|
2021-12-13 07:33:48 +02:00
|
|
|
let unreadDivider = false;
|
2021-12-03 15:02:10 +02:00
|
|
|
|
2021-12-04 11:55:14 +02:00
|
|
|
if (roomTimeline.canPaginateBackward() || limit.from > 0) {
|
2021-12-03 15:02:10 +02:00
|
|
|
tl.push(loadingMsgPlaceholders(1, PLACEHOLDER_COUNT));
|
2021-12-07 17:34:07 +02:00
|
|
|
itemCountIndex += PLACEHOLDER_COUNT;
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-12-04 11:55:14 +02:00
|
|
|
for (let i = limit.from; i < limit.getEndIndex(); i += 1) {
|
|
|
|
if (i >= timeline.length) break;
|
2021-12-03 15:02:10 +02:00
|
|
|
const mEvent = timeline[i];
|
|
|
|
const prevMEvent = timeline[i - 1] ?? null;
|
|
|
|
|
|
|
|
if (i === 0 && !roomTimeline.canPaginateBackward()) {
|
|
|
|
if (mEvent.getType() === 'm.room.create') {
|
|
|
|
tl.push(genRoomIntro(mEvent, roomTimeline));
|
2021-12-07 17:34:07 +02:00
|
|
|
itemCountIndex += 1;
|
2021-12-03 15:02:10 +02:00
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
tl.push(genRoomIntro(undefined, roomTimeline));
|
2021-12-07 17:34:07 +02:00
|
|
|
itemCountIndex += 1;
|
2021-11-18 10:02:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 17:34:07 +02:00
|
|
|
|
2021-12-13 07:33:48 +02:00
|
|
|
unreadDivider = (readEvent && !unreadDivider
|
2021-12-03 15:02:10 +02:00
|
|
|
&& prevMEvent?.getTs() <= readEvent.getTs()
|
|
|
|
&& readEvent.getTs() < mEvent.getTs());
|
|
|
|
if (unreadDivider) {
|
2021-12-07 17:34:07 +02:00
|
|
|
tl.push(<Divider key={`new-${mEvent.getId()}`} variant="positive" text="New messages" />);
|
|
|
|
itemCountIndex += 1;
|
|
|
|
if (jumpToItemIndex === -1) jumpToItemIndex = itemCountIndex;
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-12-08 10:07:25 +02:00
|
|
|
const dayDivider = prevMEvent && !isInSameDay(mEvent.getDate(), prevMEvent.getDate());
|
2021-12-03 15:02:10 +02:00
|
|
|
if (dayDivider) {
|
|
|
|
tl.push(<Divider key={`divider-${mEvent.getId()}`} text={`${dateFormat(mEvent.getDate(), 'mmmm dd, yyyy')}`} />);
|
2021-12-07 17:34:07 +02:00
|
|
|
itemCountIndex += 1;
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-12-07 17:34:07 +02:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
const focusId = timelineInfo.focusEventId;
|
2021-12-07 17:34:07 +02:00
|
|
|
const isFocus = focusId === mEvent.getId();
|
|
|
|
if (isFocus) jumpToItemIndex = itemCountIndex;
|
2021-12-03 15:02:10 +02:00
|
|
|
|
|
|
|
tl.push(renderEvent(roomTimeline, mEvent, prevMEvent, isFocus));
|
2021-12-07 17:34:07 +02:00
|
|
|
itemCountIndex += 1;
|
2021-12-03 15:02:10 +02:00
|
|
|
}
|
2021-12-04 11:55:14 +02:00
|
|
|
if (roomTimeline.canPaginateForward() || limit.getEndIndex() < timeline.length) {
|
2021-12-03 15:02:10 +02:00
|
|
|
tl.push(loadingMsgPlaceholders(2, PLACEHOLDER_COUNT));
|
2021-11-18 10:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tl;
|
|
|
|
};
|
|
|
|
|
2021-08-04 12:52:59 +03:00
|
|
|
return (
|
2021-12-03 15:02:10 +02:00
|
|
|
<ScrollView onScroll={handleTimelineScroll} ref={timelineSVRef} autoHide>
|
|
|
|
<div className="room-view__content" onClick={handleOnClickCapture}>
|
|
|
|
<div className="timeline__wrapper">
|
|
|
|
{ roomTimeline.initialized ? renderTimeline() : loadingMsgPlaceholders('loading', 3) }
|
|
|
|
</div>
|
2021-08-04 12:52:59 +03:00
|
|
|
</div>
|
2021-12-03 15:02:10 +02:00
|
|
|
</ScrollView>
|
2021-08-04 12:52:59 +03:00
|
|
|
);
|
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
|
|
|
|
RoomViewContent.defaultProps = {
|
|
|
|
eventId: null,
|
|
|
|
};
|
2021-08-31 16:13:31 +03:00
|
|
|
RoomViewContent.propTypes = {
|
2021-12-03 15:02:10 +02:00
|
|
|
eventId: PropTypes.string,
|
2021-08-04 12:52:59 +03:00
|
|
|
roomTimeline: PropTypes.shape({}).isRequired,
|
|
|
|
};
|
|
|
|
|
2021-08-31 16:13:31 +03:00
|
|
|
export default RoomViewContent;
|