@@ -3,15 +3,16 @@ import { useAPIResult, InfiniteScroll, LoadingPlaceholder } from "@macrostrat/ui
33import { SETTINGS } from "@macrostrat-web/settings" ;
44import { PageHeader , Link , AssistantLinks , DevLinkButton , LinkCard , PageBreadcrumbs } from "~/components" ;
55import { Card , Icon , Spinner , RangeSlider } from "@blueprintjs/core" ;
6- import { useState , useEffect } from "react" ;
6+ import { useState , useEffect , useRef } from "react" ;
77import { ContentPage } from "~/layouts" ;
88import { Loading } from "../../index" ;
99
1010export function Page ( ) {
1111 const [ input , setInput ] = useState ( "" ) ;
1212 const [ lastID , setLastID ] = useState ( 0 ) ;
1313 const [ data , setData ] = useState ( [ ] ) ;
14- const result = useStratData ( lastID , input ) ;
14+ const pageSize = 20 ;
15+ const result = useStratData ( lastID , input , pageSize ) ;
1516
1617 useEffect ( ( ) => {
1718 if ( result ) {
@@ -43,7 +44,7 @@ export function Page() {
4344 h ( Icon , { icon : "search" } ) ,
4445 h ( 'input' , {
4546 type : "text" ,
46- placeholder : "Filter by name, subgroup, group, or rank ..." ,
47+ placeholder : "Filter by name..." ,
4748 onChange : handleChange ,
4849 } ) ,
4950 ] )
@@ -55,13 +56,7 @@ export function Page() {
5556 )
5657 )
5758 ) ,
58- h ( 'div.page-loader-container' , [
59- h ( 'h4' , {
60- onClick : ( ) => {
61- setLastID ( data . length > 0 ? data [ data . length - 1 ] . strat_name_id : lastID ) ;
62- }
63- } , "Load More" ) ,
64- ] )
59+ LoadMoreTrigger ( { data, setLastID, pageSize, result } ) ,
6560 ] ) ;
6661}
6762
@@ -71,7 +66,29 @@ function StratItem({ data }) {
7166 return h ( LinkCard , { href : "/lex/strat-names/" + strat_name_id } , strat_name )
7267}
7368
74- function useStratData ( lastID , input ) {
75- const result = useAPIResult ( `${ SETTINGS . apiV2Prefix } /defs/strat_names?page_size=20 &last_id=${ lastID } &strat_name_like=${ input } ` ) ;
69+ function useStratData ( lastID , input , pageSize ) {
70+ const result = useAPIResult ( `${ SETTINGS . apiV2Prefix } /defs/strat_names?page_size=${ pageSize } &last_id=${ lastID } &strat_name_like=${ input } ` ) ;
7671 return result ?. success ?. data ;
7772}
73+
74+ function LoadMoreTrigger ( { data, setLastID, pageSize, result } ) {
75+ const ref = useRef ( null ) ;
76+
77+ useEffect ( ( ) => {
78+ if ( ! ref . current ) return ;
79+
80+ const observer = new IntersectionObserver ( ( [ entry ] ) => {
81+ if ( entry . isIntersecting ) {
82+ if ( data . length > 0 ) {
83+ setLastID ( data [ data . length - 1 ] . strat_name_id ) ;
84+ }
85+ }
86+ } ) ;
87+
88+ observer . observe ( ref . current ) ;
89+
90+ return ( ) => observer . disconnect ( ) ;
91+ } , [ data , setLastID ] ) ;
92+
93+ return h . if ( result ?. length == pageSize ) ( 'div.load-more' , { ref } , h ( Spinner ) ) ;
94+ }
0 commit comments