@shivaedev/react 0.50.0 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +112 -77
- package/dist/adapters/permissionGuard.d.ts +4 -3
- package/dist/adapters/permissionGuard.d.ts.map +1 -1
- package/dist/adapters/permissionGuard.js +3 -4
- package/dist/adapters/permissionGuard.js.map +1 -1
- package/dist/adapters/signInRedirect.d.ts +4 -0
- package/dist/adapters/signInRedirect.d.ts.map +1 -0
- package/dist/adapters/signInRedirect.js +6 -0
- package/dist/adapters/signInRedirect.js.map +1 -0
- package/dist/{adapters → client/adapters}/queryRoute.d.ts +12 -5
- package/dist/client/adapters/queryRoute.d.ts.map +1 -0
- package/dist/{adapters → client/adapters}/queryRoute.js +14 -1
- package/dist/client/adapters/queryRoute.js.map +1 -0
- package/dist/client/useDebouncedValue.js +1 -1
- package/dist/client/useDebouncedValue.js.map +1 -1
- package/dist/client/useDelay.js +1 -1
- package/dist/client/useDelay.js.map +1 -1
- package/package.json +7 -7
- package/src/adapters/permissionGuard.ts +6 -5
- package/src/adapters/signInRedirect.ts +7 -0
- package/src/{adapters → client/adapters}/queryRoute.ts +24 -9
- package/src/client/useDebouncedValue.ts +1 -1
- package/src/client/useDelay.ts +1 -1
- package/dist/adapters/queryRoute.d.ts.map +0 -1
- package/dist/adapters/queryRoute.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.51.0 - 2026-11-20
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- the result of `queryRoute(buildQuery)` has `useQuery(options?)`, which reads the route's query with the current match's params, decoded search, context and loader value, so a route's component no longer names the operation and its arguments again. `useLoaded()` returns the same atom as `query` and the loader value as `loaded`, to spread into a loader-backed `useEditor`. Replace `useLoaderQuery(api.get.query({ id }), route.useLoaderData())` in a route's component with `noteQuery.useQuery()`, keeping the `queryRoute` result as `noteQuery`; `useLoaderQuery` stays for loaders written with `loadQuery`.
|
|
8
|
+
- a query route reads `redirects` from the router context, so `redirects.signedOut` and `redirects.refused` are stated once for every query route. A route's own `queryRoute(buildQuery, { redirects })` still overrides them for its query.
|
|
9
|
+
- `signInRedirect(path)` in `adapters/signInRedirect.ts` builds a signed-out redirect to `path` that carries the requested location in `returnTo`.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- from this version, with the `@shivaedev/quality` release of this change, write a `failures` handler inline in the component: `useEditor(api.retitle, { query, fixed: { id }, failures: { SubtitleRejected: (error) => ({ field: "title", message: error.message }) } })`. The 0.50.0 note said to declare a handler such as `onTitle` or `couldNotRemove` outside the component because Biome's `noNestedComponentDefinitions` read it as a component; that is no longer needed, and the README now shows the inline form.
|
|
14
|
+
|
|
15
|
+
### Breaking
|
|
16
|
+
|
|
17
|
+
- `queryRoute` moves to `client/adapters/queryRoute.ts`, beside the client hooks its `useQuery()` uses. Change `@shivaedev/react/adapters/queryRoute.ts` imports to `@shivaedev/react/client/adapters/queryRoute.ts`.
|
|
18
|
+
- `permissionGuard` takes `signedOut`, the redirect callback, in place of the `signIn` path. Before: `permissionGuard({ session, signIn: "/sign-in" })`. After: put `redirects: { signedOut: signInRedirect("/sign-in") }` in the router context and pass it to the guard: `beforeLoad: ({ context, ...route }) => permissionGuard({ session, signedOut: context.redirects.signedOut })(route)`.
|
|
19
|
+
|
|
3
20
|
## 0.50.0 - 2026-11-20
|
|
4
21
|
|
|
5
22
|
### Added
|
package/README.md
CHANGED
|
@@ -273,23 +273,16 @@ The hook owns retained display data and the native atom owns execution and cachi
|
|
|
273
273
|
|
|
274
274
|
For both queries and actions, a successful `undefined` is `Option.some(undefined)` in `data`, with `pending` and `refreshing` false. A defect or interruption is available as `Option.some(cause)` in `cause`; they do not request a session recheck, even when the defect has an `Unauthorized` tag.
|
|
275
275
|
|
|
276
|
-
The example's action returns a value; it does not persist the report or invalidate its query. In an application,
|
|
276
|
+
The example's action returns a value; it does not persist the report or invalidate its query. In an application, pass the bound query and command of a module's generated contract instead:
|
|
277
277
|
|
|
278
278
|
```ts
|
|
279
|
-
const notes = useQuery(
|
|
280
|
-
|
|
281
|
-
reactivityKeys: ["notes"],
|
|
282
|
-
}),
|
|
283
|
-
);
|
|
284
|
-
const create = useAction(NotesClient.mutation("CreateNote"));
|
|
279
|
+
const notes = useQuery(api.list.query());
|
|
280
|
+
const add = useAction(api.add);
|
|
285
281
|
|
|
286
|
-
|
|
287
|
-
payload: { title: "Morning walk" },
|
|
288
|
-
reactivityKeys: ["notes"],
|
|
289
|
-
});
|
|
282
|
+
add.dispatch({ title: "Morning walk" });
|
|
290
283
|
```
|
|
291
284
|
|
|
292
|
-
`
|
|
285
|
+
`api` comes from `makeClient` over the module's generated contract, as the [contract guide](https://github.com/ShivaeDev/platform/blob/main/packages/contract/README.md#the-wire-and-the-browsers-contract) describes, with a `list` query and an `add` command that takes `{ title }`. The command's success refreshes the queries that read its keys.
|
|
293
286
|
|
|
294
287
|
An action's `dispatch(input)` returns `void`. Read the atom's latest result from `data`, `cause` or `result`. With the default `Atom.fn` action, a later dispatch interrupts the earlier work and the shared state reports the later result.
|
|
295
288
|
|
|
@@ -456,17 +449,17 @@ A bound command given to `useAction`, `usePendingCommand` or `useUndoableChange`
|
|
|
456
449
|
`failureKind` from `@shivaedev/contract/failureKind.ts` holds that classification, `AppRejection<E>` names the app's own rejections in an error type, and `RequestRejection` and `ServerFailure` in `@shivaedev/errors/taxonomy.ts` name the shared ones.
|
|
457
450
|
|
|
458
451
|
```tsx
|
|
459
|
-
function onTitle(error: SubtitleRejected) {
|
|
460
|
-
return { field: "title" as const, message: error.message };
|
|
461
|
-
}
|
|
462
|
-
|
|
463
452
|
function Retitle({ id }: { readonly id: number }) {
|
|
464
|
-
const editor = useEditor(api.retitle, {
|
|
453
|
+
const editor = useEditor(api.retitle, {
|
|
454
|
+
query: api.get.query({ id }),
|
|
455
|
+
fixed: { id },
|
|
456
|
+
failures: { SubtitleRejected: (error) => ({ field: "title", message: error.message }) },
|
|
457
|
+
});
|
|
465
458
|
return <TitleForm form={editor.form} save={editor.save} />;
|
|
466
459
|
}
|
|
467
460
|
```
|
|
468
461
|
|
|
469
|
-
Here `api.retitle` takes `{ id, title }` and can fail with the app's tagged `SubtitleRejected`, which refers to the server's `subtitle` while the form calls the input `title`; `api`, `SubtitleRejected` and `TitleForm` belong to the application.
|
|
462
|
+
Here `api.retitle` takes `{ id, title }` and can fail with the app's tagged `SubtitleRejected`, which refers to the server's `subtitle` while the form calls the input `title`; `api`, `SubtitleRejected` and `TitleForm` belong to the application. A handler is keyed by the failure's tag and receives the error and the invocation's input, which for a form is its decoded values. It returns a notice descriptor, a field rejection `{ field, message }` naming one of the form's fields (form hooks only), or `"silent"`. In place of a function, `"message"` shows the error's own message as a notice and `"silent"` shows nothing for that tag. A handler may also be given for a shared rejection to word it differently for that call. `silent: true` shows nothing for any failure of the call, which differs from leaving `failures` out: that uses the defaults. The typed failure stays in the hook's `failure` and `cause` in every case.
|
|
470
463
|
|
|
471
464
|
A missing handler is a compile error that names the tag. `useAction(api.retitle)` with no options reports `Property '"failures.SubtitleRejected"' is missing`, followed by `the operation can fail with SubtitleRejected, a rejection the app declared: say what it shows, for example failures: { SubtitleRejected: "message" }`. Options without `failures` report `Property 'failures' is missing` for a type `HandleAppRejections<"SubtitleRejected", …>`, `failures: {}` reports `Property 'SubtitleRejected' is missing`, and a key that is not a failure of the operation is an unknown property. A form's rejection whose `field` can only name a field the form lacks counts as the app's own and needs a handler.
|
|
472
465
|
|
|
@@ -643,7 +636,7 @@ With native `RpcClient.layerProtocolHttp` and `FetchHttpClient.layer` as the bro
|
|
|
643
636
|
|
|
644
637
|
#### Route loaders with TanStack Router
|
|
645
638
|
|
|
646
|
-
TanStack Router owns routes, navigation, search validation, loader data and screen components.
|
|
639
|
+
TanStack Router owns routes, navigation, search validation, loader data and screen components. Declare a route's query once with `queryRoute(buildQuery)` from `@shivaedev/react/client/adapters/queryRoute.ts`, spread the result into `createRoute`, and call its `useQuery()` in the route's component. The spread derives loader dependencies from validated search and loads the contract query atom in the router context’s registry; `useQuery()` reads the same atom, so the component names neither the operation nor its arguments. A `NotFound` instance from `@shivaedev/errors/taxonomy.ts` or a `QueryReadDenied` refusal enters TanStack's not-found handling with the original rejection in the not-found component's `data` prop, preserving its message. One root `notFoundComponent` can show that message for all query routes; a route's own `notFoundComponent` takes precedence. Other typed failures reach `errorComponent` unchanged. The atom runs through its native RPC runtime and shares its execution with React. The factory receives `{ context, params, search }`: `context` is the loader’s router context, including parent `beforeLoad` contributions. Annotate it with `typeof root.types.allContext` (or the parent route’s `types.allContext`) to keep client and viewer access checked against the route’s declaration.
|
|
647
640
|
|
|
648
641
|
This example assumes an application-owned `api.get.query({ id, page })` returning `{ id, title }`, with declared `NotFound` and `NoteMissing` rejections:
|
|
649
642
|
|
|
@@ -655,8 +648,7 @@ import { Effect, Option, Predicate, Schema } from "effect";
|
|
|
655
648
|
import type { ReactNode } from "react";
|
|
656
649
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
657
650
|
import { NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
658
|
-
import { queryRoute } from "@shivaedev/react/adapters/queryRoute.ts";
|
|
659
|
-
import { useLoaderQuery } from "@shivaedev/react/client/useLoaderQuery.ts";
|
|
651
|
+
import { queryRoute } from "@shivaedev/react/client/adapters/queryRoute.ts";
|
|
660
652
|
import { api } from "./notes-client.ts";
|
|
661
653
|
|
|
662
654
|
const registry = AtomRegistry.make();
|
|
@@ -704,7 +696,7 @@ function Shell({ children }: { readonly children: ReactNode }) {
|
|
|
704
696
|
);
|
|
705
697
|
}
|
|
706
698
|
function Note() {
|
|
707
|
-
const query =
|
|
699
|
+
const query = noteQuery.useQuery();
|
|
708
700
|
return <h1>{query.data.title}</h1>;
|
|
709
701
|
}
|
|
710
702
|
function Failure({ error }: ErrorComponentProps) {
|
|
@@ -736,28 +728,40 @@ export function App() {
|
|
|
736
728
|
|
|
737
729
|
Keep the result of `queryRoute` to call its `useRetry()` hook inside that route's error component. The returned callback refreshes the query using the current match's params, decoded search dependencies and context, then invalidates that match. A successful retry renders the route again without replacing the surrounding shell or its draft inputs.
|
|
738
730
|
|
|
739
|
-
|
|
731
|
+
State where a signed-out visitor goes once, as `redirects.signedOut` in the router's context. A query route whose query fails with the shared `Unauthorized` rejection redirects there, and `permissionGuard` takes the same callback. `signInRedirect(path)` from `@shivaedev/react/adapters/signInRedirect.ts` builds it: a TanStack redirect to `path` that carries the requested path, search and fragment in sign-in's `returnTo` search field. `redirects.refused` handles `Forbidden` and `QueryReadDenied` the same way, with a callback that returns TanStack's `redirect`. A route overrides either target for its own query with `queryRoute(buildQuery, { redirects })`, as `memberNote` does here:
|
|
740
732
|
|
|
741
733
|
```ts
|
|
742
|
-
import {
|
|
743
|
-
import
|
|
744
|
-
import {
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
734
|
+
import { createRootRouteWithContext, createRoute, createRouter } from "@tanstack/react-router";
|
|
735
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
736
|
+
import type { QueryRouteRedirects } from "@shivaedev/react/adapters/queryRouteFailure.ts";
|
|
737
|
+
import { signInRedirect } from "@shivaedev/react/adapters/signInRedirect.ts";
|
|
738
|
+
import { queryRoute } from "@shivaedev/react/client/adapters/queryRoute.ts";
|
|
739
|
+
import { api } from "./notes-client.ts";
|
|
740
|
+
|
|
741
|
+
const registry = AtomRegistry.make();
|
|
742
|
+
const root = createRootRouteWithContext<{ registry: AtomRegistry.AtomRegistry; api: typeof api; redirects: QueryRouteRedirects }>()();
|
|
743
|
+
const memberNote = createRoute({
|
|
744
|
+
getParentRoute: () => root,
|
|
745
|
+
path: "/members/$id",
|
|
746
|
+
...queryRoute(
|
|
747
|
+
({ context, params }: { context: typeof root.types.allContext; params: { id: string }; search: object }) =>
|
|
748
|
+
context.api.get.query({ id: params.id, page: 1 }),
|
|
749
|
+
{ redirects: { signedOut: signInRedirect("/members/sign-in") } },
|
|
750
|
+
),
|
|
751
|
+
});
|
|
752
|
+
const router = createRouter({
|
|
753
|
+
context: { registry, api, redirects: { signedOut: signInRedirect("/sign-in") } },
|
|
754
|
+
routeTree: root.addChildren([memberNote]),
|
|
751
755
|
});
|
|
752
756
|
```
|
|
753
757
|
|
|
754
|
-
Without
|
|
758
|
+
Without a callback at either level, failures keep their normal error or not-found handling. `NotFound` continues to use the not-found screen even when refusal redirects are configured.
|
|
755
759
|
|
|
756
760
|
For a loader that coordinates more work, use `loadQuery(registry, atom, loaderContext)` from `@shivaedev/react/adapters/loadQuery.ts` directly. It reads the atom in the supplied registry and throws typed failures unchanged, including `NotFound`; the loader decides how to handle them.
|
|
757
761
|
|
|
758
762
|
`defaultPendingMs: Infinity` keeps the current route's data visible during navigation; the shell reads TanStack's `isLoading` to display pending state. The new route commits when its loader settles. This is TanStack's pending policy, separate from `useQuery`'s retained display. The loader hands its query over to the route's component: the first render has the loaded value, and the query handler runs once, for a live subscription and for a `live: false` query alike, even when the route mounts after the registry’s idle window.
|
|
759
763
|
|
|
760
|
-
`
|
|
764
|
+
`useQuery()` reads the current match's params, decoded search, context and loader value, and exposes `data` as the query's success type, with no `Option` or Initial result. It takes the options of `useQuery` from `@shivaedev/react/client/resultState.ts`. Call it inside the route's component or a component it renders. Refetches keep the last value visible. `useQuery`, a route's `useQuery()` and a loader-backed editor’s `query` expose `denied: true` for `QueryReadDenied`, alongside the typed `cause`. Use this state to replace retained content with the application’s not-found screen after access is revoked. Refused live queries stay refused through change notifications until their input or viewer changes. For a loader-backed editor, spread the route's `useLoaded()`, which returns its atom as `query` and the loader value as `loaded`: `useEditor(api.save, { ...noteQuery.useLoaded(), fixed: { id } })`. Its form is defined on the first render and `editor.query.data` is the row directly. A loader written with `loadQuery` reads its atom with `useLoaderQuery(atom, route.useLoaderData())` from `@shivaedev/react/client/useLoaderQuery.ts`, which `useQuery()` uses underneath.
|
|
761
765
|
|
|
762
766
|
`loadQuery` mounts its query while the loader runs and, once it succeeds, for a five-second hand-over window measured on the bound query's atom runtime `Clock`. A component that mounts within the window continues the loader's subscription and its first result instead of opening another. A route that is left or never mounts releases the query when the window passes; from then on the bound query uses the same retention policy as component reads, including its expiry, capacity and session ownership. A route that mounts after the window shows that kept value while a new subscription revalidates it. A returning route whose bound query still has a kept result renders it immediately as stale. Readers without data suspend until their active loader completes. Failed loaders also release execution; the route keeps its error visible and Retry starts a new load. Already mounted readers keep their data during refreshes.
|
|
763
767
|
|
|
@@ -767,9 +771,9 @@ For a loader that coordinates more work, use `loadQuery(registry, atom, loaderCo
|
|
|
767
771
|
|
|
768
772
|
Permissions come from the session payload's `user.permissions`, populated by `@shivaedev/access` through the server's role policy. `can(session, permission)` uses access's grant evaluator without making a request: names match exactly, and an `any` grant covers an `own` check for that name. An `own` grant does not cover an `any` check. `useCan()` supplies the same check from the nearest `PermissionBoundary` for component controls. A signed-out session has no grants. These checks control visibility; the server still authorizes every request.
|
|
769
773
|
|
|
770
|
-
Declare a route's requirement in TanStack's native `staticData`: `signedIn: true` admits any signed-in viewer, and `permission` requires a specific grant. Routes that declare neither stay public. Install `permissionGuard` once as the root route's `beforeLoad`; it checks all matched routes before their loaders run, including requirements on parents. Signed-out visitors go to the
|
|
774
|
+
Declare a route's requirement in TanStack's native `staticData`: `signedIn: true` admits any signed-in viewer, and `permission` requires a specific grant. Routes that declare neither stay public. Install `permissionGuard` once as the root route's `beforeLoad`; it checks all matched routes before their loaders run, including requirements on parents. Signed-out visitors go to the guard's `signedOut` redirect; pass it the router's `redirects.signedOut` so the guard and query routes share one target. A signed-in session without a required grant throws the shared `Forbidden` failure. A signed-in-only parent does not bypass a child's permission requirement.
|
|
771
775
|
|
|
772
|
-
Set `permissionGuard({ session,
|
|
776
|
+
Set `permissionGuard({ session, signedOut, hidden: true })` to hide refused routes from signed-in members. The guard throws TanStack's native not-found result, renders the app's `notFoundComponent` at the requested URL and prevents the route's loader from running. Signed-out visitors still go to sign-in with their return location; sessions with the required grant still enter the route.
|
|
773
777
|
|
|
774
778
|
```tsx
|
|
775
779
|
import { RegistryContext } from "@effect/atom-react";
|
|
@@ -779,17 +783,23 @@ import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
|
779
783
|
import { permission } from "@shivaedev/access/permission.ts";
|
|
780
784
|
import { Forbidden } from "@shivaedev/react/adapters/Forbidden.ts";
|
|
781
785
|
import { PermissionBoundary, PermissionBoundaryError, useCan } from "@shivaedev/react/adapters/permissionBoundary.ts";
|
|
782
|
-
import { useLoaderQuery } from "@shivaedev/react/client/useLoaderQuery.ts";
|
|
783
786
|
import { permissionGuard } from "@shivaedev/react/adapters/permissionGuard.ts";
|
|
787
|
+
import type { QueryRouteRedirects } from "@shivaedev/react/adapters/queryRouteFailure.ts";
|
|
788
|
+
import { signInRedirect } from "@shivaedev/react/adapters/signInRedirect.ts";
|
|
784
789
|
import type { PermissionSession } from "@shivaedev/identity/PermissionSession.ts";
|
|
785
790
|
import { can } from "@shivaedev/react/can.ts";
|
|
786
|
-
import { queryRoute } from "@shivaedev/react/adapters/queryRoute.ts";
|
|
791
|
+
import { queryRoute } from "@shivaedev/react/client/adapters/queryRoute.ts";
|
|
787
792
|
import { api, registry } from "./notes-client.ts";
|
|
788
793
|
import { auth, SignIn } from "./auth.tsx";
|
|
789
794
|
|
|
790
795
|
const ReadNotes = permission("notes.read");
|
|
791
|
-
const root = createRootRouteWithContext<{
|
|
792
|
-
|
|
796
|
+
const root = createRootRouteWithContext<{
|
|
797
|
+
registry: AtomRegistry.AtomRegistry;
|
|
798
|
+
api: typeof api;
|
|
799
|
+
redirects: Required<Pick<QueryRouteRedirects, "signedOut">>;
|
|
800
|
+
session: PermissionSession | undefined;
|
|
801
|
+
}>()({
|
|
802
|
+
beforeLoad: ({ context, ...route }) => permissionGuard({ session: () => context.session, signedOut: context.redirects.signedOut })(route),
|
|
793
803
|
});
|
|
794
804
|
const profile = createRoute({
|
|
795
805
|
getParentRoute: () => root,
|
|
@@ -797,15 +807,17 @@ const profile = createRoute({
|
|
|
797
807
|
staticData: { signedIn: true },
|
|
798
808
|
component: () => <h1>Profile</h1>,
|
|
799
809
|
});
|
|
810
|
+
const noteQuery = queryRoute(
|
|
811
|
+
({ context, params }: { context: typeof root.types.allContext; params: { id: string }; search: Record<string, never> }) =>
|
|
812
|
+
context.api.get.query({ id: params.id, page: 1 }),
|
|
813
|
+
);
|
|
800
814
|
const note = createRoute({
|
|
801
815
|
getParentRoute: () => root,
|
|
802
816
|
path: "/notes/$id",
|
|
803
817
|
staticData: { permission: ReadNotes },
|
|
804
|
-
...
|
|
805
|
-
context.api.get.query({ id: params.id, page: 1 }),
|
|
806
|
-
),
|
|
818
|
+
...noteQuery,
|
|
807
819
|
component: () => {
|
|
808
|
-
const query =
|
|
820
|
+
const query = noteQuery.useQuery();
|
|
809
821
|
return <h1>{query.data.title}</h1>;
|
|
810
822
|
},
|
|
811
823
|
});
|
|
@@ -821,7 +833,7 @@ const login = createRoute({
|
|
|
821
833
|
},
|
|
822
834
|
});
|
|
823
835
|
const router = createRouter({
|
|
824
|
-
context: { registry, api, session: auth.session },
|
|
836
|
+
context: { registry, api, redirects: { signedOut: signInRedirect("/login") }, session: auth.session },
|
|
825
837
|
InnerWrap: PermissionShell,
|
|
826
838
|
defaultErrorComponent: PermissionBoundaryError,
|
|
827
839
|
routeTree: root.addChildren([profile, note, login]),
|
|
@@ -833,7 +845,7 @@ function Controls() {
|
|
|
833
845
|
return can(ReadNotes) ? <button type="button">Read notes</button> : null;
|
|
834
846
|
}
|
|
835
847
|
|
|
836
|
-
function PermissionShell({ children }: { readonly children: ReactNode })
|
|
848
|
+
function PermissionShell({ children }: { readonly children: ReactNode }) {
|
|
837
849
|
const session = useRouter<typeof router>().options.context?.session;
|
|
838
850
|
return (
|
|
839
851
|
<PermissionBoundary session={session} fallback={<NoAccess />}>
|
|
@@ -865,7 +877,7 @@ const showNotes = can(auth.session, ReadNotes);
|
|
|
865
877
|
|
|
866
878
|
`auth` owns the current session and passes it to `App`; `SignIn` updates that session before invoking `onSignedIn`. Use the session's registry for router context and React, as with other query routes. Importing `permissionGuard` augments TanStack's `StaticDataRouteOption`; route options and their inference remain native. The guard returns `void` explicitly, so `beforeLoad` needs no return annotation, including when it reads the session from route context.
|
|
867
879
|
|
|
868
|
-
`InnerWrap` puts `PermissionBoundary` inside the router
|
|
880
|
+
`InnerWrap` puts `PermissionBoundary` inside the router, and the boundary's fallback can render `Link` and read route context. TanStack types `InnerWrap` as a component that returns an element, so `PermissionShell` leaves its return type to inference; annotated as returning `ReactNode`, it does not compile. Set `defaultErrorComponent: PermissionBoundaryError` so route and loader failures reach the boundary's fallback while TanStack keeps its navigation controller mounted. This lets a refused viewer follow the fallback link without reloading the page. Keep `disableGlobalCatchBoundary: true`; other errors propagate to an enclosing application boundary. A route-specific `errorComponent` owns any failures it consumes.
|
|
869
881
|
|
|
870
882
|
`Forbidden` from `@shivaedev/react/adapters/Forbidden.ts` supplies the no-access heading, explanation and a router link. Set `to` to a route the viewer may open. Optional `title`, `children`, `backLabel` and `className` let the application choose wording and layout. The default heading is “Permission denied” and the link says “Go back”.
|
|
871
883
|
|
|
@@ -930,7 +942,7 @@ For other Better Auth reads, `authQuery` from `@shivaedev/react/client/authQuery
|
|
|
930
942
|
|
|
931
943
|
Pass the auth owner’s sign-out atom to `SessionBoundary` as `signOut={signOut}`. The boundary mounts it in the auth owner’s registry and unmounts the signed-in tree while sign-out is pending, including the following session refetch. A successful action keeps the tree held until the session clears; a failed action releases the hold. Supply an atom for the sign-out request itself, and give `recheck` the auth owner’s session refresh. After the request succeeds, the boundary starts that check and retries failures while holding the tree. A failed session check never releases a successful sign-out’s hold.
|
|
932
944
|
|
|
933
|
-
For an explicit sign-out in a TanStack route, use `useSignOut(signOut, { signIn: "/sign-in" })` from `@shivaedev/react/client/adapters/useSignOut.ts` in place of `useAction`. It exposes the same state and a `dispatch()` handler. The hook records sign-out intent before the request and navigates to sign-in after success. A refused request clears that intent, including when the boundary remounts its route. Repeated clicks while pending share one request and follow-up. Unmounting the component or disposing its registry stops the follow-up navigation. If a session refresh replaces the component before the response, the replacement session’s route policy owns navigation to sign-in. `
|
|
945
|
+
For an explicit sign-out in a TanStack route, use `useSignOut(signOut, { signIn: "/sign-in" })` from `@shivaedev/react/client/adapters/useSignOut.ts` in place of `useAction`. It exposes the same state and a `dispatch()` handler. The hook records sign-out intent before the request and navigates to sign-in after success. A refused request clears that intent, including when the boundary remounts its route. Repeated clicks while pending share one request and follow-up. Unmounting the component or disposing its registry stops the follow-up navigation. If a session refresh replaces the component before the response, the replacement session’s route policy owns navigation to sign-in. `signInRedirect` and `signInSearch(location)` omit `returnTo` for that intent, including when the auth owner's session refresh reruns guards first. Use `signInSearch` for module sign-in links too; ordinary visits retain the full requested location. Applications do not need history markers or Better Auth completion timing hooks.
|
|
934
946
|
|
|
935
947
|
### 3. Every day
|
|
936
948
|
|
|
@@ -1048,22 +1060,49 @@ These lines belong inside a component; `text` and `ClipboardDenied` belong to th
|
|
|
1048
1060
|
|
|
1049
1061
|
#### Render tests in happy-dom
|
|
1050
1062
|
|
|
1051
|
-
Use `@shivaedev/test-story/client/adapters/react.ts` with Vitest's `happy-dom` environment. Install contract and
|
|
1063
|
+
Use `@shivaedev/test-story/client/adapters/react.ts` with Vitest's `happy-dom` environment. Install changes, contract and identity for the module's keys, operations, middleware and client binding, and the testing peers and environment in your app's dev dependencies:
|
|
1052
1064
|
|
|
1053
1065
|
```sh
|
|
1054
|
-
pnpm add @shivaedev/changes @shivaedev/contract
|
|
1066
|
+
pnpm add @shivaedev/changes @shivaedev/contract @shivaedev/identity
|
|
1055
1067
|
pnpm add -D @shivaedev/test-story @effect/platform-node @effect/vitest vitest happy-dom react-dom @types/react-dom
|
|
1056
1068
|
```
|
|
1057
1069
|
|
|
1058
|
-
Configure Vitest with `test: { environment: "happy-dom" }`, or select that environment for your DOM test project.
|
|
1070
|
+
Configure Vitest with `test: { environment: "happy-dom" }`, or select that environment for your DOM test project. The screen below reads a defined `notes` module, whose operations yield a `NoteTitles` service and run behind identity's `Authenticated` middleware:
|
|
1071
|
+
|
|
1072
|
+
```ts
|
|
1073
|
+
import { defineModule } from "@shivaedev/contract/defineModule.ts";
|
|
1074
|
+
import { command, query } from "@shivaedev/contract/operation.ts";
|
|
1075
|
+
import { Authenticated } from "@shivaedev/identity/middleware.ts";
|
|
1076
|
+
import { notesWire } from "#generated/modules/notes/Notes.ts";
|
|
1077
|
+
import { NoteTitle, notes } from "#modules/notes/model.ts";
|
|
1078
|
+
import { NoteTitles } from "#modules/notes/services.ts";
|
|
1079
|
+
|
|
1080
|
+
const List = query("list", {
|
|
1081
|
+
reads: () => [notes.list],
|
|
1082
|
+
*run() {
|
|
1083
|
+
return yield* (yield* NoteTitles).all;
|
|
1084
|
+
},
|
|
1085
|
+
});
|
|
1086
|
+
const Add = command("add", {
|
|
1087
|
+
invalidates: () => [notes.list],
|
|
1088
|
+
payload: NoteTitle,
|
|
1089
|
+
*run({ title }) {
|
|
1090
|
+
yield* (yield* NoteTitles).add(title);
|
|
1091
|
+
},
|
|
1092
|
+
});
|
|
1093
|
+
export const Notes = defineModule({ middleware: Authenticated, name: "notes", operations: [List, Add], wire: notesWire });
|
|
1094
|
+
```
|
|
1095
|
+
|
|
1096
|
+
`model.ts` declares `notes` as `collection("notes", Schema.Number)` and `NoteTitle` as `Schema.Struct({ title: Schema.String })`, and `services.ts` declares `NoteTitles` with an `all` Effect of titles and an `add(title)` method. Keep the engine, rendering and verbs in a story kit under `src/test-support/notesScreen.tsx`. This kit serves the module's own handlers from `moduleHandlers` over native RPC, with an in-memory `NoteTitles` and a fake `Authenticated` that signs alice in, binds the generated contract and gives the screen driver the registry's pending signal:
|
|
1059
1097
|
|
|
1060
1098
|
```tsx
|
|
1061
|
-
import { Effect, Layer, Option, Ref
|
|
1099
|
+
import { Effect, Layer, Option, Ref } from "effect";
|
|
1062
1100
|
import { act, createElement } from "react";
|
|
1063
|
-
import { collection } from "@shivaedev/changes/keys.ts";
|
|
1064
1101
|
import { makeClient } from "@shivaedev/contract/bind.ts";
|
|
1065
|
-
import {
|
|
1066
|
-
import {
|
|
1102
|
+
import { moduleHandlers } from "@shivaedev/contract/moduleHandlers.ts";
|
|
1103
|
+
import { CurrentViewer } from "@shivaedev/contract/readPolicy.ts";
|
|
1104
|
+
import { Identity } from "@shivaedev/identity/identity.ts";
|
|
1105
|
+
import { Authenticated } from "@shivaedev/identity/middleware.ts";
|
|
1067
1106
|
import { layerMemoryProtocol } from "@shivaedev/test-kits/contract/testing.ts";
|
|
1068
1107
|
import { screenPending } from "@shivaedev/react/adapters/screenPending.ts";
|
|
1069
1108
|
import { useAction, useQuery } from "@shivaedev/react/client/resultState.ts";
|
|
@@ -1071,33 +1110,29 @@ import { render } from "@shivaedev/test-story/client/adapters/react.ts";
|
|
|
1071
1110
|
import { screenDriver } from "@shivaedev/test-story/server/screenDriver.ts";
|
|
1072
1111
|
import { storyKit } from "@shivaedev/test-story/server/storyKit.ts";
|
|
1073
1112
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
const Add = command("add", { invalidates: () => [notes.list], payload: { title: Schema.String } });
|
|
1078
|
-
const Notes = contract("notes", { commands: [Add], queries: [List] });
|
|
1113
|
+
import { Notes } from "#generated/modules/notes/Notes.ts";
|
|
1114
|
+
import { Notes as NotesModule } from "#modules/notes/Notes.ts";
|
|
1115
|
+
import { NoteTitles } from "#modules/notes/services.ts";
|
|
1079
1116
|
|
|
1080
1117
|
export const notesScreen = storyKit({
|
|
1081
1118
|
*create() {
|
|
1082
|
-
const
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
});
|
|
1090
|
-
}),
|
|
1119
|
+
const stored = yield* Ref.make<readonly string[]>([]);
|
|
1120
|
+
const titles = Layer.succeed(NoteTitles, {
|
|
1121
|
+
add: (title) => Ref.update(stored, (all) => [...all, title]),
|
|
1122
|
+
all: Ref.get(stored),
|
|
1123
|
+
});
|
|
1124
|
+
const signedIn = Layer.succeed(Authenticated, (effect) =>
|
|
1125
|
+
effect.pipe(Effect.provideService(CurrentViewer, Option.some({ id: "alice" })), Effect.provideService(Identity, { id: "alice" })),
|
|
1091
1126
|
);
|
|
1092
|
-
const
|
|
1127
|
+
const handlers = moduleHandlers(NotesModule).pipe(Layer.provide(titles));
|
|
1128
|
+
const { api } = makeClient(Notes, {
|
|
1093
1129
|
live: false,
|
|
1094
|
-
protocol: layerMemoryProtocol(
|
|
1130
|
+
protocol: layerMemoryProtocol(NotesModule.application).pipe(Layer.provide([handlers, signedIn])),
|
|
1095
1131
|
url: "memory://notes",
|
|
1096
1132
|
});
|
|
1097
|
-
const add = Client.runtime.fn(api.add.run);
|
|
1098
1133
|
function NotesView() {
|
|
1099
1134
|
const list = useQuery(api.list.query());
|
|
1100
|
-
const
|
|
1135
|
+
const add = useAction(api.add);
|
|
1101
1136
|
return createElement(
|
|
1102
1137
|
"section",
|
|
1103
1138
|
null,
|
|
@@ -1105,8 +1140,8 @@ export const notesScreen = storyKit({
|
|
|
1105
1140
|
createElement(
|
|
1106
1141
|
"button",
|
|
1107
1142
|
{
|
|
1108
|
-
disabled: list.pending ||
|
|
1109
|
-
onClick: () =>
|
|
1143
|
+
disabled: list.pending || add.pending,
|
|
1144
|
+
onClick: () => add.dispatch({ title: "Morning walk" }),
|
|
1110
1145
|
type: "button",
|
|
1111
1146
|
},
|
|
1112
1147
|
"Add",
|
|
@@ -1160,7 +1195,7 @@ notesScreen.it("a command refetches the rendered notes through native RPC", [],
|
|
|
1160
1195
|
|
|
1161
1196
|
`render(node, options?)` returns the attached `container`, its fresh native `registry`, `render(nextNode)` to update the same root and registry, and `unmount()` to release the root, registry and container. The options are React's own `RootOptions`, passed to `createRoot`, including `onCaughtError`, `onUncaughtError` and `onRecoverableError`. Acquire the view with `Effect.acquireRelease` so its scope releases it on success, failure or interruption. The screen driver's `act` adapter keeps component interactions and settling inside React's `act`. `expectNoReactWarnings()` registers a Vitest hook that fails a test if it logged to `console.error`.
|
|
1162
1197
|
|
|
1163
|
-
The
|
|
1198
|
+
The kit runs the module's real operations, read policies included, and replaces only the service they yield and the session. `moduleHandlers` requires exactly the services the operations yield beyond the middleware, so the compiler names one the kit forgets. The fake middleware provides the viewer the default `signedIn` policy checks and the `Identity` the operations may yield; without a viewer, the list answers `QueryReadDenied`. The starter's [notes screen kit](https://github.com/ShivaeDev/platform/blob/main/examples/starter/src/modules/notes/test-support/client.ts) fakes its middleware the same way. A story about persistence or the real session runs the operations through the application host, as the starter's application kit does.
|
|
1164
1199
|
|
|
1165
1200
|
For a boundary fallback test, keep the warnings guard and declare the caught error inside the test. `expectCaughtError(predicate)` returns an `onCaughtError` handler that accepts matching errors without logging them. The guard fails if the handler receives any other error or the expected error never arrives. Other console errors and React warnings still fail the guard.
|
|
1166
1201
|
|
|
@@ -1217,7 +1252,7 @@ pnpm add @shivaedev/react @effect/atom-react effect react
|
|
|
1217
1252
|
pnpm add @shivaedev/form
|
|
1218
1253
|
```
|
|
1219
1254
|
|
|
1220
|
-
Add the optional router peer when using `queryRoute`, `loadQuery`, `permissionGuard`, `PermissionBoundary`, `PermissionBoundaryError`, `Forbidden` or `useSignOut`:
|
|
1255
|
+
Add the optional router peer when using `queryRoute`, `loadQuery`, `permissionGuard`, `signInRedirect`, `PermissionBoundary`, `PermissionBoundaryError`, `Forbidden` or `useSignOut`:
|
|
1221
1256
|
|
|
1222
1257
|
```sh
|
|
1223
1258
|
pnpm add @tanstack/react-router
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
2
2
|
import type { PermissionSession } from "@shivaedev/identity/PermissionSession.ts";
|
|
3
|
-
import {
|
|
3
|
+
import type { QueryRouteRedirect } from "#adapters/queryRouteFailure.ts";
|
|
4
|
+
import type { SignInLocation } from "#signInSearch.ts";
|
|
4
5
|
declare module "@tanstack/react-router" {
|
|
5
6
|
interface StaticDataRouteOption {
|
|
6
7
|
readonly permission?: Permission;
|
|
@@ -10,7 +11,7 @@ declare module "@tanstack/react-router" {
|
|
|
10
11
|
export interface PermissionGuardOptions {
|
|
11
12
|
readonly hidden?: boolean;
|
|
12
13
|
readonly session: () => PermissionSession | null | undefined;
|
|
13
|
-
readonly
|
|
14
|
+
readonly signedOut: (location: SignInLocation) => QueryRouteRedirect;
|
|
14
15
|
}
|
|
15
16
|
export interface PermissionGuardContext {
|
|
16
17
|
readonly location: SignInLocation;
|
|
@@ -21,5 +22,5 @@ export interface PermissionGuardContext {
|
|
|
21
22
|
};
|
|
22
23
|
}[];
|
|
23
24
|
}
|
|
24
|
-
export declare function permissionGuard({ session,
|
|
25
|
+
export declare function permissionGuard({ session, signedOut, hidden }: PermissionGuardOptions): (context: PermissionGuardContext) => void;
|
|
25
26
|
//# sourceMappingURL=permissionGuard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionGuard.d.ts","sourceRoot":"","sources":["../../src/adapters/permissionGuard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;
|
|
1
|
+
{"version":3,"file":"permissionGuard.d.ts","sourceRoot":"","sources":["../../src/adapters/permissionGuard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,QAAQ,wBAAwB,CAAC;IACvC,UAAU,qBAAqB;QAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;QACjC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAC5B;CACD;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,kBAAkB,CAAC;CACrE;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE;YAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;YAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;SAAE,CAAA;KAAE,EAAE,CAAC;CACxH;AAED,wBAAgB,eAAe,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,MAAc,EAAE,EAAE,sBAAsB,GAAG,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,CAgBzI"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { notFound
|
|
1
|
+
import { notFound } from "@tanstack/react-router";
|
|
2
2
|
import { forbidden } from "@shivaedev/access/refusal.ts";
|
|
3
3
|
import { can } from "#can.ts";
|
|
4
|
-
|
|
5
|
-
export function permissionGuard({ session, signIn, hidden = false }) {
|
|
4
|
+
export function permissionGuard({ session, signedOut, hidden = false }) {
|
|
6
5
|
return ({ matches, location }) => {
|
|
7
6
|
const required = matches.flatMap((match) => (match.staticData.permission === undefined ? [] : [match.staticData.permission]));
|
|
8
7
|
if (required.length === 0 && !matches.some((match) => match.staticData.signedIn === true)) {
|
|
@@ -10,7 +9,7 @@ export function permissionGuard({ session, signIn, hidden = false }) {
|
|
|
10
9
|
}
|
|
11
10
|
const current = session();
|
|
12
11
|
if (current === null || current === undefined) {
|
|
13
|
-
throw
|
|
12
|
+
throw signedOut(location);
|
|
14
13
|
}
|
|
15
14
|
for (const permission of required) {
|
|
16
15
|
if (!can(current, permission)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionGuard.js","sourceRoot":"","sources":["../../src/adapters/permissionGuard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"permissionGuard.js","sourceRoot":"","sources":["../../src/adapters/permissionGuard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAGzD,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAqB9B,MAAM,UAAU,eAAe,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK,EAA0B;IAC7F,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;QAChC,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC9H,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,QAAQ,KAAK,IAAI,CAAC,EAAE,CAAC;YAC3F,OAAO;QACR,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,EAAE,CAAC;QAC1B,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC/C,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC;QAC3B,CAAC;QACD,KAAK,MAAM,UAAU,IAAI,QAAQ,EAAE,CAAC;YACnC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC,EAAE,CAAC;gBAC/B,MAAM,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;YACnD,CAAC;QACF,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { QueryRouteRedirect } from "#adapters/queryRouteFailure.ts";
|
|
2
|
+
import { type SignInLocation } from "#signInSearch.ts";
|
|
3
|
+
export declare function signInRedirect(to: string): (location: SignInLocation) => QueryRouteRedirect;
|
|
4
|
+
//# sourceMappingURL=signInRedirect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signInRedirect.d.ts","sourceRoot":"","sources":["../../src/adapters/signInRedirect.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,KAAK,cAAc,EAAgB,MAAM,kBAAkB,CAAC;AAErE,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,CAAC,QAAQ,EAAE,cAAc,KAAK,kBAAkB,CAE3F"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signInRedirect.js","sourceRoot":"","sources":["../../src/adapters/signInRedirect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD,OAAO,EAAuB,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErE,MAAM,UAAU,cAAc,CAAC,EAAU;IACxC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC"}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type LoaderFnContext } from "@tanstack/react-router";
|
|
2
2
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
5
|
import { type QueryRouteRedirects } from "#adapters/queryRouteFailure.ts";
|
|
6
|
+
import type { QueryOptions } from "#client/resultState.ts";
|
|
7
|
+
import { type LoadedQueryState } from "#client/useLoaderQuery.ts";
|
|
6
8
|
interface QueryRouteInput<TParams, TSearch, TContext> {
|
|
7
9
|
readonly context: TContext;
|
|
8
10
|
readonly params: TParams;
|
|
9
11
|
readonly search: TSearch;
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
readonly
|
|
13
|
-
} = {
|
|
13
|
+
interface QueryRouteContext {
|
|
14
|
+
readonly redirects?: QueryRouteRedirects;
|
|
14
15
|
readonly registry: AtomRegistry.AtomRegistry;
|
|
15
|
-
}
|
|
16
|
+
}
|
|
17
|
+
export declare function queryRoute<TParams extends object, TSearch extends object, A, E, TContext extends QueryRouteContext = QueryRouteContext>(query: (route: QueryRouteInput<TParams, TSearch, TContext>) => Atom.Atom<AsyncResult.AsyncResult<A, E>>, options?: {
|
|
16
18
|
readonly redirects?: QueryRouteRedirects;
|
|
17
19
|
}): {
|
|
18
20
|
loader: (context: Pick<LoaderFnContext, "abortController" | "location"> & {
|
|
@@ -23,6 +25,11 @@ export declare function queryRoute<TParams extends object, TSearch extends objec
|
|
|
23
25
|
loaderDeps: ({ search }: {
|
|
24
26
|
readonly search: TSearch;
|
|
25
27
|
}) => TSearch;
|
|
28
|
+
useLoaded: () => {
|
|
29
|
+
readonly loaded: A;
|
|
30
|
+
readonly query: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
31
|
+
};
|
|
32
|
+
useQuery: (queryOptions?: QueryOptions<E>) => LoadedQueryState<A, E>;
|
|
26
33
|
useRetry: () => () => Promise<void>;
|
|
27
34
|
};
|
|
28
35
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryRoute.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/queryRoute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,eAAe,EAAY,MAAM,wBAAwB,CAAC;AACxF,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAEpE,OAAO,EAAE,KAAK,mBAAmB,EAAqB,MAAM,gCAAgC,CAAC;AAE7F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,KAAK,gBAAgB,EAAkB,MAAM,2BAA2B,CAAC;AAElF,UAAU,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ;IACnD,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CACzB;AAED,UAAU,iBAAiB;IAC1B,QAAQ,CAAC,SAAS,CAAC,EAAE,mBAAmB,CAAC;IACzC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC,YAAY,CAAC;CAC7C;AAWD,wBAAgB,UAAU,CAAC,OAAO,SAAS,MAAM,EAAE,OAAO,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,QAAQ,SAAS,iBAAiB,GAAG,iBAAiB,EACtI,KAAK,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EACvG,OAAO,GAAE;IAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,mBAAmB,CAAA;CAAO;IAGzD,MAAM,YACI,IAAI,CAAC,eAAe,EAAE,iBAAiB,GAAG,UAAU,CAAC,GAAG;QAChE,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;QAC3B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;QACvB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;KACzB,KACC,OAAO,CAAC,CAAC,CAAC;IAOb,UAAU,eAAe;QAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;KAAE;IACrD,SAAS;;;;IACT,QAAQ,kBAAiB,YAAY,CAAC,CAAC,CAAC,KAAQ,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC;IAItE,QAAQ;EAMT"}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { useMatch } from "@tanstack/react-router";
|
|
1
2
|
import { loadQuery } from "#adapters/loadQuery.ts";
|
|
2
3
|
import { queryRouteFailure } from "#adapters/queryRouteFailure.ts";
|
|
3
4
|
import { useQueryRetry } from "#adapters/useQueryRetry.ts";
|
|
5
|
+
import { useLoaderQuery } from "#client/useLoaderQuery.ts";
|
|
6
|
+
function useLoadedQuery(query) {
|
|
7
|
+
const match = useMatch({ strict: false });
|
|
8
|
+
const { context, params, loaderDeps } = match;
|
|
9
|
+
const loaded = match.loaderData;
|
|
10
|
+
return { loaded, query: query({ context, params, search: loaderDeps }) };
|
|
11
|
+
}
|
|
4
12
|
export function queryRoute(query, options = {}) {
|
|
5
13
|
return {
|
|
6
14
|
loader: async (context) => {
|
|
@@ -8,10 +16,15 @@ export function queryRoute(query, options = {}) {
|
|
|
8
16
|
return await loadQuery(context.context.registry, query({ context: context.context, params: context.params, search: context.deps }), context);
|
|
9
17
|
}
|
|
10
18
|
catch (error) {
|
|
11
|
-
throw queryRouteFailure(error, context.location, options.redirects);
|
|
19
|
+
throw queryRouteFailure(error, context.location, { ...context.context.redirects, ...options.redirects });
|
|
12
20
|
}
|
|
13
21
|
},
|
|
14
22
|
loaderDeps: ({ search }) => search,
|
|
23
|
+
useLoaded: () => useLoadedQuery(query),
|
|
24
|
+
useQuery: (queryOptions = {}) => {
|
|
25
|
+
const { loaded, query: atom } = useLoadedQuery(query);
|
|
26
|
+
return useLoaderQuery(atom, loaded, queryOptions);
|
|
27
|
+
},
|
|
15
28
|
useRetry: () => useQueryRetry((match) => {
|
|
16
29
|
const { context, params, loaderDeps } = match;
|
|
17
30
|
context.registry.refresh(query({ context, params, search: loaderDeps }));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryRoute.js","sourceRoot":"","sources":["../../../src/client/adapters/queryRoute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwC,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAIxF,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAA4B,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAyB,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAalF,SAAS,cAAc,CACtB,KAAuG;IAEvG,MAAM,KAAK,GAAG,QAAQ,CAA8B,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IACvE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,GAAgE,KAAK,CAAC;IAC3G,MAAM,MAAM,GAAM,KAAK,CAAC,UAAU,CAAC;IACnC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;AAC1E,CAAC;AAED,MAAM,UAAU,UAAU,CACzB,KAAuG,EACvG,OAAO,GAAiD,EAAE;IAE1D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|
|
15
15
|
}
|
|
16
16
|
export function useDebouncedValue(value, duration, runtime = defaultRuntime) {
|
|
17
17
|
const registry = useContext(RegistryContext);
|
|
18
|
-
const millis = Duration.toMillis(
|
|
18
|
+
const millis = Duration.toMillis(duration);
|
|
19
19
|
const [held, hold] = useState(() => ({ ...debounce(value, millis, runtime), registry, runtime }));
|
|
20
20
|
let owner = held;
|
|
21
21
|
if (held.registry !== registry || held.runtime !== runtime) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedValue.js","sourceRoot":"","sources":["../../src/client/useDebouncedValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAExD,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAGjD,SAAS,QAAQ,CAAI,KAAQ,EAAE,MAAc,EAAE,OAAqB;IACnE,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IAC3D,MAAM,MAAM,GAAG,OAAO;SACpB,IAAI,CACJ,CAAC,GAAG,EAAE,EAAE;QACP,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,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,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7D,CAAC;IACF,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IACrC,OAAO,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;AAC5C,CAAC"}
|
package/dist/client/useDelay.js
CHANGED
|
@@ -5,7 +5,7 @@ import { useCallback, useContext, useEffect, useMemo } from "react";
|
|
|
5
5
|
const defaultRuntime = Atom.runtime(Layer.empty);
|
|
6
6
|
export function useDelay(duration, runtime = defaultRuntime) {
|
|
7
7
|
const registry = useContext(RegistryContext);
|
|
8
|
-
const millis = Duration.toMillis(
|
|
8
|
+
const millis = Duration.toMillis(duration);
|
|
9
9
|
const timer = useMemo(() => runtime.fn((complete) => Effect.andThen(Effect.sleep(millis), Effect.sync(complete))).pipe(Atom.setIdleTTL(0)), [runtime, millis]);
|
|
10
10
|
const start = useCallback((complete) => registry.set(timer, complete), [registry, timer]);
|
|
11
11
|
const result = useAtomValue(timer);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDelay.js","sourceRoot":"","sources":["../../src/client/useDelay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEpE,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAGjD,MAAM,UAAU,QAAQ,CAAC,QAAwB,EAAE,OAA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AAK,CAAC,CAAC,CAAC;IACtG,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IACnC,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;IACzF,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAClC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.0",
|
|
4
4
|
"description": "React query, action, editor and session hooks for native Effect atoms",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@shivaedev/access": "0.6.6",
|
|
49
|
-
"@shivaedev/contract": "0.
|
|
49
|
+
"@shivaedev/contract": "0.51.0",
|
|
50
50
|
"@shivaedev/errors": "0.14.0",
|
|
51
51
|
"@shivaedev/form": "0.20.0",
|
|
52
|
-
"@shivaedev/identity": "0.34.
|
|
52
|
+
"@shivaedev/identity": "0.34.2"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@effect/atom-react": "4.1.4",
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"@effect/sql-sqlite-node": "4.1.4",
|
|
59
59
|
"@shivaedev/changes": "0.14.1",
|
|
60
60
|
"@shivaedev/jobs": "0.43.1",
|
|
61
|
-
"@shivaedev/quality": "0.
|
|
62
|
-
"@shivaedev/server": "0.38.
|
|
61
|
+
"@shivaedev/quality": "0.79.0",
|
|
62
|
+
"@shivaedev/server": "0.38.1",
|
|
63
63
|
"@shivaedev/service": "0.9.2",
|
|
64
64
|
"@shivaedev/sql": "0.52.0",
|
|
65
|
-
"@shivaedev/test-kits": "0.13.
|
|
65
|
+
"@shivaedev/test-kits": "0.13.6",
|
|
66
66
|
"@shivaedev/test-sql": "0.25.5",
|
|
67
|
-
"@shivaedev/test-story": "0.
|
|
67
|
+
"@shivaedev/test-story": "0.50.0",
|
|
68
68
|
"@tanstack/react-router": "1.170.46",
|
|
69
69
|
"@types/node": "24.19.2",
|
|
70
70
|
"@types/react": "19.3.0",
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { notFound
|
|
1
|
+
import { notFound } from "@tanstack/react-router";
|
|
2
2
|
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
3
3
|
import { forbidden } from "@shivaedev/access/refusal.ts";
|
|
4
4
|
import type { PermissionSession } from "@shivaedev/identity/PermissionSession.ts";
|
|
5
|
+
import type { QueryRouteRedirect } from "#adapters/queryRouteFailure.ts";
|
|
5
6
|
import { can } from "#can.ts";
|
|
6
|
-
import {
|
|
7
|
+
import type { SignInLocation } from "#signInSearch.ts";
|
|
7
8
|
|
|
8
9
|
declare module "@tanstack/react-router" {
|
|
9
10
|
interface StaticDataRouteOption {
|
|
@@ -15,7 +16,7 @@ declare module "@tanstack/react-router" {
|
|
|
15
16
|
export interface PermissionGuardOptions {
|
|
16
17
|
readonly hidden?: boolean;
|
|
17
18
|
readonly session: () => PermissionSession | null | undefined;
|
|
18
|
-
readonly
|
|
19
|
+
readonly signedOut: (location: SignInLocation) => QueryRouteRedirect;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export interface PermissionGuardContext {
|
|
@@ -23,7 +24,7 @@ export interface PermissionGuardContext {
|
|
|
23
24
|
readonly matches: readonly { readonly staticData: { readonly permission?: Permission; readonly signedIn?: boolean } }[];
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function permissionGuard({ session,
|
|
27
|
+
export function permissionGuard({ session, signedOut, hidden = false }: PermissionGuardOptions): (context: PermissionGuardContext) => void {
|
|
27
28
|
return ({ matches, location }) => {
|
|
28
29
|
const required = matches.flatMap((match) => (match.staticData.permission === undefined ? [] : [match.staticData.permission]));
|
|
29
30
|
if (required.length === 0 && !matches.some((match) => match.staticData.signedIn === true)) {
|
|
@@ -31,7 +32,7 @@ export function permissionGuard({ session, signIn, hidden = false }: PermissionG
|
|
|
31
32
|
}
|
|
32
33
|
const current = session();
|
|
33
34
|
if (current === null || current === undefined) {
|
|
34
|
-
throw
|
|
35
|
+
throw signedOut(location);
|
|
35
36
|
}
|
|
36
37
|
for (const permission of required) {
|
|
37
38
|
if (!can(current, permission)) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { redirect } from "@tanstack/react-router";
|
|
2
|
+
import type { QueryRouteRedirect } from "#adapters/queryRouteFailure.ts";
|
|
3
|
+
import { type SignInLocation, signInSearch } from "#signInSearch.ts";
|
|
4
|
+
|
|
5
|
+
export function signInRedirect(to: string): (location: SignInLocation) => QueryRouteRedirect {
|
|
6
|
+
return (location) => redirect({ search: signInSearch(location), to });
|
|
7
|
+
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnyRouter, type LoaderFnContext, useMatch } from "@tanstack/react-router";
|
|
2
2
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
5
|
import { loadQuery } from "#adapters/loadQuery.ts";
|
|
6
6
|
import { type QueryRouteRedirects, queryRouteFailure } from "#adapters/queryRouteFailure.ts";
|
|
7
7
|
import { useQueryRetry } from "#adapters/useQueryRetry.ts";
|
|
8
|
+
import type { QueryOptions } from "#client/resultState.ts";
|
|
9
|
+
import { type LoadedQueryState, useLoaderQuery } from "#client/useLoaderQuery.ts";
|
|
8
10
|
|
|
9
11
|
interface QueryRouteInput<TParams, TSearch, TContext> {
|
|
10
12
|
readonly context: TContext;
|
|
@@ -12,13 +14,21 @@ interface QueryRouteInput<TParams, TSearch, TContext> {
|
|
|
12
14
|
readonly search: TSearch;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
>
|
|
17
|
+
interface QueryRouteContext {
|
|
18
|
+
readonly redirects?: QueryRouteRedirects;
|
|
19
|
+
readonly registry: AtomRegistry.AtomRegistry;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function useLoadedQuery<TParams, TSearch, A, E, TContext>(
|
|
23
|
+
query: (route: QueryRouteInput<TParams, TSearch, TContext>) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
24
|
+
): { readonly loaded: A; readonly query: Atom.Atom<AsyncResult.AsyncResult<A, E>> } {
|
|
25
|
+
const match = useMatch<AnyRouter, undefined, false>({ strict: false });
|
|
26
|
+
const { context, params, loaderDeps }: { context: TContext; params: TParams; loaderDeps: TSearch } = match;
|
|
27
|
+
const loaded: A = match.loaderData;
|
|
28
|
+
return { loaded, query: query({ context, params, search: loaderDeps }) };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function queryRoute<TParams extends object, TSearch extends object, A, E, TContext extends QueryRouteContext = QueryRouteContext>(
|
|
22
32
|
query: (route: QueryRouteInput<TParams, TSearch, TContext>) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
23
33
|
options: { readonly redirects?: QueryRouteRedirects } = {},
|
|
24
34
|
) {
|
|
@@ -33,10 +43,15 @@ export function queryRoute<
|
|
|
33
43
|
try {
|
|
34
44
|
return await loadQuery(context.context.registry, query({ context: context.context, params: context.params, search: context.deps }), context);
|
|
35
45
|
} catch (error) {
|
|
36
|
-
throw queryRouteFailure(error, context.location, options.redirects);
|
|
46
|
+
throw queryRouteFailure(error, context.location, { ...context.context.redirects, ...options.redirects });
|
|
37
47
|
}
|
|
38
48
|
},
|
|
39
49
|
loaderDeps: ({ search }: { readonly search: TSearch }) => search,
|
|
50
|
+
useLoaded: () => useLoadedQuery(query),
|
|
51
|
+
useQuery: (queryOptions: QueryOptions<E> = {}): LoadedQueryState<A, E> => {
|
|
52
|
+
const { loaded, query: atom } = useLoadedQuery(query);
|
|
53
|
+
return useLoaderQuery(atom, loaded, queryOptions);
|
|
54
|
+
},
|
|
40
55
|
useRetry: () =>
|
|
41
56
|
useQueryRetry((match) => {
|
|
42
57
|
const { context, params, loaderDeps }: { context: TContext; params: TParams; loaderDeps: TSearch } = match;
|
|
@@ -22,7 +22,7 @@ function debounce<A>(value: A, millis: number, runtime: TimerRuntime) {
|
|
|
22
22
|
|
|
23
23
|
export function useDebouncedValue<A>(value: A, duration: Duration.Input, runtime: TimerRuntime = defaultRuntime): A {
|
|
24
24
|
const registry = useContext(RegistryContext);
|
|
25
|
-
const millis = Duration.toMillis(
|
|
25
|
+
const millis = Duration.toMillis(duration);
|
|
26
26
|
const [held, hold] = useState(() => ({ ...debounce(value, millis, runtime), registry, runtime }));
|
|
27
27
|
let owner = held;
|
|
28
28
|
if (held.registry !== registry || held.runtime !== runtime) {
|
package/src/client/useDelay.ts
CHANGED
|
@@ -8,7 +8,7 @@ type TimerRuntime = Pick<Atom.AtomRuntime<never, unknown>, "fn">;
|
|
|
8
8
|
|
|
9
9
|
export function useDelay(duration: Duration.Input, runtime: TimerRuntime = defaultRuntime) {
|
|
10
10
|
const registry = useContext(RegistryContext);
|
|
11
|
-
const millis = Duration.toMillis(
|
|
11
|
+
const millis = Duration.toMillis(duration);
|
|
12
12
|
const timer = useMemo(
|
|
13
13
|
() => runtime.fn((complete: () => void) => Effect.andThen(Effect.sleep(millis), Effect.sync(complete))).pipe(Atom.setIdleTTL(0)),
|
|
14
14
|
[runtime, millis],
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"queryRoute.d.ts","sourceRoot":"","sources":["../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