1- import React , { useMemo , useContext , useState } from 'react' ;
1+ import React , { useMemo , useContext , useState , useCallback } from 'react' ;
22import { createContext } from 'react' ;
33
44import StacApi from '../stac-api' ;
55import useStacApi from '../hooks/useStacApi' ;
6- import type { CollectionsResponse } from '../types/stac' ;
6+ import type { CollectionsResponse , Item } from '../types/stac' ;
77import { GenericObject } from '../types' ;
88
99type StacApiContextType = {
1010 stacApi ?: StacApi ;
1111 collections ?: CollectionsResponse ;
1212 setCollections : ( collections ?: CollectionsResponse ) => void ;
13+ getItem : ( id : string ) => Item | undefined ;
14+ addItem : ( id : string , item : Item ) => void ;
1315}
1416
1517type StacApiProviderType = {
@@ -23,12 +25,21 @@ export const StacApiContext = createContext<StacApiContextType>({} as StacApiCon
2325export function StacApiProvider ( { children, apiUrl, options } : StacApiProviderType ) {
2426 const { stacApi } = useStacApi ( apiUrl , options ) ;
2527 const [ collections , setCollections ] = useState < CollectionsResponse > ( ) ;
28+ const [ items , setItems ] = useState ( new Map < string , Item > ( ) ) ;
29+
30+ const getItem = useCallback ( ( id : string ) => items . get ( id ) , [ items ] ) ;
31+
32+ const addItem = useCallback ( ( itemPath : string , item : Item ) => {
33+ setItems ( new Map ( items . set ( itemPath , item ) ) ) ;
34+ } , [ items ] ) ;
2635
2736 const contextValue = useMemo ( ( ) => ( {
2837 stacApi,
2938 collections,
30- setCollections
31- } ) , [ collections , stacApi ] ) ;
39+ setCollections,
40+ getItem,
41+ addItem
42+ } ) , [ addItem , collections , getItem , stacApi ] ) ;
3243
3344 return (
3445 < StacApiContext . Provider value = { contextValue } >
@@ -38,11 +49,19 @@ export function StacApiProvider({ children, apiUrl, options }: StacApiProviderTy
3849}
3950
4051export function useStacApiContext ( ) {
41- const { stacApi, collections, setCollections } = useContext ( StacApiContext ) ;
52+ const {
53+ stacApi,
54+ collections,
55+ setCollections,
56+ getItem,
57+ addItem
58+ } = useContext ( StacApiContext ) ;
4259
4360 return {
4461 stacApi,
4562 collections,
46- setCollections
63+ setCollections,
64+ getItem,
65+ addItem
4766 } ;
4867}
0 commit comments