Skip to content

Commit fd431a3

Browse files
committed
refactor!: remove collections/items state from context
BREAKING CHANGE: Remove collections, setCollections, getItem, addItem, deleteItem from context. Context now only provides stacApi. Users should use useCollections() hook directly instead of accessing collections from context. React Query cache is now the single source of truth for data.
1 parent 363d53e commit fd431a3

File tree

5 files changed

+4
-70
lines changed

5 files changed

+4
-70
lines changed

src/context/StacApiProvider.test.tsx

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,6 @@ describe('StacApiProvider', () => {
165165
});
166166
});
167167

168-
it('provides context methods', () => {
169-
function ContextChecker() {
170-
const context = useStacApiContext();
171-
const hasMethods =
172-
typeof context.getItem === 'function' &&
173-
typeof context.addItem === 'function' &&
174-
typeof context.deleteItem === 'function';
175-
return <div data-testid="has-methods">{hasMethods ? 'true' : 'false'}</div>;
176-
}
177-
178-
render(
179-
<StacApiProvider apiUrl="https://test-stac-api.com">
180-
<ContextChecker />
181-
</StacApiProvider>
182-
);
183-
184-
expect(screen.getByTestId('has-methods')).toHaveTextContent('true');
185-
});
186-
187168
it('passes options to useStacApi hook', () => {
188169
const customOptions = { headers: { 'X-Custom': 'value' } };
189170

src/context/context.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { createContext } from 'react';
2-
import type { CollectionsResponse, Item } from '../types/stac';
32

43
export type StacApiContextType = {
54
// eslint-disable-next-line @typescript-eslint/no-explicit-any
65
stacApi?: any;
7-
collections?: CollectionsResponse;
8-
setCollections: (collections?: CollectionsResponse) => void;
9-
getItem: (id: string) => Item | undefined;
10-
addItem: (id: string, item: Item) => void;
11-
deleteItem: (id: string) => void;
126
};
137

148
export const StacApiContext = createContext<StacApiContextType>({} as StacApiContextType);

src/context/index.tsx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { useMemo, useState, useCallback, useContext } from 'react';
1+
import React, { useMemo, useContext } from 'react';
22
import { StacApiContext } from './context';
3-
import type { CollectionsResponse, Item } from '../types/stac';
43
import { GenericObject } from '../types';
54
import { QueryClient, QueryClientProvider, QueryClientContext } from '@tanstack/react-query';
65

@@ -19,37 +18,12 @@ function StacApiProviderInner({
1918
options,
2019
}: Omit<StacApiProviderType, 'enableDevTools'>) {
2120
const { stacApi } = useStacApi(apiUrl, options);
22-
const [collections, setCollections] = useState<CollectionsResponse>();
23-
const [items, setItems] = useState(new Map<string, Item>());
24-
25-
const getItem = useCallback((id: string) => items.get(id), [items]);
26-
27-
const addItem = useCallback(
28-
(itemPath: string, item: Item) => {
29-
setItems(new Map(items.set(itemPath, item)));
30-
},
31-
[items]
32-
);
33-
34-
const deleteItem = useCallback(
35-
(itemPath: string) => {
36-
const tempItems = new Map(items);
37-
items.delete(itemPath);
38-
setItems(tempItems);
39-
},
40-
[items]
41-
);
4221

4322
const contextValue = useMemo(
4423
() => ({
4524
stacApi,
46-
collections,
47-
setCollections,
48-
getItem,
49-
addItem,
50-
deleteItem,
5125
}),
52-
[addItem, collections, deleteItem, getItem, stacApi]
26+
[stacApi]
5327
);
5428

5529
return <StacApiContext.Provider value={contextValue}>{children}</StacApiContext.Provider>;

src/context/useStacApiContext.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ import { useContext } from 'react';
22
import { StacApiContext } from './context';
33

44
export function useStacApiContext() {
5-
const { stacApi, collections, setCollections, getItem, addItem, deleteItem } =
6-
useContext(StacApiContext);
5+
const { stacApi } = useContext(StacApiContext);
76

87
return {
98
stacApi,
10-
collections,
11-
setCollections,
12-
getItem,
13-
addItem,
14-
deleteItem,
159
};
1610
}

src/hooks/useCollections.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type StacCollectionsHook = {
1515
};
1616

1717
function useCollections(): StacCollectionsHook {
18-
const { stacApi, setCollections } = useStacApiContext();
18+
const { stacApi } = useStacApiContext();
1919
const [state, setState] = useState<LoadingState>('IDLE');
2020

2121
const fetchCollections = async (): Promise<CollectionsResponse> => {
@@ -47,15 +47,6 @@ function useCollections(): StacCollectionsHook {
4747
retry: false,
4848
});
4949

50-
// Sync collections with context
51-
useEffect(() => {
52-
if (collections) {
53-
setCollections(collections);
54-
} else if (error) {
55-
setCollections(undefined);
56-
}
57-
}, [collections, error, setCollections]);
58-
5950
const reload = useMemo(() => debounce(refetch), [refetch]);
6051

6152
useEffect(() => {

0 commit comments

Comments
 (0)