1- import { Stack } from "@chakra-ui/react" ;
1+ import { Heading , Skeleton , Stack , Text } from "@chakra-ui/react" ;
2+ import { useEffect , useState , type Dispatch , type SetStateAction } from "react" ;
23import { LuFolder } from "react-icons/lu" ;
3- import type { StacCatalog } from "stac-ts" ;
4+ import type { StacCatalog , StacCollection , StacLink } from "stac-ts" ;
5+ import { CollectionCard } from "./collection" ;
46import { ValueInfo } from "./shared" ;
57
6- export function Catalog ( { catalog } : { catalog : StacCatalog } ) {
8+ function Collections ( {
9+ href,
10+ setHref,
11+ } : {
12+ href : string ;
13+ setHref : Dispatch < SetStateAction < string > > ;
14+ } ) {
15+ const { collections, loading, error } = useCollections ( href ) ;
16+
17+ if ( error ) {
18+ < Text colorPalette = { "red" } > Error when loading collections: { error } </ Text > ;
19+ } else if ( loading ) {
20+ return < Skeleton h = { 200 } > </ Skeleton > ;
21+ } else {
22+ return (
23+ < Stack >
24+ { collections . map ( ( collection ) => (
25+ < CollectionCard
26+ collection = { collection }
27+ setHref = { setHref }
28+ key = { collection . id }
29+ > </ CollectionCard >
30+ ) ) }
31+ </ Stack >
32+ ) ;
33+ }
34+ }
35+
36+ export function Catalog ( {
37+ catalog,
38+ setHref,
39+ } : {
40+ catalog : StacCatalog ;
41+ setHref : Dispatch < SetStateAction < string > > ;
42+ } ) {
43+ const collectionsHref = catalog . links . find (
44+ ( link ) => link . rel == "data"
45+ ) ?. href ;
746 return (
847 < Stack >
948 < ValueInfo
@@ -14,6 +53,56 @@ export function Catalog({ catalog }: { catalog: StacCatalog }) {
1453 title = { catalog . title }
1554 description = { catalog . description }
1655 > </ ValueInfo >
56+
57+ { collectionsHref && (
58+ < >
59+ < Heading size = { "md" } mt = { 8 } >
60+ Collections
61+ </ Heading >
62+ < Collections href = { collectionsHref } setHref = { setHref } > </ Collections >
63+ </ >
64+ ) }
1765 </ Stack >
1866 ) ;
1967}
68+
69+ function useCollections ( href : string ) {
70+ const [ loading , setLoading ] = useState ( false ) ;
71+ const [ error , setError ] = useState < string | undefined > ( ) ;
72+ const [ collections , setCollections ] = useState < StacCollection [ ] > ( [ ] ) ;
73+
74+ useEffect ( ( ) => {
75+ setLoading ( true ) ;
76+ ( async ( ) => {
77+ let nextHref = href ;
78+ let fetchedCollections : StacCollection [ ] = [ ] ;
79+ while ( true ) {
80+ const response = await fetch ( nextHref ) ;
81+ if ( response . ok ) {
82+ const data = await response . json ( ) ;
83+ fetchedCollections = [
84+ ...fetchedCollections ,
85+ ...( data . collections ?? [ ] ) ,
86+ ] ;
87+ const nextLink = ( data . links ?? [ ] ) . find (
88+ ( link : StacLink ) => link . rel == "next"
89+ ) ;
90+ if ( nextLink && nextLink . href != nextHref ) {
91+ nextHref = nextLink . href ;
92+ } else {
93+ break ;
94+ }
95+ } else {
96+ setError (
97+ "Error while fetching " + href + ": " + ( await response . text ( ) )
98+ ) ;
99+ break ;
100+ }
101+ }
102+ setCollections ( fetchedCollections ) ;
103+ setLoading ( false ) ;
104+ } ) ( ) ;
105+ } , [ href ] ) ;
106+
107+ return { collections, loading, error } ;
108+ }
0 commit comments