@@ -7,6 +7,7 @@ import { ColumnNotes } from "../../notes";
77import { useMemo } from "react" ;
88import type { IUnit } from "../../units" ;
99import styles from "./index.module.sass" ;
10+ import { useCallback } from "react" ;
1011
1112const h = hyper . styled ( styles ) ;
1213
@@ -28,39 +29,57 @@ function FossilInfo(props: FossilItemProps) {
2829 const { note, spacing } = props ;
2930 const { data, unit } = note ;
3031
31- let d1 = data ;
32+ return h ( TruncatedList , {
33+ data,
34+ className : "fossil-collections" ,
35+ itemRenderer : PBDBCollectionLink ,
36+ } ) ;
37+ }
38+
39+ interface TruncatedListProps {
40+ data : any [ ] ;
41+ className ?: string ;
42+ maxItems ?: number ;
43+ itemRenderer ?: ( props : { data : any } ) => any ;
44+ }
3245
46+ export function TruncatedList ( {
47+ data,
48+ className,
49+ maxItems = 5 ,
50+ itemRenderer = ( p ) => h ( "span" , p . data ) ,
51+ } : TruncatedListProps ) {
3352 let tooMany = null ;
34- if ( data . length > 5 ) {
35- const n = data . length - 5 ;
36- d1 = data . slice ( 0 , 5 ) ;
53+ let d1 = data ;
54+ if ( data . length > maxItems ) {
55+ const n = data . length - maxItems ;
56+ d1 = data . slice ( 0 , maxItems ) ;
3757 tooMany = h ( "li.too-many" , `and ${ n } more` ) ;
3858 }
3959
40- return h ( "ul.fossil-collections" , [
41- d1 . map ( ( d ) => {
42- return h ( "li.collection" , { key : d . cltn_id } , [
43- h ( PBDBCollectionLink , { collection : d } ) ,
44- ] ) ;
60+ return h ( "ul.truncated-list" , { className } , [
61+ d1 . map ( ( d , i ) => {
62+ return h ( "li.element" , { key : i } , h ( itemRenderer , { data : d } ) ) ;
4563 } ) ,
4664 tooMany ,
4765 ] ) ;
4866}
4967
50- function PBDBCollectionLink ( { collection } : { collection : PBDBCollection } ) {
68+ function PBDBCollectionLink ( { data } : { data : PBDBCollection } ) {
5169 return h (
5270 "a.link-id" ,
5371 {
54- href : `https://paleobiodb.org/classic/basicCollectionSearch?collection_no=${ collection . cltn_id } ` ,
72+ href : `https://paleobiodb.org/classic/basicCollectionSearch?collection_no=${ data . cltn_id } ` ,
5573 } ,
56- collection . cltn_name ,
74+ data . cltn_name ,
5775 ) ;
5876}
5977
6078const matchingUnit = ( dz ) => ( d ) => d . unit_id == dz . unit_id ;
6179
6280export function PBDBFossilsColumn ( { columnID, color = "magenta" } ) {
6381 const data = useFossilData ( { col_id : columnID } ) ;
82+
6483 const { axisType, units } = useMacrostratColumnData ( ) ;
6584
6685 const notes : any [ ] = useMemo ( ( ) => {
@@ -118,3 +137,76 @@ export function PBDBFossilsColumn({ columnID, color = "magenta" }) {
118137 } ) ,
119138 ) ;
120139}
140+
141+ interface BaseMeasurementsColumnProps < T > {
142+ data : T [ ] ;
143+ noteComponent ?: any ;
144+ width ?: number ;
145+ paddingLeft ?: number ;
146+ className ?: string ;
147+ getUnitID ?: ( d : T ) => number | string ;
148+ }
149+
150+ export function BaseMeasurementsColumn ( {
151+ data,
152+ noteComponent,
153+ width = 500 ,
154+ paddingLeft = 40 ,
155+ className,
156+ getUnitID = ( d ) => d . unit_id ,
157+ } : BaseMeasurementsColumnProps < any > ) {
158+ const { axisType, units } = useMacrostratColumnData ( ) ;
159+
160+ const matchingUnit = useCallback (
161+ ( dz ) => {
162+ return ( d ) => {
163+ return getUnitID ( d ) === dz . unit_id ;
164+ } ;
165+ } ,
166+ [ getUnitID ] ,
167+ ) ;
168+
169+ const notes : any [ ] = useMemo ( ( ) => {
170+ if ( data == null || units == null ) return [ ] ;
171+ let unitRefData = Array . from ( data . values ( ) )
172+ . map ( ( d ) => {
173+ return {
174+ data : d ,
175+ unit : units . find ( matchingUnit ( d ) ) ,
176+ } ;
177+ } )
178+ . filter ( ( d ) => d . unit != null ) ;
179+
180+ unitRefData . sort ( ( a , b ) => {
181+ const v1 = units . indexOf ( a . unit ) ;
182+ const v2 = units . indexOf ( b . unit ) ;
183+ return v1 - v2 ;
184+ } ) ;
185+
186+ return unitRefData . map ( ( d ) => {
187+ const { unit, data } = d ;
188+ const heightRange = getUnitHeightRange ( unit , axisType ) ;
189+
190+ return {
191+ top_height : heightRange [ 1 ] ,
192+ height : heightRange [ 0 ] ,
193+ data,
194+ unit,
195+ id : unit . unit_id ,
196+ } ;
197+ } ) ;
198+ } , [ data , units , matchingUnit ] ) ;
199+
200+ if ( data == null || units == null ) return null ;
201+
202+ return h (
203+ "div" ,
204+ { className } ,
205+ h ( ColumnNotes , {
206+ width,
207+ paddingLeft,
208+ notes,
209+ noteComponent,
210+ } ) ,
211+ ) ;
212+ }
0 commit comments