From 7c7f8131182e2479a37affd6e45a57eb3981223c Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 7 Aug 2021 20:53:17 +0100 Subject: [PATCH] Scroll to last position when re-opening a chat. --- src/lib/renderer/Singleton.ts | 3 ++- src/pages/channels/messaging/MessageArea.tsx | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/renderer/Singleton.ts b/src/lib/renderer/Singleton.ts index 1f52a6c1..7f51fe56 100644 --- a/src/lib/renderer/Singleton.ts +++ b/src/lib/renderer/Singleton.ts @@ -4,7 +4,6 @@ import { Channel } from "revolt.js/dist/maps/Channels"; import { Message } from "revolt.js/dist/maps/Messages"; import { Nullable } from "revolt.js/dist/util/null"; -import { defer } from "../defer"; import { SimpleRenderer } from "./simple/SimpleRenderer"; import { RendererRoutines, ScrollState } from "./types"; @@ -23,6 +22,7 @@ export class ChannelRenderer { stale = false; fetching = false; + scrollPosition = 0; constructor(channel: Channel) { this.channel = channel; @@ -30,6 +30,7 @@ export class ChannelRenderer { makeAutoObservable(this, { channel: false, currentRenderer: false, + scrollPosition: false, }); this.receive = this.receive.bind(this); diff --git a/src/pages/channels/messaging/MessageArea.tsx b/src/pages/channels/messaging/MessageArea.tsx index dc9bd6dd..3577c967 100644 --- a/src/pages/channels/messaging/MessageArea.tsx +++ b/src/pages/channels/messaging/MessageArea.tsx @@ -173,7 +173,7 @@ export const MessageArea = observer(({ channel }: Props) => { if (message) return; if (renderer.state === "RENDER") { runInAction(() => (renderer.fetching = true)); - setScrollState({ type: "ScrollTop", y: 151 }); + setScrollState({ type: "ScrollTop", y: renderer.scrollPosition }); } else { renderer.init(); } @@ -245,12 +245,14 @@ export const MessageArea = observer(({ channel }: Props) => { if (!current) return; async function onScroll() { + renderer.scrollPosition = current!.scrollTop; + if (atTop(100)) { - renderer.loadTop(ref.current!); + renderer.loadTop(current!); } if (atBottom(100)) { - renderer.loadBottom(ref.current!); + renderer.loadBottom(current!); } }