@@ -6,16 +6,23 @@ import { Card, Switch, Spinner } from "@blueprintjs/core";
66import { useState , useEffect , useRef } from "react" ;
77import { ContentPage } from "~/layouts" ;
88import { Loading , SearchBar } from "../../index" ;
9+ import { useData } from "vike-react/useData" ;
910
1011export function Page ( ) {
1112 return StratPage ( { show : false } ) ;
1213}
1314
1415export function StratPage ( { show } ) {
16+ const { res } = useData ( ) ;
17+ console . log ( "res" , res ) ;
18+ const startingID = show
19+ ? res [ res ?. length - 1 ] ?. concept_id
20+ : res [ res ?. length - 1 ] ?. strat_name_id ;
21+
1522 const [ input , setInput ] = useState ( "" ) ;
1623 const [ showConcepts , setShowConcepts ] = useState ( show ?? false ) ;
17- const [ lastID , setLastID ] = useState ( 0 ) ;
18- const [ data , setData ] = useState ( [ ] ) ;
24+ const [ lastID , setLastID ] = useState ( startingID ) ;
25+ const [ data , setData ] = useState ( res ) ;
1926 const pageSize = 20 ;
2027
2128 const strat_name_vars = {
@@ -38,16 +45,40 @@ export function StratPage({ show }) {
3845
3946 const result = useStratData ( lastID , input , pageSize , data_route , like ) ;
4047
48+ console . log ( lastID ) ;
49+ console . log ( "data" , data ) ;
50+
51+ const prevInputRef = useRef ( input ) ;
52+ const prevShowConceptsRef = useRef ( showConcepts ) ;
53+
4154 useEffect ( ( ) => {
42- if ( result ) {
43- setData ( ( prevData ) => [ ...prevData , ...result ] ) ;
55+ // Only reset if input or showConcepts actually changed from previous render
56+ if (
57+ prevInputRef . current !== input ||
58+ prevShowConceptsRef . current !== showConcepts
59+ ) {
60+ // Reset data and lastID to starting ID for current mode
61+ setData ( [ ] ) ;
62+ setLastID ( 0 ) ;
63+
64+ prevInputRef . current = input ;
65+ prevShowConceptsRef . current = showConcepts ;
4466 }
45- } , [ result ] ) ;
67+ } , [ input , showConcepts ] ) ;
4668
4769 useEffect ( ( ) => {
48- setData ( [ ] ) ;
49- setLastID ( 0 ) ;
50- } , [ input , showConcepts ] ) ;
70+ if (
71+ result &&
72+ data [ data . length - 1 ] ?. [ showConcepts ? "concept_id" : "strat_name_id" ] !==
73+ result [ result . length - 1 ] ?. [
74+ showConcepts ? "concept_id" : "strat_name_id"
75+ ]
76+ ) {
77+ setData ( ( prevData ) => {
78+ return [ ...prevData , ...result ] ;
79+ } ) ;
80+ }
81+ } , [ result ] ) ;
5182
5283 if ( data == null ) return h ( Loading ) ;
5384
@@ -59,17 +90,43 @@ export function StratPage({ show }) {
5990 h ( StickyHeader , { className : "header" } , [
6091 h ( PageBreadcrumbs , { title } ) ,
6192 h ( "div.header-description" , [
62- h ( "p" , [
63- h ( "strong" , "Strat Names: " ) ,
64- h ( "span" , "names of rock units, organized hierarchically" ) ,
65- ] ) ,
66- h ( "p" , [
67- h ( "strong" , "Strat Concepts: " ) ,
68- h (
69- "span" ,
70- "capture relationships between differently-named rock units"
71- ) ,
72- ] ) ,
93+ h (
94+ Card ,
95+ {
96+ className : ! showConcepts ? "selected" : "unselected" ,
97+ onClick : ( ) => {
98+ if ( showConcepts ) {
99+ setShowConcepts ( false ) ;
100+ setLastID ( 0 ) ;
101+ setData ( [ ] ) ;
102+ }
103+ } ,
104+ } ,
105+ [
106+ h ( "strong" , "Strat Names: " ) ,
107+ h ( "span" , "names of rock units, organized hierarchically" ) ,
108+ ]
109+ ) ,
110+ h (
111+ Card ,
112+ {
113+ className : showConcepts ? "selected" : "unselected" ,
114+ onClick : ( ) => {
115+ if ( ! showConcepts ) {
116+ setShowConcepts ( true ) ;
117+ setLastID ( 0 ) ;
118+ setData ( [ ] ) ;
119+ }
120+ } ,
121+ } ,
122+ [
123+ h ( "strong" , "Strat Concepts: " ) ,
124+ h (
125+ "span" ,
126+ "capture relationships between differently-named rock units"
127+ ) ,
128+ ]
129+ ) ,
73130 ] ) ,
74131 h ( Card , { className : "filter" } , [
75132 h ( SearchBar , {
@@ -81,6 +138,8 @@ export function StratPage({ show }) {
81138 checked : showConcepts ,
82139 onChange : ( e ) => {
83140 setShowConcepts ( e . target . checked ) ;
141+ setLastID ( 0 ) ;
142+ setData ( [ ] ) ;
84143 } ,
85144 } ) ,
86145 ] ) ,
0 commit comments