Skip to main content

Schema lookup hooks

Small hooks around the schemas the admin has already fetched: the service-level list of resources, one resource's entry in it, and a way to throw the cache away.

Use them when a component needs to know something about a resource without fetching its full schema — whether it exists, whether it may be aggregated, what its base URL is.

useGetGlobalResourceSchema​

Returns a lookup function into the service schemas held by SchemaAdmin.

import {useGetGlobalResourceSchema} from 'habarico';

const getGlobalResourceSchema = useGetGlobalResourceSchema();
const base = getGlobalResourceSchema('eshop/product');

if (base?.globalFulltextSearchEnabled) {
// offer this resource in the search overlay
}

The returned function takes a "service/resource" string and gives back that resource's GlobalResourceSchema — its url, label, pkFieldName, menu, the views and actions it exposes, and the globalFulltextSearchEnabled flag — or undefined when the service or the resource is unknown. A paid backend also sends aggregateEnabled and dashboardEnabled; those are typed by habarico-enterprise on DashboardResourceSchema, since no free backend emits them. It reads from context and performs no request, so it is cheap to call in a render.

useGlobalResourceSchemaContext​

The same lookup, already resolved for one resource and enriched with the capability flags — without a request. Called with no argument it takes the resource from the surrounding resource context.

import {useGlobalResourceSchemaContext} from 'habarico';

const {schema, getGlobalViewSchema, hasEditView, hasDeleteAction} =
useGlobalResourceSchemaContext('eshop/product');
NameTypeDescription
schemaGlobalResourceSchema | undefinedThe resource's global entry
getGlobalViewSchema(name?: string) => GlobalViewSchema | undefinedOne view by name
hasListView, hasCreateView, hasEditView, hasShowViewbooleanWhether the resource exposes that view
hasListAction, hasCreateAction, hasEditAction, hasShowAction, hasDeleteActionbooleanWhether the resource exposes that action

useGetGlobalResourceSchemaContext() is the uncurried form — it returns (resource?: string) => … so one component can inspect several resources.

tip

Prefer this over useGetLocalResourceSchema when all you need is "does this resource have an edit view". The global layer is already in memory; the local one costs a request per resource, per record and per locale.

useGetServiceSchemas​

Fetches the service-level schema of every service in a ServiceConfigMap. SchemaAdmin calls it once and puts the result in context; call it yourself only when you build an admin shell of your own.

const {isLoading, isLoaded, isError, servicesSchema, refetch} = useGetServiceSchemas(
serviceConfigMap,
schemaProvider,
locale,
identity,
() => authProvider.isAuthenticated()
);
NameTypeDescription
servicesSchemaServicesSchemaResources per service key; a service still loading is simply absent
isLoading / isErrorbooleanTrue when any of the services is loading or failed
isLoadedbooleanEverything resolved and a signed-in identity is known
refetch() => voidRefetches every service schema

Queries are keyed by service, locale and user id, and are skipped entirely until an identity exists. The optional fifth argument, isAuthenticated, is read on every render; while it returns false the queries are skipped too. Pass it so that a render after a sign-out, which may still hold the last identity, does not refetch the schemas without credentials.

useLocalResourceSchemaRefresh​

Returns a function that invalidates every cached resource schema, so the next screen fetches a fresh one.

import {useLocalResourceSchemaRefresh} from 'habarico';

const refreshSchemas = useLocalResourceSchemaRefresh();

// after an action that changes what the user may do
await callAction(...);
refreshSchemas();

Reach for it when a backend change alters the schema rather than the data — a role change, a workflow transition that unlocks new actions. Plain record edits need no refresh; the data provider already invalidates those.

caution

It invalidates the resourceSchema queries only. Service-level schemas — the menu, the resource list — are not touched; use refetch from useGetServiceSchemas for those.

See also​