Skip to content

added scroll restoration on forward/backward navigation #1878

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 34 additions & 4 deletions src/components/FolderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ContentCard } from './ContentCard';
import { courseContent, getFilteredContent } from '@/lib/utils';
import { useRecoilValue } from 'recoil';
import { selectFilter } from '@/store/atoms/filterContent';
import { useEffect, useRef } from 'react';

export const FolderView = ({
courseContent,
Expand All @@ -29,15 +30,16 @@ export const FolderView = ({
}
// why? because we have to reset the segments or they will be visible always after a video

const currentfilter = useRecoilValue(selectFilter);
const currentfilter:string = useRecoilValue(selectFilter);

const filteredCourseContent = getFilteredContent(
courseContent,
currentfilter,
);

if (filteredCourseContent?.length === 0) {
const filterMessages = {
const filterMessages: Record<string, string> = {

watched: "You haven't completed any content in this section yet.",
watching: "No content currently in progress.",
unwatched: "No new content available to watch.",
Expand All @@ -52,7 +54,35 @@ export const FolderView = ({
</div>
);
}

const lastScrollPosition = useRef(0);

useEffect(() => {
const savedPosition = sessionStorage.getItem('scrollPosition');

if (savedPosition) {
// Add a small delay to ensure content is rendered
setTimeout(() => {
window.scrollTo(0, parseInt(savedPosition, 10),);
console.log("Restoring scroll position to:", savedPosition);
}, 100);
}
const handleScroll = () => {
// const currentPosition = windowRef.current ? windowRef.current.scrollTop : 0;
const currentPosition = window.scrollY;
lastScrollPosition.current = currentPosition;

sessionStorage.setItem('scrollPosition', currentPosition.toString());

if (currentPosition > lastScrollPosition.current) {
console.log("Scrolling down");
} else {
console.log("Scrolling up");
}
};
// if (windowRef.current)
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return (
<div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
Expand All @@ -72,7 +102,7 @@ export const FolderView = ({
title={content.title}
image={content.image || ''}
onClick={() => {
router.push(`${updatedRoute}/${content.id}`);
router.push(`${updatedRoute}/${content.id}`,{scroll: false});
}}
markAsCompleted={content.markAsCompleted}
percentComplete={content.percentComplete}
Expand Down
2 changes: 1 addition & 1 deletion src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { toast } from 'sonner';
import { createRoot } from 'react-dom/client';
import { PictureInPicture2 } from 'lucide-react';
import { AppxVideoPlayer } from './AppxVideoPlayer';
import HammerInput from 'hammerjs';

// todo correct types
interface VideoPlayerProps {
Expand Down Expand Up @@ -111,7 +112,6 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
};

const setupZoomFeatures = (player: any) => {

if (typeof window === 'undefined' || typeof document === 'undefined') return;

const videoEl = player.el().querySelector('video');
Expand Down
1 change: 0 additions & 1 deletion src/components/posts/form/form-vote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const VoteForm: React.FC<IVoteFormProps> = ({
);
};


const userVoted = Boolean(votesArr.length);
const userVoteVal = votesArr[0];

Expand Down
Loading