@shivaedev/react 0.49.1 → 0.50.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 (128) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +84 -116
  3. package/dist/client/FailureHostContext.d.ts +3 -0
  4. package/dist/client/FailureHostContext.d.ts.map +1 -0
  5. package/dist/client/FailureHostContext.js +3 -0
  6. package/dist/client/FailureHostContext.js.map +1 -0
  7. package/dist/client/NoticeContext.d.ts +1 -1
  8. package/dist/client/QueryView.d.ts +20 -5
  9. package/dist/client/QueryView.d.ts.map +1 -1
  10. package/dist/client/QueryView.js +23 -3
  11. package/dist/client/QueryView.js.map +1 -1
  12. package/dist/client/actionCompletion.d.ts +6 -5
  13. package/dist/client/actionCompletion.d.ts.map +1 -1
  14. package/dist/client/actionCompletion.js +19 -18
  15. package/dist/client/actionCompletion.js.map +1 -1
  16. package/dist/client/adapters/formFields.d.ts +4 -0
  17. package/dist/client/adapters/formFields.d.ts.map +1 -0
  18. package/dist/client/adapters/formFields.js +30 -0
  19. package/dist/client/adapters/formFields.js.map +1 -0
  20. package/dist/client/adapters/operationHooks.d.ts +5 -0
  21. package/dist/client/adapters/operationHooks.d.ts.map +1 -0
  22. package/dist/client/adapters/operationHooks.js +7 -0
  23. package/dist/client/adapters/operationHooks.js.map +1 -0
  24. package/dist/client/adapters/rowValues.d.ts +7 -0
  25. package/dist/client/adapters/rowValues.d.ts.map +1 -0
  26. package/dist/client/adapters/rowValues.js +17 -0
  27. package/dist/client/adapters/rowValues.js.map +1 -0
  28. package/dist/client/adapters/useFeedback.d.ts +11 -0
  29. package/dist/client/adapters/useFeedback.d.ts.map +1 -0
  30. package/dist/client/adapters/useFeedback.js +38 -0
  31. package/dist/client/adapters/useFeedback.js.map +1 -0
  32. package/dist/client/adapters/useQueryNotice.js +1 -1
  33. package/dist/client/adapters/useQueryNotice.js.map +1 -1
  34. package/dist/client/adapters/useStableRun.js +1 -1
  35. package/dist/client/adapters/useStableRun.js.map +1 -1
  36. package/dist/client/adapters/useSubmissionCallbacks.d.ts +4 -5
  37. package/dist/client/adapters/useSubmissionCallbacks.d.ts.map +1 -1
  38. package/dist/client/adapters/useSubmissionCallbacks.js +37 -17
  39. package/dist/client/adapters/useSubmissionCallbacks.js.map +1 -1
  40. package/dist/client/adapters/useUndoableChange.d.ts +13 -12
  41. package/dist/client/adapters/useUndoableChange.d.ts.map +1 -1
  42. package/dist/client/adapters/useUndoableChange.js +16 -4
  43. package/dist/client/adapters/useUndoableChange.js.map +1 -1
  44. package/dist/client/boundOperation.d.ts +12 -0
  45. package/dist/client/boundOperation.d.ts.map +1 -0
  46. package/dist/client/boundOperation.js +2 -0
  47. package/dist/client/boundOperation.js.map +1 -0
  48. package/dist/client/create.d.ts +15 -11
  49. package/dist/client/create.d.ts.map +1 -1
  50. package/dist/client/create.js +12 -5
  51. package/dist/client/create.js.map +1 -1
  52. package/dist/client/editor.d.ts +25 -18
  53. package/dist/client/editor.d.ts.map +1 -1
  54. package/dist/client/editor.js +14 -8
  55. package/dist/client/editor.js.map +1 -1
  56. package/dist/client/failureDecision.d.ts +16 -0
  57. package/dist/client/failureDecision.d.ts.map +1 -0
  58. package/dist/client/failureDecision.js +49 -0
  59. package/dist/client/failureDecision.js.map +1 -0
  60. package/dist/client/failures.d.ts +45 -0
  61. package/dist/client/failures.d.ts.map +1 -0
  62. package/dist/client/failures.js +2 -0
  63. package/dist/client/failures.js.map +1 -0
  64. package/dist/client/notices.d.ts +9 -4
  65. package/dist/client/notices.d.ts.map +1 -1
  66. package/dist/client/notices.js +22 -3
  67. package/dist/client/notices.js.map +1 -1
  68. package/dist/client/operationForm.d.ts +40 -0
  69. package/dist/client/operationForm.d.ts.map +1 -0
  70. package/dist/client/operationForm.js +2 -0
  71. package/dist/client/operationForm.js.map +1 -0
  72. package/dist/client/pendingCommand.d.ts +6 -9
  73. package/dist/client/pendingCommand.d.ts.map +1 -1
  74. package/dist/client/pendingCommand.js +3 -3
  75. package/dist/client/pendingCommand.js.map +1 -1
  76. package/dist/client/resultState.d.ts +7 -4
  77. package/dist/client/resultState.d.ts.map +1 -1
  78. package/dist/client/resultState.js +6 -3
  79. package/dist/client/resultState.js.map +1 -1
  80. package/dist/client/unexpectedFailure.d.ts +8 -0
  81. package/dist/client/unexpectedFailure.d.ts.map +1 -0
  82. package/dist/client/unexpectedFailure.js +17 -0
  83. package/dist/client/unexpectedFailure.js.map +1 -0
  84. package/dist/noticeEntry.d.ts +3 -1
  85. package/dist/noticeEntry.d.ts.map +1 -1
  86. package/dist/noticeEntry.js +2 -1
  87. package/dist/noticeEntry.js.map +1 -1
  88. package/dist/noticeStore.d.ts +3 -3
  89. package/dist/noticeStore.d.ts.map +1 -1
  90. package/dist/noticeStore.js +4 -4
  91. package/dist/noticeStore.js.map +1 -1
  92. package/dist/noticeTypes.d.ts +1 -2
  93. package/dist/noticeTypes.d.ts.map +1 -1
  94. package/dist/submission.d.ts +2 -4
  95. package/dist/submission.d.ts.map +1 -1
  96. package/dist/submission.js +1 -3
  97. package/dist/submission.js.map +1 -1
  98. package/package.json +22 -22
  99. package/src/client/FailureHostContext.ts +4 -0
  100. package/src/client/QueryView.ts +50 -7
  101. package/src/client/actionCompletion.ts +33 -23
  102. package/src/client/adapters/formFields.ts +33 -0
  103. package/src/client/adapters/operationHooks.ts +14 -0
  104. package/src/client/adapters/rowValues.ts +25 -0
  105. package/src/client/adapters/useFeedback.ts +50 -0
  106. package/src/client/adapters/useQueryNotice.ts +1 -1
  107. package/src/client/adapters/useStableRun.ts +1 -1
  108. package/src/client/adapters/useSubmissionCallbacks.ts +44 -21
  109. package/src/client/adapters/useUndoableChange.ts +66 -30
  110. package/src/client/boundOperation.ts +13 -0
  111. package/src/client/create.ts +50 -20
  112. package/src/client/editor.ts +98 -38
  113. package/src/client/failureDecision.ts +70 -0
  114. package/src/client/failures.ts +56 -0
  115. package/src/client/notices.ts +44 -6
  116. package/src/client/operationForm.ts +52 -0
  117. package/src/client/pendingCommand.ts +12 -14
  118. package/src/client/resultState.ts +20 -11
  119. package/src/client/unexpectedFailure.ts +24 -0
  120. package/src/noticeEntry.ts +5 -0
  121. package/src/noticeStore.ts +9 -4
  122. package/src/noticeTypes.ts +1 -2
  123. package/src/submission.ts +4 -6
  124. package/dist/client/adapters/useNoticePublisher.d.ts +0 -4
  125. package/dist/client/adapters/useNoticePublisher.d.ts.map +0 -1
  126. package/dist/client/adapters/useNoticePublisher.js +0 -8
  127. package/dist/client/adapters/useNoticePublisher.js.map +0 -1
  128. package/src/client/adapters/useNoticePublisher.ts +0 -10
@@ -1,15 +1,16 @@
1
1
  import { RegistryContext, useAtomRefresh, useAtomSet, useAtomValue } from "@effect/atom-react";
2
- import { Cause, type Effect, Option, Predicate } from "effect";
2
+ import { Cause, Option, Predicate } from "effect";
3
3
  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
7
  import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
8
- import type { AtomServices } from "#AtomServices.ts";
9
- import { type ActionOptions, useActionSource } from "#client/actionCompletion.ts";
8
+ import { type ActionOptions, type OperationActionOptions, useActionSource } from "#client/actionCompletion.ts";
10
9
  import { useActionResult } from "#client/adapters/useActionResult.ts";
11
10
  import { usePermissionFailure } from "#client/adapters/usePermissionFailure.ts";
12
11
  import { useQueryNotice } from "#client/adapters/useQueryNotice.ts";
12
+ import type { BoundOperation } from "#client/boundOperation.ts";
13
+ import type { FailureChoices, FailuresChecked } from "#client/failures.ts";
13
14
  import type { NoticeDescriptor } from "#client/notices.ts";
14
15
  import { useSessionRecheck } from "#client/sessionBoundary.ts";
15
16
 
@@ -89,18 +90,26 @@ export interface ActionState<TInput, A, E> extends ResultState<A, E> {
89
90
  readonly dispatch: (input: TInput) => void;
90
91
  }
91
92
  export function useAction<TInput, A, E>(atom: Atom.AtomResultFn<TInput, A, E>): ActionState<TInput, A, E>;
93
+ export function useAction<TInput, A, E, R, TRuntimeError, TSilent extends boolean = false>(
94
+ operation: BoundOperation<TInput, A, E, R, TRuntimeError>,
95
+ options: OperationActionOptions<TInput, A, E, R, TSilent>,
96
+ ): ActionState<TInput, A, E | TRuntimeError>;
97
+ // Declared last so that a call without options reports this overload's error, which names each rejection that needs `failures`.
92
98
  export function useAction<TInput, A, E, R, TRuntimeError>(
93
- runtime: Atom.AtomRuntime<R, TRuntimeError>,
94
- run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>,
95
- options?: ActionOptions<NoInfer<TInput>, NoInfer<A>, NoInfer<E>, R>,
99
+ operation: BoundOperation<TInput, A, E, R, TRuntimeError> & FailuresChecked<E>,
96
100
  ): ActionState<TInput, A, E | TRuntimeError>;
97
101
  export function useAction<TInput, A, E, R, TRuntimeError>(
98
- source: Atom.AtomResultFn<TInput, A, E> | Atom.AtomRuntime<R, TRuntimeError>,
99
- run?: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>,
100
- options: ActionOptions<TInput, A, E, R> = {},
102
+ source: Atom.AtomResultFn<TInput, A, E> | BoundOperation<TInput, A, E, R, TRuntimeError>,
103
+ options: ActionOptions<TInput, A, E, R> & FailureChoices = {},
104
+ ): ActionState<TInput, A, E | TRuntimeError> {
105
+ return useActionState(source, options);
106
+ }
107
+ export function useActionState<TInput, A, E, R, TRuntimeError>(
108
+ source: Atom.AtomResultFn<TInput, A, E> | BoundOperation<TInput, A, E, R, TRuntimeError>,
109
+ options: ActionOptions<TInput, A, E, R> & FailureChoices = {},
101
110
  ): ActionState<TInput, A, E | TRuntimeError> {
102
- const atom = useActionSource(source, run, options);
111
+ const atom = useActionSource(source, options);
103
112
  const result = useActionResult(atom);
104
- useRecheckOnUnauthorized(result, run === undefined);
113
+ useRecheckOnUnauthorized(result, !("runtime" in source));
105
114
  return { ...state(result), dispatch: useAtomSet(atom) };
106
115
  }
@@ -0,0 +1,24 @@
1
+ import { Cause, Option, Predicate } from "effect";
2
+ import { isAuthUnavailable, isInternalServerError, isServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
3
+
4
+ // `cause` is for the application's diagnostics; only `message` is meant for the person.
5
+ export interface UnexpectedFailure {
6
+ readonly cause: Cause.Cause<unknown>;
7
+ readonly kind: "connection" | "unavailable" | "unexpected";
8
+ readonly message: string;
9
+ }
10
+
11
+ export function unexpectedFailure(cause: Cause.Cause<unknown>): UnexpectedFailure {
12
+ const error = Option.getOrUndefined(Cause.findErrorOption(cause));
13
+ if (Predicate.isTagged(error, "RpcClientError") || Predicate.isTagged(error, "WebSocketRequired")) {
14
+ return { cause, kind: "connection", message: "Could not reach the server. Check your connection and try again." };
15
+ }
16
+ // These messages come from the server's handler edge, which has already redacted them.
17
+ if (isServiceUnavailable(error) || isAuthUnavailable(error)) {
18
+ return { cause, kind: "unavailable", message: error.message };
19
+ }
20
+ if (isInternalServerError(error)) {
21
+ return { cause, kind: "unexpected", message: error.message };
22
+ }
23
+ return { cause, kind: "unexpected", message: "Something went wrong. Try again." };
24
+ }
@@ -9,10 +9,13 @@ export interface NoticeAction<TMessage = unknown> {
9
9
  readonly label: TMessage;
10
10
  }
11
11
  export type NoticeClock = Pick<Atom.AtomRuntime<never, unknown>, "atom">;
12
+ // A success reports what a command did, a rejection why the server refused it, a failure that the framework could not complete it.
13
+ export type NoticeKind = "failure" | "rejection" | "success";
12
14
  export interface NoticeEntry<TMessage = unknown> {
13
15
  readonly action?: { readonly label: TMessage; readonly run: () => void } | undefined;
14
16
  readonly dismiss: () => void;
15
17
  readonly id: number;
18
+ readonly kind: NoticeKind;
16
19
  readonly message: TMessage;
17
20
  readonly release: () => void;
18
21
  readonly status: Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>;
@@ -25,6 +28,7 @@ export function noticeEntry<TMessage>(
25
28
  descriptor: Pick<NoticeDescriptor<never, TMessage>, "message" | "expiresIn">,
26
29
  remove: (id: number) => void,
27
30
  action: NoticeAction<TMessage> | undefined,
31
+ kind: NoticeKind,
28
32
  ): NoticeEntry<TMessage> {
29
33
  let active = true;
30
34
  function dismiss() {
@@ -63,6 +67,7 @@ export function noticeEntry<TMessage>(
63
67
  action: action === undefined ? undefined : { label: action.label, run },
64
68
  dismiss,
65
69
  id,
70
+ kind,
66
71
  message: descriptor.message,
67
72
  release,
68
73
  status: action?.atom ?? idle,
@@ -1,6 +1,6 @@
1
1
  import * as Atom from "effect/reactivity/Atom";
2
2
  import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
3
- import { type NoticeAction, type NoticeClock, type NoticeEntry, noticeEntry } from "#noticeEntry.ts";
3
+ import { type NoticeAction, type NoticeClock, type NoticeEntry, type NoticeKind, noticeEntry } from "#noticeEntry.ts";
4
4
  import type { NoticeDescriptor } from "#noticeTypes.ts";
5
5
 
6
6
  export function noticeStore<TMessage = unknown>(registry: AtomRegistry.AtomRegistry, clock: NoticeClock) {
@@ -22,11 +22,11 @@ export function noticeStore<TMessage = unknown>(registry: AtomRegistry.AtomRegis
22
22
  registry.set(entries, []);
23
23
  },
24
24
  entries,
25
- publish: (descriptor: Pick<NoticeDescriptor<never, TMessage>, "message" | "expiresIn">, action?: NoticeAction<TMessage>) => {
25
+ publish: (descriptor: Pick<NoticeDescriptor<never, TMessage>, "message" | "expiresIn">, kind: NoticeKind, action?: NoticeAction<TMessage>) => {
26
26
  if (!store.active) {
27
27
  return;
28
28
  }
29
- const entry = noticeEntry(registry, clock, nextId, descriptor, remove, action);
29
+ const entry = noticeEntry(registry, clock, nextId, descriptor, remove, action, kind);
30
30
  nextId += 1;
31
31
  registry.set(entries, [...registry.get(entries), entry]);
32
32
  },
@@ -37,10 +37,15 @@ export function publishNotice<R, E, TMessage = unknown>(
37
37
  store: ReturnType<typeof noticeStore<TMessage>> | undefined,
38
38
  runtime: Atom.AtomRuntime<R, E>,
39
39
  descriptor: NoticeDescriptor<R, TMessage> | undefined,
40
+ kind: NoticeKind,
40
41
  ) {
41
42
  if (!(store?.active && descriptor)) {
42
43
  return;
43
44
  }
44
45
  const action = descriptor.action;
45
- store.publish(descriptor, action === undefined ? undefined : { atom: runtime.fn(() => action.run).pipe(Atom.setIdleTTL(0)), label: action.label });
46
+ store.publish(
47
+ descriptor,
48
+ kind,
49
+ action === undefined ? undefined : { atom: runtime.fn(() => action.run).pipe(Atom.setIdleTTL(0)), label: action.label },
50
+ );
46
51
  }
@@ -12,7 +12,6 @@ export interface NoticeDescriptor<R = never, TMessage = unknown> {
12
12
  readonly message: TMessage;
13
13
  }
14
14
 
15
- export interface NoticeFeedback<TInput, A, E, R = never, TMessage = unknown> {
16
- readonly onFailure?: (error: E, input: TInput) => NoticeDescriptor<R, TMessage> | undefined;
15
+ export interface NoticeFeedback<TInput, A, R = never, TMessage = unknown> {
17
16
  readonly onSuccess?: (value: A, input: TInput) => NoticeDescriptor<R, TMessage> | undefined;
18
17
  }
package/src/submission.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import type { Effect, Schema } from "effect";
2
2
  import type * as Atom from "effect/reactivity/Atom";
3
- import { type RejectField, rejecting } from "@shivaedev/form/fieldRejection.ts";
4
3
  import { form } from "@shivaedev/form/form.ts";
5
4
  import type { PersistedDraftConfig } from "@shivaedev/form/persistence.ts";
6
- import type { Config, Decoded, Encoded, FieldFailure, Fields, Form, Services } from "@shivaedev/form/shape.ts";
5
+ import type { Config, Decoded, Encoded, FieldFailure, Fields, Form, Name, Services, Submitter } from "@shivaedev/form/shape.ts";
7
6
  import type { AtomServices } from "#AtomServices.ts";
8
7
  import { actionTrace } from "#actionTrace.ts";
9
8
 
@@ -12,9 +11,9 @@ export interface SubmissionConfig<TFields extends Fields, A, E, R, TRuntimeError
12
11
  readonly fields: Schema.Struct<TFields>;
13
12
  readonly initialValues: Encoded<TFields>;
14
13
  readonly persistedDraft?: PersistedDraftConfig | undefined;
15
- readonly rejectField: RejectField<TFields, E> | undefined;
16
14
  readonly runtime: Atom.AtomRuntime<Services<TFields, R>, TRuntimeError>;
17
- readonly save: (values: Decoded<TFields>) => Effect.Effect<A, E, AtomServices<R>>;
15
+ // The submitter lets a save place a rejection on one of the form's fields.
16
+ readonly save: (values: Decoded<TFields>, submitter: Submitter<Name<TFields>>) => Effect.Effect<A, E | FieldFailure, AtomServices<R>>;
18
17
  readonly savedValues?: (value: A) => Encoded<TFields>;
19
18
  }
20
19
 
@@ -26,8 +25,7 @@ export interface Submission<TFields extends Fields, A, E, TRuntimeError> {
26
25
  export function submission<TFields extends Fields, A, E, R, TRuntimeError>(
27
26
  config: SubmissionConfig<TFields, A, E, R, TRuntimeError>,
28
27
  ): Submission<TFields, A, E, TRuntimeError> {
29
- const { fields, initialValues, runtime } = config;
30
- const save = rejecting(fields, config.save, config.rejectField);
28
+ const { fields, initialValues, runtime, save } = config;
31
29
  let submitted: Encoded<TFields> | undefined;
32
30
  const currentForm: Form<TFields, A, E | FieldFailure, TRuntimeError> = form(fields, {
33
31
  ...(config.arrayKeys === undefined ? {} : { arrayKeys: config.arrayKeys }),
@@ -1,4 +0,0 @@
1
- import type * as Atom from "effect/reactivity/Atom";
2
- import type { NoticeDescriptor } from "#client/notices.ts";
3
- export declare function useNoticePublisher<R, E>(runtime: Atom.AtomRuntime<R, E>): (descriptor: NoticeDescriptor<R> | undefined) => void;
4
- //# sourceMappingURL=useNoticePublisher.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useNoticePublisher.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useNoticePublisher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAGpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,KAAK,IAAI,CAG/H"}
@@ -1,8 +0,0 @@
1
- import { useContext, useMemo } from "react";
2
- import { NoticeContext } from "#client/NoticeContext.ts";
3
- import { publishNotice } from "#noticeStore.ts";
4
- export function useNoticePublisher(runtime) {
5
- const store = useContext(NoticeContext);
6
- return useMemo(() => (descriptor) => publishNotice(store, runtime, descriptor), [store, runtime]);
7
- }
8
- //# sourceMappingURL=useNoticePublisher.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useNoticePublisher.js","sourceRoot":"","sources":["../../../src/client/adapters/useNoticePublisher.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,MAAM,UAAU,kBAAkB,CAAO,OAA+B;IACvE,MAAM,KAAK,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC;IACxC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,UAA2C,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;AACpI,CAAC"}
@@ -1,10 +0,0 @@
1
- import type * as Atom from "effect/reactivity/Atom";
2
- import { useContext, useMemo } from "react";
3
- import { NoticeContext } from "#client/NoticeContext.ts";
4
- import type { NoticeDescriptor } from "#client/notices.ts";
5
- import { publishNotice } from "#noticeStore.ts";
6
-
7
- export function useNoticePublisher<R, E>(runtime: Atom.AtomRuntime<R, E>): (descriptor: NoticeDescriptor<R> | undefined) => void {
8
- const store = useContext(NoticeContext);
9
- return useMemo(() => (descriptor: NoticeDescriptor<R> | undefined) => publishNotice(store, runtime, descriptor), [store, runtime]);
10
- }