Skip to content

Commit

Permalink
Add performance improvement for marking posts as read (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Jul 8, 2023
1 parent c5c4976 commit 9573c71
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/features/post/detail/PostDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IonSpinner,
IonTitle,
IonToolbar,
useIonViewDidEnter,
} from "@ionic/react";
import { useAppDispatch, useAppSelector } from "../../../store";
import { useParams } from "react-router";
Expand Down Expand Up @@ -140,17 +141,27 @@ export default function PostDetail() {
const { presentLoginIfNeeded, presentCommentReply } = useContext(PageContext);
const [commentsLastUpdated, setCommentsLastUpdated] = useState(Date.now());
const [sort, setSort] = useState<CommentSortType>("Hot");
const [ionViewEntered, setIonViewEntered] = useState(false);

useEffect(() => {
if (post) {
dispatch(setPostRead(+id));

return;
}
if (post) return;

dispatch(getPost(+id));
}, [post, jwt, dispatch, id]);

// Avoid rerender from marking a post as read until the page
// has fully transitioned in.
// This keeps the page transition as performant as possible
useEffect(() => {
if (!post || !ionViewEntered) return;

dispatch(setPostRead(+id));
}, [post, ionViewEntered, dispatch, id]);

useIonViewDidEnter(() => {
setIonViewEntered(true);
});

useEffect(() => {
titleRef.current?.scrollIntoView({ behavior: "smooth" });
}, [collapsed]);
Expand Down

0 comments on commit 9573c71

Please sign in to comment.