11import h from "./main.module.scss" ;
2- import { useAPIResult } from "@macrostrat/ui-components" ;
2+ import { useAPIResult , InfiniteScroll , LoadingPlaceholder } from "@macrostrat/ui-components" ;
33import { SETTINGS } from "@macrostrat-web/settings" ;
44import { PageHeader , Link , AssistantLinks , DevLinkButton , LinkCard , PageBreadcrumbs } from "~/components" ;
5- import { Card , Icon , Popover , RangeSlider } from "@blueprintjs/core" ;
6- import { useState } from "react" ;
5+ import { Card , Icon , Spinner , RangeSlider } from "@blueprintjs/core" ;
6+ import { useState , useEffect , useRef } from "react" ;
77import { ContentPage } from "~/layouts" ;
8- import { usePageContext } from 'vike-react/usePageContext' ;
98import { Loading } from "../../index" ;
109
1110export function Page ( ) {
1211 const [ input , setInput ] = useState ( "" ) ;
13- const res = useAPIResult ( SETTINGS . apiV2Prefix + "/defs/strat_name_concepts?all" ) ?. success . data ;
12+ const [ lastID , setLastID ] = useState ( 0 ) ;
13+ const [ data , setData ] = useState ( [ ] ) ;
14+ const pageSize = 20 ;
15+ const result = useStratData ( lastID , input , pageSize ) ;
1416
15- if ( res == null ) return h ( Loading ) ;
17+ useEffect ( ( ) => {
18+ if ( result ) {
19+ setData ( prevData => [ ...prevData , ...result ] ) ;
20+ }
21+ } , [ result ] ) ;
22+
23+ useEffect ( ( ) => {
24+ // Example: Reset data if lastID changes
25+ setData ( [ ] ) ;
26+ } , [ input ] ) ;
27+
28+
29+ if ( data == null ) return h ( Loading ) ;
1630
1731 const handleChange = ( event ) => {
1832 setInput ( event . target . value . toLowerCase ( ) ) ;
1933 }
2034
2135 return h ( ContentPage , [
2236 h ( PageBreadcrumbs , { title : "Strat Name Concepts" } ) ,
23- /*
37+ h ( 'div.sift-link' , [
38+ h ( 'p' , "This page is is in development." ) ,
39+ h ( 'a' , { href : "/sift/definitions/strat_name_concepts" , target : "_blank" } , "View in Sift" )
40+ ] ) ,
2441 h ( Card , { className : 'filters' } , [
2542 h ( "h2" , 'Filter' ) ,
2643 h ( 'div.search-bar' , [
2744 h ( Icon , { icon : "search" } ) ,
2845 h ( 'input' , {
2946 type : "text" ,
30- placeholder: "Filter by name, subgroup, group, or rank ...",
47+ placeholder : "Filter by name..." ,
3148 onChange : handleChange ,
3249 } ) ,
3350 ] )
3451 ] ) ,
35- */
3652 h ( 'div.strat-list' ,
37- res . map ( ( item ) => h ( StratItem , { data : item } ) )
38- )
39- ] )
40-
53+ h ( 'div.strat-items' ,
54+ data . map ( data =>
55+ h ( StratItem , { data } )
56+ )
57+ )
58+ ) ,
59+ LoadMoreTrigger ( { data, setLastID, pageSize, result } ) ,
60+ ] ) ;
4161}
4262
4363function StratItem ( { data } ) {
@@ -46,3 +66,29 @@ function StratItem({ data }) {
4666 return h ( LinkCard , { href : "/lex/strat-name-concepts/" + concept_id } , name )
4767}
4868
69+ function useStratData ( lastID , input , pageSize ) {
70+ const result = useAPIResult ( `${ SETTINGS . apiV2Prefix } /defs/strat_name_concepts?page_size=${ pageSize } &last_id=${ lastID } &concept_like=${ input } ` ) ;
71+ return result ?. success ?. data ;
72+ }
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 ] . concept_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