Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 1x 1x 1x 45x 45x 45x 26x 13x 45x 13x 2x 45x 34x 2x 2x 45x | import { debounce } from "@mui/material";
import { UseMutateFunction } from "@tanstack/react-query";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { useEffect, useMemo, useRef } from "react";
import { MARK_LAST_SEEN_TIMEOUT } from "./constants";
export type MarkLastSeenVariables = number;
export default function useMarkLastSeen(
markLastSeenMutate: UseMutateFunction<Empty, Error, MarkLastSeenVariables>,
lastSeenMessageId?: number,
) {
const maxMessageIdRef = useRef(0);
// Sync with latest lastSeenMessageId so anything below that ID doesn't get tried again.
// Needed since lastSeenMessageId comes from react query which is initially
// undefined so can't do useRef(lastSeenMessageId).
useEffect(() => {
if (lastSeenMessageId) {
maxMessageIdRef.current = Math.max(
maxMessageIdRef.current,
lastSeenMessageId,
);
}
}, [lastSeenMessageId]);
const debouncedMarkLastSeen = useMemo(
() =>
debounce((messageId: number) => {
markLastSeenMutate(messageId);
}, MARK_LAST_SEEN_TIMEOUT),
[markLastSeenMutate],
);
const markLastSeen = (messageId: number) => {
if (messageId > maxMessageIdRef.current) {
maxMessageIdRef.current = messageId;
debouncedMarkLastSeen(messageId);
}
};
return { markLastSeen };
}
|