Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade dependencies #1724

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
upgrade virtua
  • Loading branch information
aeharding committed Nov 15, 2024
commit af9ffd3e23412c746bb7e6f3f8013c56bd0e780a
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"use-long-press": "^3.2.0",
"virtua": "^0.36.3"
"virtua": "^0.37.0"
},
"devDependencies": {
"@babel/preset-react": "^7.25.9",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/features/comment/inTree/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { VList, VListHandle } from "virtua";

import { useSetActivePage } from "#/features/auth/AppContext";
import FeedLoadMoreFailed from "#/features/feed/endItems/FeedLoadMoreFailed";
import { useRangeChange } from "#/features/feed/useRangeChange";
import { getPost } from "#/features/post/postSlice";
import { defaultCommentDepthSelector } from "#/features/settings/settingsSlice";
import { scrollIntoView, useScrollIntoViewWorkaround } from "#/helpers/dom";
Expand Down Expand Up @@ -490,6 +491,12 @@ export default function Comments({
[allComments, bottomPadding, header, renderFooter],
);

const onScroll = useRangeChange(virtuaRef, (start, end) => {
if (end + 10 > allComments.length && !loadFailed) {
fetchComments();
}
});

return (
<CommentsContext.Provider value={commentsContextValue}>
<IonRefresher
Expand All @@ -512,12 +519,8 @@ export default function Comments({
// @ts-expect-error Virtua types not updated for forwardRef-less components
item={IndexedVirtuaItem}
overscan={1}
onRangeChange={(start, end) => {
if (end + 10 > allComments.length && !loadFailed) {
fetchComments();
}
}}
onScroll={(offset) => {
onScroll();
setIsListAtTop(offset < 6);
}}
>
Expand Down
61 changes: 32 additions & 29 deletions src/features/feed/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useAppSelector } from "#/store";
import EndPost, { EndPostProps } from "./endItems/EndPost";
import FeedLoadMoreFailed from "./endItems/FeedLoadMoreFailed";
import FetchMore from "./endItems/FetchMore";
import { useRangeChange } from "./useRangeChange";

const ABORT_REASON_UNMOUNT = "unmount";

Expand Down Expand Up @@ -258,6 +259,35 @@ export default function Feed<I>({

useSetActivePage(virtuaHandle);

const onScroll = useRangeChange(
virtuaHandle,
function onRangeChange(start, end) {
if (start < 0 || end < 0 || (!start && !end)) return; // no items rendered

// if scrolled down
const startOffset = header ? 1 : 0; // header counts as item to VList
if (
scrollingRef.current &&
start > startOffset &&
start > startRangeRef.current
) {
// emit what was removed
onRemovedFromTop?.(
filteredItems.slice(
startRangeRef.current - startOffset,
start - startOffset,
),
);
}

startRangeRef.current = start;

if (end + 10 > filteredItems.length && !loadFailed && infiniteScrolling) {
fetchMore();
}
},
);

const fetchMoreEvent = useEffectEvent(fetchMore);

useEffect(() => {
Expand Down Expand Up @@ -330,39 +360,12 @@ export default function Feed<I>({
scrollingRef.current = false;
}}
onScroll={(offset) => {
onScroll();

scrollingRef.current = true;
setIsListAtTop(offset < 10);
setScrolledPastSearch(offset > 40);
}}
onRangeChange={(start, end) => {
if (start < 0 || end < 0 || (!start && !end)) return; // no items rendered

// if scrolled down
const startOffset = header ? 1 : 0; // header counts as item to VList
if (
scrollingRef.current &&
start > startOffset &&
start > startRangeRef.current
) {
// emit what was removed
onRemovedFromTop?.(
filteredItems.slice(
startRangeRef.current - startOffset,
start - startOffset,
),
);
}

startRangeRef.current = start;

if (
end + 10 > filteredItems.length &&
!loadFailed &&
infiniteScrolling
) {
fetchMore();
}
}}
/* Large posts reflow with image load, so mount to dom a bit sooner */
overscan={1}
>
Expand Down
28 changes: 28 additions & 0 deletions src/features/feed/useRangeChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useThrottledCallback } from "@mantine/hooks";
import { useRef } from "react";
import { VListHandle } from "virtua";

export function useRangeChange(
virtuaHandleRef: React.RefObject<VListHandle>,
onRangeChange: (startIndex: number, endIndex: number) => void,
) {
const startIndexRef = useRef(-1);
const endIndexRef = useRef(-1);

return useThrottledCallback(function onScroll() {
const virtuaHandle = virtuaHandleRef.current;

if (!virtuaHandle) return;
const startIndex = virtuaHandle.startIndex;
const endIndex = virtuaHandle.endIndex;

if (
startIndex !== startIndexRef.current ||
endIndex !== endIndexRef.current
) {
onRangeChange(startIndex, endIndex);
}
startIndexRef.current = startIndex;
endIndexRef.current = endIndex;
}, 200);
}