Skip to main content

Overview

The useCollections hook gives you a navigable view of the user’s collection tree. It automatically fetches the root collection on mount and loads sub-collections as you navigate deeper.

Setting Up

useCollections requires a logged-in user. It reads the user and project context from the nearest SublayProvider.
The hook maintains a navigation stack. Calling openCollection pushes a collection onto the stack and fetches its sub-collections. goBack pops the stack; goToRoot resets to the root.

Creating Sub-collections

createCollection creates a new sub-collection inside the current collection.
You cannot create a collection at the root of useCollections — there is no concept of “creating inside root” vs “creating inside a sub-collection” from the hook’s perspective. The current collection is always the parent.

Renaming a Collection

The root collection cannot be renamed.

Deleting a Collection

Deleting a collection also deletes every collection under it. The root collection cannot be deleted. If the current collection is among the deleted ones, navigation moves back to the most recent collection in the history that wasn’t deleted; otherwise it stays where it is. deleteCollection resolves to { deletedCollectionIds }, the deleted collection and every collection under it, or to undefined when the delete fails.

Saving and Removing Entities

addToCollection saves an entity into the current collection. removeFromCollection removes it.
An entity in a space the user can’t read (see who can read content) can’t be saved: the request fails with 404 entity/not-found, like a missing entity.

Entity counts

A collection’s entityCount counts the entities the user can see in it, by the same rule as the total of its entities list, so the two always match: entities that are deleted, removed by moderation, back in drafts, or in a space the user can no longer read aren’t counted or listed, though they stay saved. See Collection → entityCount.

Checking If an Entity Is Saved

Fetch the entity with include: "saved" to get entity.saved: whether it is saved anywhere in the user’s collections, and which collections hold it. It works on lists too, so each entity in a feed carries its own saved state.
Adding an entity to a collection or removing it from one through useCollections updates saved on the entity in every entity list that carries it, so a feed fetched with include: "saved" shows the change without fetching again. Renaming a collection updates its name in saved the same way, and deleting a collection drops it, and every collection under it, from saved.collections; isSaved stays true only while another collection is listed. These updates, like those for reactions, edits and comment counts, reach lists loaded with useEntityList, and an EntityProvider given one of their entities. They don’t reach an EntityProvider that fetched the entity itself, or entities held by useFetchManyEntitiesWrapper; fetch those again to see the change. See Saved state.

Displaying Collection Entities

Use useCollectionEntitiesWrapper to fetch the paginated list of entities saved in a collection.