Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@developmentseed/stac-react",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.2",
"description": "React components and hooks for building STAC-API front-ends",
"repository": "[email protected]:developmentseed/stac-react.git",
"authors": [
Expand Down
12 changes: 7 additions & 5 deletions src/hooks/useCollection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import type { StacHook, StacRefetchFn } from '../types';
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
import type { Collection } from '../types/stac';
import { handleStacResponse } from '../utils/handleStacResponse';
import { generateCollectionQueryKey } from '../utils/queryKeys';
import { useStacApiContext } from '../context/useStacApiContext';
import { ApiError } from '../utils/ApiError';

interface StacCollectionHook extends StacHook {
type StacCollectionHook = {
collection?: Collection;
refetch: StacRefetchFn<Collection>;
}
isLoading: boolean;
isFetching: boolean;
refetch: () => Promise<QueryObserverResult<Collection, ApiError>>;
error: ApiError | null;
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could extend with types too like

type StacHook = {
  isLoading: boolean;
  isFetching: boolean;
  error: ApiError | null;
};

type StacCollectionHook = {
refetch: () => Promise<QueryObserverResult<Collection, ApiError>>;
} & StacHook


function useCollection(collectionId: string): StacCollectionHook {
const { stacApi } = useStacApiContext();
Expand Down
12 changes: 7 additions & 5 deletions src/hooks/useCollections.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import type { StacHook, StacRefetchFn } from '../types';
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
import type { CollectionsResponse } from '../types/stac';
import { handleStacResponse } from '../utils/handleStacResponse';
import { generateCollectionsQueryKey } from '../utils/queryKeys';
import { useStacApiContext } from '../context/useStacApiContext';
import { ApiError } from '../utils/ApiError';

interface StacCollectionsHook extends StacHook {
type StacCollectionsHook = {
collections?: CollectionsResponse;
refetch: StacRefetchFn<CollectionsResponse>;
}
refetch: () => Promise<QueryObserverResult<CollectionsResponse, ApiError>>;
isLoading: boolean;
isFetching: boolean;
error: ApiError | null;
};

function useCollections(): StacCollectionsHook {
const { stacApi } = useStacApiContext();
Expand Down
14 changes: 8 additions & 6 deletions src/hooks/useItem.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { useQuery } from '@tanstack/react-query';
import type { StacHook, StacRefetchFn } from '../types';
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
import type { Item } from '../types/stac';
import { useStacApiContext } from '../context/useStacApiContext';
import { handleStacResponse } from '../utils/handleStacResponse';
import { generateItemQueryKey } from '../utils/queryKeys';
import { ApiError } from '../utils/ApiError';

interface StacItemHook extends StacHook {
type ItemHook = {
item?: Item;
refetch: StacRefetchFn<Item>;
}
isLoading: boolean;
isFetching: boolean;
error: ApiError | null;
refetch: () => Promise<QueryObserverResult<Item, ApiError>>;
};

function useItem(url: string): StacItemHook {
function useItem(url: string): ItemHook {
const { stacApi } = useStacApiContext();

const fetchItem = async (): Promise<Item> => {
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/useStacSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useState, useMemo, useEffect } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import debounce from '../utils/debounce';
import { generateStacSearchQueryKey } from '../utils/queryKeys';
import type { StacHook } from '../types';
import { handleStacResponse } from '../utils/handleStacResponse';
import type {
Link,
Expand All @@ -17,7 +16,7 @@ import { ApiError } from '../utils/ApiError';

type PaginationHandler = () => void;

interface StacSearchHook extends StacHook {
type StacSearchHook = {
ids?: string[];
setIds: (ids?: string[]) => void;
bbox?: Bbox;
Expand All @@ -34,9 +33,12 @@ interface StacSearchHook extends StacHook {
setLimit: (limit: number) => void;
submit: () => void;
results?: SearchResponse;
isLoading: boolean;
isFetching: boolean;
error: ApiError | null;
nextPage: PaginationHandler | undefined;
previousPage: PaginationHandler | undefined;
}
};

function useStacSearch(): StacSearchHook {
const { stacApi } = useStacApiContext();
Expand Down Expand Up @@ -212,7 +214,7 @@ function useStacSearch(): StacSearchHook {
results,
isLoading,
isFetching,
error,
error: error ?? null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is already null. Why is this needed?

sortby,
setSortby,
limit,
Expand Down
23 changes: 0 additions & 23 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
import type { QueryObserverResult } from '@tanstack/react-query';
import { ApiError } from '../utils/ApiError';

export type GenericObject = {
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
};

/**
* Base interface for all STAC hooks providing common loading state and error handling.
* All data-fetching hooks (useCollection, useCollections, useItem, useStacSearch)
* extend this interface with their specific data and refetch signatures.
*/
export interface StacHook {
/** True during initial data fetch (no cached data available) */
isLoading: boolean;
/** True during any fetch operation (including background refetches) */
isFetching: boolean;
/** Error information if the last request was unsuccessful */
error: ApiError | null;
}

/**
* Generic refetch function type for STAC hooks.
* Returns a Promise with the query result including data and error information.
*/
export type StacRefetchFn<T> = () => Promise<QueryObserverResult<T, ApiError>>;
Comment on lines -7 to -26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to keep this pattern and have the types correctly exported. Was there any reason not to?