Skip to content

@activitypods/refine-hooks

React hooks to use inside a <Refine> component configured with the auth provider of @activitypods/refine-providers. They require @refinedev/core 5 and @tanstack/react-query 5.

useAuthProvider

Return the auth provider given to Refine, with its additional methods. It throws an error if it wasn’t created with @activitypods/refine-providers.

const session = useAuthProvider().getSession();

useCollection

Fetch an ActivityPub collection. See Inbox & outbox for an example.

const { items, hasNextPage, fetchNextPage } = useCollection(predicateOrUri);

Parameters

ParameterTypeDescription
predicateOrUriStringURI of the collection, or a predicate of the user’s WebID such as inbox or apods:contacts. Nothing is fetched if it is empty

Return value

PropertyTypeDescription
itemsArrayItems of the pages fetched so far: URIs, or objects for the embedded items
urlStringURI of the collection
isLoadingBooleantrue during the first fetch
isSuccessBooleantrue when the collection has been fetched
errorErrorThe error of the last fetch. Requests are not retried
refetchFunctionFetch the collection again
hasNextPageBooleantrue if there are more pages
fetchNextPageFunctionFetch the next page
isFetchingNextPageBooleantrue during the fetch of the next page
addItemFunctionAdd an item at the beginning of the list, without any request
removeItemFunctionRemove an item from the list, without any request

useOutbox

Post activities to the user’s outbox. See Inbox & outbox for an example.

const { post, url, owner } = useOutbox({ jsonContext });

Parameters

ParameterTypeDefaultDescription
jsonContextAnyActivityStreams’ contextJSON-LD context of the activities

Return value

PropertyTypeDescription
postFunctionPost the given activity and return its URI. It throws an error if the outbox is not known yet
urlStringURI of the outbox
ownerStringWebID of the user

useLoginFlow

The logic of the login page, to build it with another UI library. It must be used on the redirectUri route of the auth provider.

const { isProcessing, error, podProviders, login, backToLogin } = useLoginFlow(options);

Options

PropertyTypeDefaultDescription
defaultPodProviderStringOffer only this Pod provider
customPodProvidersArrayThe Pod providers to offer, as { 'apods:baseUrl': string, 'apods:area'?: string } objects
redirectString/Where to go once logged in

Without any of the first two options, the hook fetches the public Pod providers matching the current locale.

Return value

PropertyTypeDescription
isProcessingBooleantrue while the OIDC flow is being completed. Display a loader or the error
errorStringThe error to display
podProvidersArrayThe Pod providers to offer
loginFunctionRedirect to the given Pod provider
backToLoginFunctionGo back to the list of Pod providers

Search params

ParamDescription
?signupGo through the signup form of the Pod provider when logging in
?iss=<url>Log in directly to this Pod provider
?logoutLog out the user
?redirect=<path>Where to go once logged in. It takes precedence over the redirect option
?codeComplete the OIDC flow. Added by the Pod provider
?register_appCheck that the app is registered. Added by the hook

useBackgroundChecks

Check the app status while the user is logged in, to build your own version of the BackgroundChecks component. Checks are done regularly and whenever the tab becomes visible again.

const { isLoggedOut, isChecked, error, retry } = useBackgroundChecks({ listeningTo, checkInterval });

If the app is not registered or if its access needs changed, the user is redirected to the authorization screen of the Pod provider. When they come back, the hook returns to the page they were on.

Options

PropertyTypeDefaultDescription
listeningToArray[]URIs on which the app backend must have a webhook channel
checkIntervalNumber | false120000Interval in ms between checks. With false, there is no periodic check

Return value

PropertyTypeDescription
isLoggedOutBooleantrue if the user is not logged in
isCheckedBooleantrue once a check has succeeded
errorStringThe error to display if the backend is offline, doesn’t listen to the URIs, or if the check failed
retryFunctionCheck again

useResourceRedirect

Handle the links coming from the data browser of the Pod provider. The boilerplate uses it on the /r route, which is advertised as the resource_url in its nodeinfo.

useResourceRedirect({ resources, jsonContext });

Parameters

ParameterTypeDefaultDescription
resourcesObjectrequiredThe same value as for the data provider
jsonContextAnyActivityStreams’ contextThe same value as for the data provider

Search params

The hook finds the Refine resource matching the type param, then redirects:

ParamsRedirects to
?type=<type>&uri=<uri>The show page of the resource
?type=<type>&uri=<uri>&mode=editThe edit page of the resource
?type=<type>The list page of the resource
Unknown or missing typeThe homepage

Translations

The hooks translate their texts with the i18nProvider given to Refine, using these keys and default texts:

KeyDefault text
pages.login.podProvidersNotLoadedUnable to load the list of Pod providers
apods.error.app_offlineThe app backend is offline
apods.error.app_not_listeningThe app is not listening to {{uri}}
apods.error.app_status_unavailableUnable to check app status