web/roomview: scroll timeline to bottom on resize

This commit is contained in:
Tulir Asokan 2024-12-15 14:14:20 +02:00
parent 0512945c57
commit b352255d14
3 changed files with 16 additions and 7 deletions

View file

@ -13,7 +13,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { JSX, useRef } from "react" import { JSX, useEffect, useState } from "react"
import { RoomStateStore } from "@/api/statestore" import { RoomStateStore } from "@/api/statestore"
import MessageComposer from "../composer/MessageComposer.tsx" import MessageComposer from "../composer/MessageComposer.tsx"
import TypingNotifications from "../composer/TypingNotifications.tsx" import TypingNotifications from "../composer/TypingNotifications.tsx"
@ -30,11 +30,18 @@ interface RoomViewProps {
} }
const RoomView = ({ room, rightPanelResizeHandle, rightPanel }: RoomViewProps) => { const RoomView = ({ room, rightPanelResizeHandle, rightPanel }: RoomViewProps) => {
const roomContextDataRef = useRef<RoomContextData | undefined>(undefined) const [roomContextData] = useState(() => new RoomContextData(room))
if (roomContextDataRef.current === undefined) { useEffect(() => {
roomContextDataRef.current = new RoomContextData(room) window.activeRoomContext = roomContextData
} window.addEventListener("resize", roomContextData.scrollToBottom)
return <RoomContext value={roomContextDataRef.current}> return () => {
window.removeEventListener("resize", roomContextData.scrollToBottom)
if (window.activeRoomContext === roomContextData) {
window.activeRoomContext = undefined
}
}
}, [roomContextData])
return <RoomContext value={roomContextData}>
<div className="room-view"> <div className="room-view">
<RoomViewHeader room={room}/> <RoomViewHeader room={room}/>
<TimelineView/> <TimelineView/>

View file

@ -34,7 +34,7 @@ export class RoomContextData {
constructor(public store: RoomStateStore) {} constructor(public store: RoomStateStore) {}
scrollToBottom() { scrollToBottom = () => {
if (this.scrolledToBottom) { if (this.scrolledToBottom) {
this.timelineBottomRef.current?.scrollIntoView() this.timelineBottomRef.current?.scrollIntoView()
} }

View file

@ -4,11 +4,13 @@
import type Client from "@/api/client.ts" import type Client from "@/api/client.ts"
import type { GCSettings, RoomStateStore } from "@/api/statestore" import type { GCSettings, RoomStateStore } from "@/api/statestore"
import type { MainScreenContextFields } from "@/ui/MainScreenContext.ts" import type { MainScreenContextFields } from "@/ui/MainScreenContext.ts"
import type { RoomContextData } from "@/ui/roomview/roomcontext.ts"
declare global { declare global {
interface Window { interface Window {
client: Client client: Client
activeRoom?: RoomStateStore | null activeRoom?: RoomStateStore | null
activeRoomContext?: RoomContextData
mainScreenContext: MainScreenContextFields mainScreenContext: MainScreenContextFields
openLightbox: (params: { src: string, alt: string }) => void openLightbox: (params: { src: string, alt: string }) => void
gcSettings: GCSettings gcSettings: GCSettings