11import React from 'react' ;
22import { observer } from "mobx-react-lite" ;
3- import { useState , useEffect , useRef } from 'react' ;
3+ import { useState , useEffect , useRef , useMemo } from 'react' ;
44import ListView from "../views/ListView.jsx" ;
55import CoursePagePopup from '../views/Components/CoursePagePopup.jsx' ;
66import PrerequisitePresenter from './PrerequisitePresenter.jsx' ;
77import { ReviewPresenter } from "./ReviewPresenter.jsx"
88import { syncScrollPositionToFirebase } from "../../firebase.js"
9- import { model } from '../model.js ' ;
9+ import { useCallback } from 'react ' ;
1010
1111const ListViewPresenter = observer ( ( { model } ) => {
1212 const scrollContainerRef = useRef ( null ) ;
@@ -50,12 +50,12 @@ const ListViewPresenter = observer(({ model }) => {
5050 if ( savedPosition ) {
5151 model . setScrollPosition ( parseInt ( savedPosition , 10 ) ) ;
5252 }
53- } , [ model . user ] ) ;
53+ } , [ model , model . user ] ) ;
5454
5555 useEffect ( ( ) => {
5656 const cleanup = syncScrollPositionToFirebase ( model , scrollContainerRef ) ;
5757 return ( ) => cleanup ;
58- } , [ model . user , model . currentSearch , scrollContainerRef ] ) ;
58+ } , [ model , model . user , model . currentSearch , scrollContainerRef ] ) ;
5959
6060 const addFavourite = ( course ) => {
6161 model . addFavourite ( course ) ;
@@ -77,9 +77,8 @@ const ListViewPresenter = observer(({ model }) => {
7777
7878 const [ sortBy , setSortBy ] = useState ( 'relevance' ) ;
7979 const [ sortDirection , setSortDirection ] = useState ( 'desc' ) ;
80- const [ sortedCourses , setSortedCourses ] = useState ( [ ] ) ;
8180
82- const sortCourses = ( courses , sortType ) => {
81+ const sortCourses = useCallback ( ( courses , sortType ) => {
8382 if ( ! courses ) return [ ] ;
8483
8584 const sortedCourses = [ ...courses ] ;
@@ -143,12 +142,12 @@ const ListViewPresenter = observer(({ model }) => {
143142 default :
144143 return direction === 1 ? sortedCourses : sortedCourses . reverse ( ) ;
145144 }
146- } ;
145+ } , [ sortDirection , model . avgRatings ] ) ;
147146
148- useEffect ( ( ) => {
149- const sorted = sortCourses ( model . currentSearch , sortBy ) ;
150- setSortedCourses ( sorted ) ;
151- } , [ model . currentSearch , sortBy , sortDirection ] ) ;
147+ const sortedCourses = useMemo ( ( ) => {
148+ if ( ! model . currentSearch ) return [ ] ;
149+ return sortCourses ( model . currentSearch , sortBy ) ;
150+ } , [ model . currentSearch , sortBy , sortCourses ] ) ;
152151
153152
154153 function indexOfNth ( string , char , n ) {
@@ -167,8 +166,8 @@ const ListViewPresenter = observer(({ model }) => {
167166 window . addEventListener ( 'popstate' , ( ) => {
168167 model . handleUrlChange ( ) ;
169168 } ) ;
170-
171- model . onCoursesSet ( ( courses ) => {
169+
170+ model . onCoursesSet ( ( courses ) => { // eslint-disable-line no-unused-vars
172171 let current_url = window . location . href ;
173172 if ( current_url . indexOf ( "#" ) != - 1 ) { return ; }
174173 let course_code = "" ;
0 commit comments