Augment the custom event types and make the dep arrays honest. [skip build]
Checks / checks (push) Successful in 2m7s
Trigger cinny-mobile / dispatch (push) Successful in 2s

This commit is contained in:
2026-11-02 05:12:35 +11:00
parent b85a2b9290
commit fea879497c
14 changed files with 64 additions and 51 deletions
+17 -11
View File
@@ -1,17 +1,16 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { MatrixClient, MatrixEvent, Room, RoomEvent, IRoomTimelineData } from 'matrix-js-sdk';
/**
* Hook that tracks timeline events across all rooms to trigger re-sorting of room lists.
* Returns a counter that increments whenever a message is sent or received in any room.
* This can be used as a dependency in useMemo to trigger re-sorting of room lists.
* Hook that tracks timeline events across all rooms to order room lists by activity.
* Returns the room IDs sorted by last active timestamp, recomputed on each live timeline event.
*
* @param mx - The Matrix client instance
* @param roomIds - Optional array of room IDs to track. If not provided, tracks all rooms.
* @returns A number that increments on each timeline event
* @param roomIds - Room IDs to track
* @returns Room IDs ordered by most recently active
*/
export const useRoomListReorder = (mx: MatrixClient, roomIds?: string[]): number => {
const [updateCounter, setUpdateCounter] = useState(0);
export const useRoomListReorder = (mx: MatrixClient, roomIds: string[]): string[] => {
const [lastActivity, setLastActivity] = useState<Map<string, number>>(new Map());
useEffect(() => {
const handleTimelineEvent = (
@@ -24,9 +23,9 @@ export const useRoomListReorder = (mx: MatrixClient, roomIds?: string[]): number
if (!room || !data.liveEvent) return;
if (toStartOfTimeline) return;
if (roomIds && !roomIds.includes(room.roomId)) return;
if (!roomIds.includes(room.roomId)) return;
setUpdateCounter((prev) => prev + 1);
setLastActivity((prev) => new Map(prev).set(room.roomId, room.getLastActiveTimestamp()));
};
mx.on(RoomEvent.Timeline, handleTimelineEvent);
@@ -36,7 +35,14 @@ export const useRoomListReorder = (mx: MatrixClient, roomIds?: string[]): number
};
}, [mx, roomIds]);
return updateCounter;
return useMemo(() => {
const activityOf = (roomId: string) =>
lastActivity.get(roomId) ??
mx.getRoom(roomId)?.getLastActiveTimestamp() ??
Number.MIN_SAFE_INTEGER;
return [...roomIds].sort((a, b) => activityOf(b) - activityOf(a));
}, [mx, roomIds, lastActivity]);
};
/**