@saplings/react
v0.3.0
Published
Reusable Saplings react toolkit
Readme
@saplings/react
React context bindings for application session, permissions and feature flags.
Install
npm install @saplings/react reactQuick start
import { SaplingsProvider, usePermission, useFeatureFlag } from '@saplings/react';
function Dashboard() {
const canEdit = usePermission('project:write');
const showReports = useFeatureFlag('reports');
return <div>{canEdit && 'Editor'} {showReports && 'Reports'}</div>;
}
const tree = <SaplingsProvider value={{
session: { userId: 'u1', tenantId: 't1', permissions: ['project:write'] },
flags: { reports: true },
}}><Dashboard /></SaplingsProvider>;API and operational notes
The application supplies verified session data and flag decisions. Missing sessions and flags fail closed in the hooks. These hooks only control presentation: the backend must independently authorize every operation. The provider does not implement login, session refresh, or flag fetching.
Validation
Run the workspace build and pnpm test after making changes. Confirm application-specific behavior and accessibility in the consuming environment.
Loading application data
import { useCallback } from 'react';
import { useAsyncResource } from '@saplings/react';
function Profile() {
const load = useCallback(async (signal: AbortSignal) => {
const response = await fetch('/api/profile', { signal });
if (!response.ok) throw new Error('Profile unavailable');
return response.json() as Promise<{ name: string }>;
}, []);
const state = useAsyncResource(load);
return <p>{state.status === 'ready' ? state.data.name : state.status}</p>;
}Pass a stable loader. The hook aborts on unmount or loader change and ignores superseded results. Authentication, data validation, retries and cache policy belong to the application.
Authentication lifecycle
createAuthController(adapter) coordinates refresh, sign-in and sign-out,
cancelling superseded operations and ignoring stale completions. The application
must supply an adapter backed by its own verified identity service, secure
cookies, refresh policy and CSRF protection. Subscribe to state changes and
dispose the controller when it is no longer needed. A failed sign-out clears the
local session and reports an error; revoke the server session separately.
Kingsman-backed starter sessions
import { createCookieSessionAdapter, createAuthController, useAuthState } from '@saplings/react';
import { createStarterSessionClient, type StarterLogin, type StarterSession } from '@saplings/client';
const transport = createStarterSessionClient({ baseUrl: location.origin });
const adapter = createCookieSessionAdapter<StarterSession, StarterLogin>(transport);
const auth = createAuthController(adapter);
await auth.refresh();
function AccountStatus() {
const state = useAuthState(auth);
return <output>{state.status}</output>;
}Create the controller per browser session or SSR request. Sign-in reads the session back from the server after the cookie is set. The starter serializes session mutations and clears identity-dependent query data when the session changes. The server checks permissions on every protected operation.
