@quitecustard/react 0.1.1 → 0.2.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/README.md
CHANGED
|
@@ -25,7 +25,11 @@ so below.
|
|
|
25
25
|
- `use-action-runner` — `useActionRunner()` runs a Next.js server action from a client component.
|
|
26
26
|
It returns `{ run, isPending, error, setError }`: `run(action, onSuccess?)` clears the last error,
|
|
27
27
|
runs the action in a transition, sets `error` when the action answers `{ error: string }`, and
|
|
28
|
-
otherwise calls `onSuccess` with the result and then refreshes the route.
|
|
28
|
+
otherwise calls `onSuccess` with the result and then refreshes the route. An action that throws
|
|
29
|
+
(a dropped connection, a crashed action) sets `error` to `failureMessage`
|
|
30
|
+
(`useActionRunner({ failureMessage })`, default "Something went wrong. Please try again.")
|
|
31
|
+
instead of reaching an error boundary; Next's `redirect()` and `notFound()` still pass through.
|
|
32
|
+
Needs `next`.
|
|
29
33
|
|
|
30
34
|
```tsx
|
|
31
35
|
'use client'
|
|
@@ -1,15 +1,24 @@
|
|
|
1
|
+
/** What `useActionRunner` shows when an action throws instead of answering. */
|
|
2
|
+
export type ActionRunnerOptions = {
|
|
3
|
+
/** Set as `error` when the action throws. Defaults to "Something went wrong. Please try again." */
|
|
4
|
+
failureMessage?: string;
|
|
5
|
+
};
|
|
1
6
|
/**
|
|
2
7
|
* Runs a server action from a client component: clears the last error, runs the action inside a
|
|
3
8
|
* transition (so `isPending` can disable the caller's controls), sets `error` when the action
|
|
4
9
|
* answers with a string `error`, and otherwise refreshes the route.
|
|
5
10
|
*
|
|
11
|
+
* An action that throws instead - a dropped connection, a crashed action - sets `error` to
|
|
12
|
+
* `failureMessage` and logs the error, rather than reaching the nearest error boundary and
|
|
13
|
+
* replacing the page. Next's own `redirect()` and `notFound()` still pass through.
|
|
14
|
+
*
|
|
6
15
|
* `onSuccess` receives the action's own result and runs on the non-error path only, BEFORE the
|
|
7
16
|
* refresh - for local state the refresh would otherwise race (a cleared search box, a closed
|
|
8
17
|
* editor).
|
|
9
18
|
*
|
|
10
19
|
* Needs the `next` peer dependency (it reads the App Router's `useRouter`).
|
|
11
20
|
*/
|
|
12
|
-
export declare const useActionRunner: () => {
|
|
21
|
+
export declare const useActionRunner: ({ failureMessage, }?: ActionRunnerOptions) => {
|
|
13
22
|
run: <T>(action: () => Promise<T>, onSuccess?: (result: T) => void) => void;
|
|
14
23
|
isPending: boolean;
|
|
15
24
|
error: string | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useState, useTransition } from 'react';
|
|
3
|
-
import { useRouter } from 'next/navigation.js';
|
|
3
|
+
import { unstable_rethrow, useRouter } from 'next/navigation.js';
|
|
4
4
|
// Narrowed from `unknown` rather than constrained on the generic: actions return unions like
|
|
5
5
|
// `{ error } | { id }`, and a result arm carrying no `error` key at all still has to pass through.
|
|
6
6
|
const errorOf = (result) => {
|
|
@@ -14,20 +14,33 @@ const errorOf = (result) => {
|
|
|
14
14
|
* transition (so `isPending` can disable the caller's controls), sets `error` when the action
|
|
15
15
|
* answers with a string `error`, and otherwise refreshes the route.
|
|
16
16
|
*
|
|
17
|
+
* An action that throws instead - a dropped connection, a crashed action - sets `error` to
|
|
18
|
+
* `failureMessage` and logs the error, rather than reaching the nearest error boundary and
|
|
19
|
+
* replacing the page. Next's own `redirect()` and `notFound()` still pass through.
|
|
20
|
+
*
|
|
17
21
|
* `onSuccess` receives the action's own result and runs on the non-error path only, BEFORE the
|
|
18
22
|
* refresh - for local state the refresh would otherwise race (a cleared search box, a closed
|
|
19
23
|
* editor).
|
|
20
24
|
*
|
|
21
25
|
* Needs the `next` peer dependency (it reads the App Router's `useRouter`).
|
|
22
26
|
*/
|
|
23
|
-
export const useActionRunner = () => {
|
|
27
|
+
export const useActionRunner = ({ failureMessage = 'Something went wrong. Please try again.', } = {}) => {
|
|
24
28
|
const router = useRouter();
|
|
25
29
|
const [isPending, startTransition] = useTransition();
|
|
26
30
|
const [error, setError] = useState(null);
|
|
27
31
|
const run = (action, onSuccess) => {
|
|
28
32
|
setError(null);
|
|
29
33
|
startTransition(async () => {
|
|
30
|
-
|
|
34
|
+
let result;
|
|
35
|
+
try {
|
|
36
|
+
result = await action();
|
|
37
|
+
}
|
|
38
|
+
catch (thrown) {
|
|
39
|
+
unstable_rethrow(thrown);
|
|
40
|
+
console.error('[action] failed', thrown);
|
|
41
|
+
setError(failureMessage);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
31
44
|
const refused = errorOf(result);
|
|
32
45
|
if (refused != null) {
|
|
33
46
|
setError(refused);
|