-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/extend-collection-detail' into fix/merge-conflicts…
…-16-01/2
- Loading branch information
Showing
10 changed files
with
368 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
ui/src/components/Collection/CollectionDetail/Exclusions/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import OverviewContent, { IPlexMetadata } from '../../../Overview/Content' | ||
import _ from 'lodash' | ||
import { ICollection, ICollectionMedia } from '../..' | ||
import GetApiHandler from '../../../../utils/ApiHandler' | ||
|
||
interface ICollectionExclusions { | ||
collection: ICollection | ||
libraryId: number | ||
} | ||
|
||
const CollectionExcludions = (props: ICollectionExclusions) => { | ||
const [data, setData] = useState<IPlexMetadata[]>([]) | ||
const [media, setMedia] = useState<ICollectionMedia[]>([]) | ||
// paging | ||
const pageData = useRef<number>(0) | ||
const fetchAmount = 25 | ||
const [totalSize, setTotalSize] = useState<number>(999) | ||
const totalSizeRef = useRef<number>(999) | ||
const dataRef = useRef<IPlexMetadata[]>([]) | ||
const mediaRef = useRef<ICollectionMedia[]>([]) | ||
const loadingRef = useRef<boolean>(true) | ||
const loadingExtraRef = useRef<boolean>(false) | ||
const [page, setPage] = useState(0) | ||
|
||
useEffect(() => { | ||
// Initial first fetch | ||
setPage(1) | ||
}, []) | ||
|
||
const handleScroll = () => { | ||
if ( | ||
window.innerHeight + document.documentElement.scrollTop >= | ||
document.documentElement.scrollHeight * 0.9 | ||
) { | ||
if ( | ||
!loadingRef.current && | ||
!loadingExtraRef.current && | ||
!(fetchAmount * (pageData.current - 1) >= totalSizeRef.current) | ||
) { | ||
setPage(pageData.current + 1) | ||
} | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (page !== 0) { | ||
// Ignore initial page render | ||
pageData.current = pageData.current + 1 | ||
fetchData() | ||
} | ||
}, [page]) | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', _.debounce(handleScroll.bind(this), 200)) | ||
return () => { | ||
window.removeEventListener( | ||
'scroll', | ||
_.debounce(handleScroll.bind(this), 200), | ||
) | ||
} | ||
}, []) | ||
|
||
const fetchData = async () => { | ||
if (!loadingRef.current) { | ||
loadingExtraRef.current = true | ||
} | ||
// setLoading(true) | ||
const resp: { totalSize: number; items: ICollectionMedia[] } = | ||
await GetApiHandler( | ||
`/collections/exclusions/${props.collection.id}/content/${pageData.current}?size=${fetchAmount}`, | ||
) | ||
|
||
setTotalSize(resp.totalSize) | ||
// pageData.current = pageData.current + 1 | ||
setMedia([...mediaRef.current, ...resp.items]) | ||
|
||
setData([ | ||
...dataRef.current, | ||
...resp.items.map((el) => | ||
el.plexData ? el.plexData : ({} as IPlexMetadata), | ||
), | ||
]) | ||
loadingRef.current = false | ||
loadingExtraRef.current = false | ||
} | ||
|
||
useEffect(() => { | ||
dataRef.current = data | ||
|
||
// If page is not filled yet, fetch more | ||
if ( | ||
!loadingRef.current && | ||
!loadingExtraRef.current && | ||
window.innerHeight + document.documentElement.scrollTop >= | ||
document.documentElement.scrollHeight * 0.9 && | ||
!(fetchAmount * (pageData.current - 1) >= totalSizeRef.current) | ||
) { | ||
setPage(page + 1) | ||
} | ||
}, [data]) | ||
|
||
useEffect(() => { | ||
mediaRef.current = media | ||
}, [media]) | ||
|
||
useEffect(() => { | ||
totalSizeRef.current = totalSize | ||
}, [totalSize]) | ||
|
||
return ( | ||
<OverviewContent | ||
dataFinished={true} | ||
fetchData={() => {}} | ||
loading={loadingRef.current} | ||
data={data} | ||
libraryId={props.libraryId} | ||
collectionPage={true} | ||
extrasLoading={ | ||
loadingExtraRef && | ||
!loadingRef.current && | ||
totalSize >= pageData.current * fetchAmount | ||
} | ||
onRemove={(id: string) => | ||
setTimeout(() => { | ||
setData(dataRef.current.filter((el) => +el.ratingKey !== +id)) | ||
setMedia(mediaRef.current.filter((el) => +el.plexId !== +id)) | ||
}, 500) | ||
} | ||
collectionInfo={media.map((el) => { | ||
props.collection.media = [] | ||
el.collection = props.collection | ||
return el | ||
})} | ||
/> | ||
) | ||
} | ||
export default CollectionExcludions |
Oops, something went wrong.