Can load older messages in conversations
This commit is contained in:
@@ -36,6 +36,10 @@ export class RoomEventsManager {
|
|||||||
typingUsers: string[];
|
typingUsers: string[];
|
||||||
receiptsEventsMap: Map<string, Receipt[]>;
|
receiptsEventsMap: Map<string, Receipt[]>;
|
||||||
|
|
||||||
|
get canLoadOlder(): boolean {
|
||||||
|
return !!this.endToken;
|
||||||
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
room: Room,
|
room: Room,
|
||||||
initialMessages: MatrixEventsList,
|
initialMessages: MatrixEventsList,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
Button,
|
Button,
|
||||||
ButtonGroup,
|
ButtonGroup,
|
||||||
Chip,
|
Chip,
|
||||||
|
CircularProgress,
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogActions,
|
DialogActions,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
@@ -40,6 +41,10 @@ export function RoomMessagesList(p: {
|
|||||||
users: UsersMap;
|
users: UsersMap;
|
||||||
manager: RoomEventsManager;
|
manager: RoomEventsManager;
|
||||||
}): React.ReactElement {
|
}): React.ReactElement {
|
||||||
|
const snackbar = useSnackbar();
|
||||||
|
|
||||||
|
const [loadingOlder, setLoadingOlder] = React.useState(false);
|
||||||
|
|
||||||
const listContainerRef = React.createRef<HTMLDivElement>();
|
const listContainerRef = React.createRef<HTMLDivElement>();
|
||||||
const messagesEndRef = React.createRef<HTMLDivElement>();
|
const messagesEndRef = React.createRef<HTMLDivElement>();
|
||||||
|
|
||||||
@@ -50,8 +55,9 @@ export function RoomMessagesList(p: {
|
|||||||
}, [p.manager.messages.at(-1)?.event_id]);
|
}, [p.manager.messages.at(-1)?.event_id]);
|
||||||
|
|
||||||
// Watch scroll to detect when user reach the top to load older messages
|
// Watch scroll to detect when user reach the top to load older messages
|
||||||
const handleScroll = () => {
|
const handleScroll = async () => {
|
||||||
if (!listContainerRef.current) return;
|
if (!listContainerRef.current || loadingOlder || !p.manager.canLoadOlder)
|
||||||
|
return;
|
||||||
|
|
||||||
const { scrollTop } = listContainerRef.current;
|
const { scrollTop } = listContainerRef.current;
|
||||||
|
|
||||||
@@ -59,9 +65,20 @@ export function RoomMessagesList(p: {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("reached top");
|
setLoadingOlder(true);
|
||||||
// TODO : load old messages
|
|
||||||
// TODO : block when user reached begining of conversation
|
try {
|
||||||
|
const older = await MatrixApiEvent.GetRoomEvents(
|
||||||
|
p.room,
|
||||||
|
p.manager.endToken
|
||||||
|
);
|
||||||
|
p.manager.processNewEvents(older);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to load older messages!", e);
|
||||||
|
snackbar(`Failed to load older messages for conversation! ${e}`);
|
||||||
|
} finally {
|
||||||
|
setLoadingOlder(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -90,6 +107,31 @@ export function RoomMessagesList(p: {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/** Begining of conversation */}
|
||||||
|
{!p.manager.canLoadOlder && (
|
||||||
|
<Typography
|
||||||
|
component={"div"}
|
||||||
|
variant="caption"
|
||||||
|
style={{ textAlign: "center", marginTop: "10px" }}
|
||||||
|
>
|
||||||
|
Begining of conversation
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/** Loading older messages spinner */}
|
||||||
|
{loadingOlder && (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "inline-flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
width: "100%",
|
||||||
|
padding: "20px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<CircularProgress />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{p.manager.messages.map((m, idx) => (
|
{p.manager.messages.map((m, idx) => (
|
||||||
<RoomMessage
|
<RoomMessage
|
||||||
key={m.event_id}
|
key={m.event_id}
|
||||||
|
|||||||
Reference in New Issue
Block a user