Merge pull request #10089 from overleaf/ii-project-list-provider-effects-refactoring

[web] Projects list provider refactoring

GitOrigin-RevId: e53fcdd32e5493d438d46f4a1bbca049a1967f5c
This commit is contained in:
ilkin-overleaf 2022-10-21 14:33:27 +03:00 committed by Copybot
parent 185ccde5a0
commit 82c6484ca1

View file

@ -15,7 +15,6 @@ import {
useContext, useContext,
useEffect, useEffect,
useMemo, useMemo,
useRef,
useState, useState,
} from 'react' } from 'react'
import { Tag } from '../../../../../app/src/Features/Tags/types' import { Tag } from '../../../../../app/src/Features/Tags/types'
@ -126,7 +125,6 @@ export function ProjectListProvider({ children }: ProjectListProviderProps) {
'project-list-filter', 'project-list-filter',
'all' 'all'
) )
const prevSortRef = useRef<Sort>(sort)
const [selectedTagId, setSelectedTagId] = usePersistedState< const [selectedTagId, setSelectedTagId] = usePersistedState<
string | undefined string | undefined
>('project-list-selected-tag-id', undefined) >('project-list-selected-tag-id', undefined)
@ -158,14 +156,27 @@ export function ProjectListProvider({ children }: ProjectListProviderProps) {
}) })
}, [prefetchedProjectsBlob, runAsync]) }, [prefetchedProjectsBlob, runAsync])
// Search related effects
useEffect(() => { useEffect(() => {
let filteredProjects = [...loadedProjects] if (!searchText.length) {
return
}
if (searchText.length) { const filteredProjects = loadedProjects.filter(project =>
filteredProjects = filteredProjects.filter(project =>
project.name.toLowerCase().includes(searchText.toLowerCase()) project.name.toLowerCase().includes(searchText.toLowerCase())
) )
}
setVisibleProjects(filteredProjects)
}, [searchText, loadedProjects])
// Sort related effects
useEffect(() => {
setLoadedProjects(loadedProjects => sortProjects(loadedProjects, sort))
}, [sort])
// Filters/Tags and load more related effects
useEffect(() => {
let filteredProjects = [...loadedProjects]
if (selectedTagId !== undefined) { if (selectedTagId !== undefined) {
if (selectedTagId === UNCATEGORIZED_KEY) { if (selectedTagId === UNCATEGORIZED_KEY) {
@ -191,12 +202,7 @@ export function ProjectListProvider({ children }: ProjectListProviderProps) {
filteredProjects = arrayFilter(filteredProjects, filters[filter]) filteredProjects = arrayFilter(filteredProjects, filters[filter])
} }
if (prevSortRef.current !== sort) { // load more
filteredProjects = sortProjects(filteredProjects, sort)
const loadedProjectsSorted = sortProjects(loadedProjects, sort)
setLoadedProjects(loadedProjectsSorted)
}
if (filteredProjects.length > maxVisibleProjects) { if (filteredProjects.length > maxVisibleProjects) {
const visibleFilteredProjects = filteredProjects.slice( const visibleFilteredProjects = filteredProjects.slice(
0, 0,
@ -220,21 +226,15 @@ export function ProjectListProvider({ children }: ProjectListProviderProps) {
setHiddenProjectsCount(0) setHiddenProjectsCount(0)
} }
}, [ }, [
filter,
loadedProjects, loadedProjects,
maxVisibleProjects, maxVisibleProjects,
tags,
filter,
setFilter,
selectedTagId, selectedTagId,
setFilter,
setSelectedTagId, setSelectedTagId,
searchText, tags,
sort,
]) ])
useEffect(() => {
prevSortRef.current = sort
}, [sort])
const showAllProjects = useCallback(() => { const showAllProjects = useCallback(() => {
setLoadMoreCount(0) setLoadMoreCount(0)
setHiddenProjectsCount(0) setHiddenProjectsCount(0)