Skip to content

Alpha 11 #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Alpha 11 #24

wants to merge 7 commits into from

Conversation

danielfdsilva
Copy link
Member

Changelog

🎉 Features

  • Add pagination support to useCollections

🚀 Improvements

  • Allow STAC API request headers to be set via Provider
  • Allow useStacApi to return the instance in the context

🐛 Fixes

Allows headers to be set globally for STAC API
Moves STAC API logic into a context provider, allowing for easier management of API state and access to STAC API instance, collections, and item data.
It also introduces pagination to the collections hook.
@danielfdsilva danielfdsilva requested a review from oliverroick July 8, 2025 11:00
@danielfdsilva
Copy link
Member Author

danielfdsilva commented Jul 8, 2025

Mostly to fix this: content type json error and collections pagination on stac-manager

Copy link
Member

@oliverroick oliverroick left a comment

Choose a reason for hiding this comment

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

The changes look mostly good, although this is a bit hard to review with that many changes and very little context why we need these changes.

I left a couple of comments and I think we should address the following:

  • The style updates are fine, I guess, but shouldn’t the linter be updated to match those preferences?
  • Any changes to developer-facing APIs should be documented in the README.

);

const nextPage = useCallback(() => {
setOffset(offset + limit);
Copy link
Member

Choose a reason for hiding this comment

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

If the collections response has next and/or previous links specified, why are we using offset and limit here instead of calling the provided links. Also, the collections spec uses page query params for pagination in collections.

Copy link
Member Author

Choose a reason for hiding this comment

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

While working with the eoepca api (https://eoapi.apx.develop.eoepca.org/stac/collections?limit=2) it uses offset and limit, so I went with that.
Using the offset and limit allows the user to go to a specific page, or to reset to the first page more easily. There's no "first" link, so to go back we'd need to get the current link and set the offset to 0.

Not sure what the best approach would be. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

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

What I said was not 100% correct. The spec actually says this:

With this, a link with relation next is included in the links array, and this is used to navigate to the next page of Collection objects. The specific query parameter used for paging is implementation specific and not defined by STAC API. For example, an implementation may take a parameter (e.g., page) indicating the numeric page of results, a base64-encoded value indicating the last result returned for the current page (e.g., search_after as in Elasticsearch), or a cursor token representing backend state.

So really, we don't know how individual services implement pagination. Seeing, that STAC React is a generic library, the safest bet is to rely on next and prev/previous links.

Not being able to reliably infer a first or last page link is an obvious flaw, which should probably be added to the STAC API spec. We could try to infer the pagination parameters, but it seems to open a huge can of worms because we really don't know how pagination will work in each instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, this is unfortunate. I'll check about using the prev/next

Comment on lines +12 to +17
// The context uses this hook to get the StacApi instance.
// If a URL is provided, it will create a new StacApi instance.
// If no URL is provided, it will use the StacApi instance from the context.
// This allows the hook to be used in different contexts, such as in using the
// existing instance or to create a new one.
const { stacApi: ctxStacApi } = useContext(StacApiContext);
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we need this. useStacApi was an undocumented feature, it wasn't meant to be used by implementers. How will this be used?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is basically to access the underlying api instance to make custom requests not covered by the hooks. In this case I'm using it to make the requests needed to update/create collections. Instead of making a call directly with window.fetch I'm getting the api instance to make use of its fetch method which gets the url from context and also has nice error handling.

Copy link
Member

Choose a reason for hiding this comment

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

The stacApi object is exposed through the StacApiContext. Would something like this not work?

const { stacApi } = useStacApiContext();
stacApi.fetch("/collections", {
  method: "POST"
  payload: { ... }
  headers: { ... }
})

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, that would work as well. However the useStacApiContext is not exported, and having to change something, it made more sense to me that the useStacApi hook (which is exported), would return the instance in the context.

stac-react/src/index.ts

Lines 8 to 15 in a619465

export {
useCollections,
useCollection,
useItem,
useStacSearch,
useStacApi,
StacApiProvider,
};

Copy link
Member

Choose a reason for hiding this comment

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

Yes ok, in that case I would actually suggest to not export useStacApi (which is only exported because it was needed in previous versions and I forgot to remove the export) and to export useStacApiContext instead. It makes sense for exactly the developer case you're facing, where you need access to the stacApi instance without the need to change useStacApi and introducing complexity by making the url param optional.

@danielfdsilva
Copy link
Member Author

@oliverroick thank you for the review and apologies for the lack of detail, I'll try to go over the points, but let me know if you want to have a longer discussion about this:

  • Linting: the linter automatically formatted the files when I went to fix the errors. Maybe it picked up more than just the project's linter for some reason?
  • Readme: Will update.

Allows `useCollections` hook to accept query parameters.

Updates the hook and tests to reflect this change.
@danielfdsilva
Copy link
Member Author

@oliverroick Updated to make use of previous/next links if they exist.
Let me know if this looks good and I'll update the documentation.

setOffset,
nextPage: nextUrl ? nextPage : undefined,
prevPage: prevUrl ? prevPage : undefined,
setCurrentUrl,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to expose this? It feels like this should only be used internally.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, thinking about it. Do we need the currentUrl state at all? It seems like it's only needed the set the prev/next page URLs, but couldn't we call getCollections directly from the nextPage/prevPage callbacks?

const nextPage = useCallback(() => getCollections(nextUrl), [nextUrl]);
const prevPage = useCallback(() => getCollections(prevUrl), [prevUrl]);

@oliverroick
Copy link
Member

@danielfdsilva This looks good, thank you for those changes. Just one minor thing, just wondering if we need to expose setCurrentUrl?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants