11import { LngLatCoords } from "@macrostrat/map-interface" ;
2- import { useState } from "react" ;
2+ import { useState , useEffect } from "react" ;
33import "mapbox-gl/dist/mapbox-gl.css" ;
44import {
55 BlankImage ,
66 getImageUrl ,
7+ TestImage ,
78 Footer ,
89} from "~/components/general" ;
910import { Icon } from "@blueprintjs/core" ;
@@ -23,14 +24,9 @@ export function Page() {
2324 . replace ( / % 4 0 / g, "@" )
2425 . replace ( / % 2 B / g, "+" ) ) ;
2526
26- let photoIDArr = [ checkin . photo ] ;
27- if ( checkin . observations ) {
28- checkin . observations . forEach ( ( obs ) => {
29- if ( obs . photo && ! photoIDArr . includes ( obs . photo ) ) {
30- photoIDArr . push ( obs . photo ) ;
31- }
32- } ) ;
33- }
27+
28+ const TestObject = PhotoIDCollector ( { checkin } )
29+ const photoIDArr = TestObject . props . photoIDArr . sort ( ( a , b ) => a - b ) ;
3430
3531 const photoIndex = photoIDArr . indexOf ( photoID ) ;
3632
@@ -68,7 +64,9 @@ export function Page() {
6864 h ( Icon , { icon : "symbol-circle" , style : { color : "white" } } ) ,
6965 ] ) ;
7066 }
71- } ) ) , h ( Footer )
67+ } ) ) ,
68+ h ( 'div.hide' , TestObject ) ,
69+ h ( Footer )
7270 ] ) ;
7371}
7472
@@ -84,7 +82,7 @@ function ObservationContent({ observation, setBody }) {
8482 }
8583
8684 let lithologies = [ ] ;
87- rocks . liths . forEach ( ( lith ) => {
85+ rocks . liths ? .forEach ( ( lith ) => {
8886 if ( ! lith . color . includes ( "#" ) ) {
8987 lithologies . push ( { name : lith . name , color : rgbaStringToHex ( lith . color ) } ) ;
9088 } else {
@@ -119,7 +117,9 @@ function ObservationContent({ observation, setBody }) {
119117 zoom : 10 ,
120118 } ;
121119
122- return h ( "div" , { className : "observation-body" } , [
120+ const show = lithologies . length > 0 || rocks ?. notes ?. length > 0 && observation . lat && rocks . strat_name ?. strat_name_long
121+
122+ return h . if ( show ) ( "div" , { className : "observation-body" } , [
123123 h ( Icon , {
124124 className : "close-body" ,
125125 icon : "ban-circle" ,
@@ -131,7 +131,7 @@ function ObservationContent({ observation, setBody }) {
131131 LngLatCoords ( LngLatProps ) ,
132132 ] )
133133 : null ,
134- h ( "div" , { className : "observation-details" } , [
134+ h . if ( lithologies || rocks ) ( "div" , { className : "observation-details" } , [
135135 h ( LithologyList , { lithologies } ) ,
136136 h ( "p" , { className : "notes" } , rocks . notes ) ,
137137 ] ) ,
@@ -166,4 +166,53 @@ function Item({ checkin, photoID }) {
166166 onClick : ( ) => setBody ( true ) ,
167167 } )
168168 ] ) ;
169+ }
170+
171+ function PhotoIDCollector ( { checkin } ) {
172+ const [ photoIDArr , setPhotoIDArr ] = useState ( ( ) =>
173+ checkin . photo ? [ checkin . photo ] : [ ]
174+ ) ;
175+ const [ testedPhotos , setTestedPhotos ] = useState ( ( ) => {
176+ return checkin . observations
177+ ? checkin . observations . map ( obs => obs . photo ) . filter ( Boolean )
178+ : [ ] ;
179+ } ) ;
180+
181+ useEffect ( ( ) => {
182+ // Only update if checkin changes
183+ if ( checkin . photo && ! photoIDArr . includes ( checkin . photo ) ) {
184+ setPhotoIDArr ( prev => [ ...prev , checkin . photo ] ) ;
185+ }
186+
187+ const newPhotos = ( checkin . observations || [ ] )
188+ . map ( obs => obs . photo )
189+ . filter ( photo => photo && ! testedPhotos . includes ( photo ) ) ;
190+
191+ if ( newPhotos . length > 0 ) {
192+ setTestedPhotos ( prev => [ ...prev , ...newPhotos ] ) ;
193+ }
194+ } , [ checkin ] ) ;
195+
196+ return h ( 'div' , { photoIDArr } , [
197+ ...testedPhotos . map ( ( photo ) => {
198+ return h ( HideImage , {
199+ key : photo ,
200+ checkin,
201+ photo,
202+ setPhotoIDArr,
203+ } ) ;
204+ } )
205+ ] ) ;
206+ }
207+
208+
209+ function HideImage ( { checkin, photo, setPhotoIDArr } ) {
210+ return h ( TestImage , {
211+ key : photo ,
212+ src : getImageUrl ( checkin . person_id , photo ) ,
213+ onLoad : ( ) => {
214+ setPhotoIDArr ( ( prev ) => ( prev . includes ( photo ) ? prev : [ ...prev , photo ] ) ) ;
215+ } ,
216+ onError : ( ) => console . warn ( "Image failed to load:" , photo ) ,
217+ } ) ;
169218}
0 commit comments