@quitecustard/react 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +58 -2
- package/dist/hooks/use-action-runner.d.ts +17 -0
- package/dist/hooks/use-action-runner.js +41 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QuiteCustard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @quitecustard/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React hooks for client components. One module per file — no barrels:
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { useActionRunner } from '@quitecustard/react/hooks/use-action-runner'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pnpm add @quitecustard/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Every module is a subpath export, so TypeScript needs `moduleResolution` set to `bundler`,
|
|
16
|
+
`node16` or `nodenext` — the legacy `node` resolution can't see subpath exports at all.
|
|
17
|
+
|
|
18
|
+
Peer dependencies: `react` 19 or later. `next` is optional and needed only by the modules that say
|
|
19
|
+
so below.
|
|
20
|
+
|
|
21
|
+
## Contents
|
|
22
|
+
|
|
23
|
+
### `hooks/`
|
|
24
|
+
|
|
25
|
+
- `use-action-runner` — `useActionRunner()` runs a Next.js server action from a client component.
|
|
26
|
+
It returns `{ run, isPending, error, setError }`: `run(action, onSuccess?)` clears the last error,
|
|
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. Needs `next`.
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
'use client'
|
|
32
|
+
import { useActionRunner } from '@quitecustard/react/hooks/use-action-runner'
|
|
33
|
+
import { renameThing } from './actions'
|
|
34
|
+
|
|
35
|
+
export const RenameForm = ({ id }: { id: number }) => {
|
|
36
|
+
const { run, isPending, error } = useActionRunner()
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<form
|
|
40
|
+
onSubmit={(event) => {
|
|
41
|
+
event.preventDefault()
|
|
42
|
+
run(() => renameThing(id, new FormData(event.currentTarget)))
|
|
43
|
+
}}
|
|
44
|
+
>
|
|
45
|
+
<label>
|
|
46
|
+
Name
|
|
47
|
+
<input name="name" disabled={isPending} />
|
|
48
|
+
</label>
|
|
49
|
+
<button type="submit" disabled={isPending}>
|
|
50
|
+
Save
|
|
51
|
+
</button>
|
|
52
|
+
{error && <p>{error}</p>}
|
|
53
|
+
</form>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The action signals a refusal by returning an object with a string `error`; anything else counts as
|
|
59
|
+
success.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a server action from a client component: clears the last error, runs the action inside a
|
|
3
|
+
* transition (so `isPending` can disable the caller's controls), sets `error` when the action
|
|
4
|
+
* answers with a string `error`, and otherwise refreshes the route.
|
|
5
|
+
*
|
|
6
|
+
* `onSuccess` receives the action's own result and runs on the non-error path only, BEFORE the
|
|
7
|
+
* refresh - for local state the refresh would otherwise race (a cleared search box, a closed
|
|
8
|
+
* editor).
|
|
9
|
+
*
|
|
10
|
+
* Needs the `next` peer dependency (it reads the App Router's `useRouter`).
|
|
11
|
+
*/
|
|
12
|
+
export declare const useActionRunner: () => {
|
|
13
|
+
run: <T>(action: () => Promise<T>, onSuccess?: (result: T) => void) => void;
|
|
14
|
+
isPending: boolean;
|
|
15
|
+
error: string | null;
|
|
16
|
+
setError: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState, useTransition } from 'react';
|
|
3
|
+
import { useRouter } from 'next/navigation';
|
|
4
|
+
// Narrowed from `unknown` rather than constrained on the generic: actions return unions like
|
|
5
|
+
// `{ error } | { id }`, and a result arm carrying no `error` key at all still has to pass through.
|
|
6
|
+
const errorOf = (result) => {
|
|
7
|
+
if (typeof result !== 'object' || result === null || !('error' in result))
|
|
8
|
+
return null;
|
|
9
|
+
const { error } = result;
|
|
10
|
+
return typeof error === 'string' ? error : null;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Runs a server action from a client component: clears the last error, runs the action inside a
|
|
14
|
+
* transition (so `isPending` can disable the caller's controls), sets `error` when the action
|
|
15
|
+
* answers with a string `error`, and otherwise refreshes the route.
|
|
16
|
+
*
|
|
17
|
+
* `onSuccess` receives the action's own result and runs on the non-error path only, BEFORE the
|
|
18
|
+
* refresh - for local state the refresh would otherwise race (a cleared search box, a closed
|
|
19
|
+
* editor).
|
|
20
|
+
*
|
|
21
|
+
* Needs the `next` peer dependency (it reads the App Router's `useRouter`).
|
|
22
|
+
*/
|
|
23
|
+
export const useActionRunner = () => {
|
|
24
|
+
const router = useRouter();
|
|
25
|
+
const [isPending, startTransition] = useTransition();
|
|
26
|
+
const [error, setError] = useState(null);
|
|
27
|
+
const run = (action, onSuccess) => {
|
|
28
|
+
setError(null);
|
|
29
|
+
startTransition(async () => {
|
|
30
|
+
const result = await action();
|
|
31
|
+
const refused = errorOf(result);
|
|
32
|
+
if (refused != null) {
|
|
33
|
+
setError(refused);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
onSuccess?.(result);
|
|
37
|
+
router.refresh();
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
return { run, isPending, error, setError };
|
|
41
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quitecustard/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks for Next.js apps: run a server action with pending state, a surfaced refusal and a route refresh",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"hooks",
|
|
8
|
+
"next",
|
|
9
|
+
"server-actions",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "QuiteCustard",
|
|
14
|
+
"homepage": "https://github.com/QuiteCustard/custard-kit/tree/HEAD/packages/react#readme",
|
|
15
|
+
"bugs": "https://github.com/QuiteCustard/custard-kit/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/QuiteCustard/custard-kit.git",
|
|
19
|
+
"directory": "packages/react"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"exports": {
|
|
33
|
+
"./*": {
|
|
34
|
+
"types": "./dist/*.d.ts",
|
|
35
|
+
"import": "./dist/*.js",
|
|
36
|
+
"default": "./dist/*.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"next": ">=15",
|
|
41
|
+
"react": ">=19"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"next": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@testing-library/react": "^16.3.0",
|
|
50
|
+
"@types/node": "22.19.9",
|
|
51
|
+
"@types/react": "^19",
|
|
52
|
+
"@types/react-dom": "^19",
|
|
53
|
+
"jsdom": "^26.1.0",
|
|
54
|
+
"next": "^16.4.1",
|
|
55
|
+
"react": "^19.2.4",
|
|
56
|
+
"react-dom": "^19.2.4"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsc -p tsconfig.build.json",
|
|
60
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
61
|
+
}
|
|
6
62
|
}
|