@shivaedev/react 0.39.0 → 0.41.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +76 -3
  3. package/dist/client/actionCompletion.d.ts.map +1 -1
  4. package/dist/client/actionCompletion.js +2 -2
  5. package/dist/client/actionCompletion.js.map +1 -1
  6. package/dist/client/adapters/SessionSignOut.d.ts +9 -0
  7. package/dist/client/adapters/SessionSignOut.d.ts.map +1 -0
  8. package/dist/client/adapters/SessionSignOut.js +18 -0
  9. package/dist/client/adapters/SessionSignOut.js.map +1 -0
  10. package/dist/client/adapters/crypto.d.ts +3 -0
  11. package/dist/client/adapters/crypto.d.ts.map +1 -0
  12. package/dist/client/adapters/crypto.js +9 -0
  13. package/dist/client/adapters/crypto.js.map +1 -0
  14. package/dist/client/adapters/handlerRuntime.d.ts +3 -0
  15. package/dist/client/adapters/handlerRuntime.d.ts.map +1 -0
  16. package/dist/client/adapters/handlerRuntime.js +4 -0
  17. package/dist/client/adapters/handlerRuntime.js.map +1 -0
  18. package/dist/client/adapters/useClock.d.ts +5 -0
  19. package/dist/client/adapters/useClock.d.ts.map +1 -0
  20. package/dist/client/adapters/useClock.js +9 -0
  21. package/dist/client/adapters/useClock.js.map +1 -0
  22. package/dist/client/adapters/useNewId.d.ts +5 -0
  23. package/dist/client/adapters/useNewId.d.ts.map +1 -0
  24. package/dist/client/adapters/useNewId.js +9 -0
  25. package/dist/client/adapters/useNewId.js.map +1 -0
  26. package/dist/client/adapters/usePromiseAction.d.ts +7 -0
  27. package/dist/client/adapters/usePromiseAction.d.ts.map +1 -0
  28. package/dist/client/adapters/usePromiseAction.js +16 -0
  29. package/dist/client/adapters/usePromiseAction.js.map +1 -0
  30. package/dist/client/adapters/useSessionRetry.d.ts +3 -0
  31. package/dist/client/adapters/useSessionRetry.d.ts.map +1 -0
  32. package/dist/client/adapters/useSessionRetry.js +48 -0
  33. package/dist/client/adapters/useSessionRetry.js.map +1 -0
  34. package/dist/client/adapters/useSignOut.d.ts +3 -2
  35. package/dist/client/adapters/useSignOut.d.ts.map +1 -1
  36. package/dist/client/adapters/useSignOut.js +6 -1
  37. package/dist/client/adapters/useSignOut.js.map +1 -1
  38. package/dist/client/adapters/useUndoableChange.d.ts +31 -0
  39. package/dist/client/adapters/useUndoableChange.d.ts.map +1 -0
  40. package/dist/client/adapters/useUndoableChange.js +48 -0
  41. package/dist/client/adapters/useUndoableChange.js.map +1 -0
  42. package/dist/client/mountLive.js +1 -1
  43. package/dist/client/mountLive.js.map +1 -1
  44. package/dist/client/resultState.d.ts.map +1 -1
  45. package/dist/client/resultState.js +2 -2
  46. package/dist/client/resultState.js.map +1 -1
  47. package/dist/client/saveState.d.ts.map +1 -1
  48. package/dist/client/saveState.js +2 -2
  49. package/dist/client/saveState.js.map +1 -1
  50. package/dist/client/sessionBoundary.d.ts +6 -3
  51. package/dist/client/sessionBoundary.d.ts.map +1 -1
  52. package/dist/client/sessionBoundary.js +16 -4
  53. package/dist/client/sessionBoundary.js.map +1 -1
  54. package/dist/internal/adapters/useLatestCallback.d.ts +1 -1
  55. package/dist/internal/adapters/useLatestCallback.d.ts.map +1 -1
  56. package/dist/internal/adapters/useLatestCallback.js.map +1 -1
  57. package/dist/internal/sessionRecheck.d.ts +1 -2
  58. package/dist/internal/sessionRecheck.d.ts.map +1 -1
  59. package/dist/internal/sessionRecheck.js +2 -6
  60. package/dist/internal/sessionRecheck.js.map +1 -1
  61. package/package.json +17 -17
  62. package/src/client/actionCompletion.ts +2 -2
  63. package/src/client/adapters/SessionSignOut.ts +27 -0
  64. package/src/client/adapters/crypto.ts +14 -0
  65. package/src/client/adapters/handlerRuntime.ts +4 -0
  66. package/src/client/adapters/useClock.ts +11 -0
  67. package/src/client/adapters/useNewId.ts +11 -0
  68. package/src/client/adapters/usePromiseAction.ts +25 -0
  69. package/src/client/adapters/useSessionRetry.ts +54 -0
  70. package/src/client/adapters/useSignOut.ts +6 -1
  71. package/src/client/adapters/useUndoableChange.ts +82 -0
  72. package/src/client/mountLive.ts +1 -1
  73. package/src/client/resultState.ts +2 -2
  74. package/src/client/saveState.ts +2 -2
  75. package/src/client/sessionBoundary.ts +33 -17
  76. package/src/internal/adapters/useLatestCallback.ts +1 -1
  77. package/src/internal/sessionRecheck.ts +2 -7
@@ -4,13 +4,13 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import type * as Atom from "effect/reactivity/Atom";
5
5
  import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
6
6
  import { useContext, useEffect, useState } from "react";
7
+ import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
7
8
  import type { AtomServices } from "#AtomServices.ts";
8
9
  import { acceptLoaderQuery } from "#adapters/queryHandoff.ts";
9
10
  import { useActionResult } from "#client/adapters/useActionResult.ts";
10
11
  import { useLiveWindow } from "#client/adapters/useLiveWindow.ts";
11
12
  import { usePermissionFailure } from "#internal/adapters/usePermissionFailure.ts";
12
13
  import { useQueryNotice } from "#internal/adapters/useQueryNotice.ts";
13
- import { requestsRecheck } from "#internal/sessionRecheck.ts";
14
14
  import type { NoticeDescriptor } from "#notices.ts";
15
15
  import { type ActionOptions, useActionSource } from "./actionCompletion.ts";
16
16
  import { useSessionRecheck } from "./sessionBoundary.ts";
@@ -34,7 +34,7 @@ function state<A, E>(result: AsyncResult.AsyncResult<A, E>, data = AsyncResult.v
34
34
  function useRecheckOnUnauthorized<A, E>(result: AsyncResult.AsyncResult<A, E>, enabled = true): void {
35
35
  const recheck = useSessionRecheck();
36
36
  useEffect(() => {
37
- if (enabled && AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), requestsRecheck)) {
37
+ if (enabled && AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), isUnauthorized)) {
38
38
  recheck();
39
39
  }
40
40
  }, [enabled, recheck, result]);
@@ -3,8 +3,8 @@ import { Cause, Option } from "effect";
3
3
  import * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import * as Atom from "effect/reactivity/Atom";
5
5
  import { useCallback, useContext, useEffect } from "react";
6
+ import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
6
7
  import { FieldFailure, type Fields, type Form, Invalid } from "@shivaedev/form/shape.ts";
7
- import { requestsRecheck } from "#internal/sessionRecheck.ts";
8
8
  import { useSessionRecheck } from "./sessionBoundary.ts";
9
9
 
10
10
  const idle = Atom.make(AsyncResult.initial());
@@ -26,7 +26,7 @@ export function useSaveState<TFields extends Fields, A, E, TRuntimeError>(
26
26
  const result: AsyncResult.AsyncResult<A, E | TRuntimeError | FieldFailure | Invalid> = useAtomValue(form?.submit ?? idle);
27
27
  const dirty = useAtomValue(form?.dirty ?? clean);
28
28
  useEffect(() => {
29
- if (AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), requestsRecheck)) {
29
+ if (AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), isUnauthorized)) {
30
30
  recheck();
31
31
  }
32
32
  }, [recheck, result]);
@@ -1,10 +1,14 @@
1
1
  import { RegistryContext } from "@effect/atom-react";
2
- import type { Duration } from "effect";
2
+ import type { Duration, Effect } from "effect";
3
+ import type * as AsyncResult from "effect/reactivity/AsyncResult";
4
+ import type * as Atom from "effect/reactivity/Atom";
3
5
  import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
4
6
  import { createElement, Fragment, type ReactNode, useContext } from "react";
5
7
  import { DraftSessionContext } from "@shivaedev/form/adapters/react.ts";
6
8
  import { SessionGeneration } from "#client/adapters/SessionGeneration.ts";
7
9
  import { SessionHandoff } from "#client/adapters/SessionHandoff.ts";
10
+ import { SessionSignOut } from "#client/adapters/SessionSignOut.ts";
11
+ import { useSessionRetry } from "#client/adapters/useSessionRetry.ts";
8
12
  import { RecheckContext } from "#internal/RecheckContext.ts";
9
13
  import { SignOutRegistryContext } from "#internal/SignOutRegistryContext.ts";
10
14
  import { useDraftOwner } from "./useDraftOwner.ts";
@@ -14,10 +18,11 @@ export interface SessionBoundaryProps<TSession, TClient> {
14
18
  readonly identify: (session: TSession) => string;
15
19
  readonly liveCoalesceWindow?: Duration.Input | undefined;
16
20
  readonly navigate?: ((href: string) => void) | undefined;
17
- readonly recheck: () => void;
21
+ readonly recheck: (() => void) | Effect.Effect<unknown, unknown, AtomRegistry.AtomRegistry>;
18
22
  readonly redirectTo?: string | undefined;
19
23
  readonly session: TSession | undefined;
20
24
  readonly signedOut?: ReactNode;
25
+ readonly signOut?: Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>> | undefined;
21
26
  }
22
27
  export function useSessionRecheck(): () => void {
23
28
  return useContext(RecheckContext);
@@ -29,6 +34,7 @@ export function SessionBoundary<TSession, TClient>({
29
34
  connect,
30
35
  recheck,
31
36
  signedOut,
37
+ signOut,
32
38
  redirectTo,
33
39
  navigate,
34
40
  children,
@@ -36,19 +42,29 @@ export function SessionBoundary<TSession, TClient>({
36
42
  const registry = useContext(RegistryContext);
37
43
  const id = session === undefined ? undefined : identify(session);
38
44
  const current = useDraftOwner(id);
39
- return session === undefined
40
- ? createElement(Fragment, null, signedOut)
41
- : createElement(
42
- SignOutRegistryContext.Provider,
43
- { value: registry },
44
- createElement(
45
- DraftSessionContext.Provider,
46
- { value: current },
47
- createElement(
48
- SessionHandoff,
49
- { key: id, navigate, redirectTo },
50
- createElement(SessionGeneration<TSession, TClient>, { children, connect, identity: id, liveCoalesceWindow, recheck, session }),
51
- ),
52
- ),
53
- );
45
+ const retry = useSessionRetry(recheck, id);
46
+ if (session === undefined) {
47
+ return createElement(Fragment, null, signedOut);
48
+ }
49
+ const generation = createElement(SessionGeneration<TSession, TClient>, {
50
+ children,
51
+ connect,
52
+ identity: id,
53
+ liveCoalesceWindow,
54
+ recheck: retry,
55
+ session,
56
+ });
57
+ return createElement(
58
+ SignOutRegistryContext.Provider,
59
+ { value: registry },
60
+ createElement(
61
+ DraftSessionContext.Provider,
62
+ { value: current },
63
+ createElement(
64
+ SessionHandoff,
65
+ { key: id, navigate, redirectTo },
66
+ signOut === undefined ? generation : createElement(SessionSignOut, { action: signOut, recheck: retry }, generation),
67
+ ),
68
+ ),
69
+ );
54
70
  }
@@ -1,6 +1,6 @@
1
1
  import { useCallback, useLayoutEffect, useRef } from "react";
2
2
 
3
- export function useLatestCallback(callback: () => void): () => void {
3
+ export function useLatestCallback<A>(callback: () => A): () => A {
4
4
  const latestRef = useRef(callback);
5
5
  useLayoutEffect(() => {
6
6
  latestRef.current = callback;
@@ -1,5 +1,4 @@
1
1
  import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
2
- import { RpcClientError } from "effect/rpc/RpcClientError";
3
2
  import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
4
3
 
5
4
  const rechecks = new WeakMap<AtomRegistry.AtomRegistry, () => void>();
@@ -11,12 +10,8 @@ export function registerSessionRecheck(registry: AtomRegistry.AtomRegistry, rech
11
10
  };
12
11
  }
13
12
 
14
- export function requestsRecheck(error: unknown, connection = false): boolean {
15
- return isUnauthorized(error) || (connection && error instanceof RpcClientError);
16
- }
17
-
18
- export async function recheckUnauthorized(registry: AtomRegistry.AtomRegistry, error: unknown, connection = false): Promise<void> {
19
- if (requestsRecheck(error, connection)) {
13
+ export async function recheckUnauthorized(registry: AtomRegistry.AtomRegistry, error: unknown): Promise<void> {
14
+ if (isUnauthorized(error)) {
20
15
  await rechecks.get(registry)?.();
21
16
  }
22
17
  }