A few small buttons that each post one AI job to the app's route, and what came of it.
<div data-mount="live"></div>
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/ai-job-buttons.css";
import { otherEngine, retryLabel, canRetry } from "capsomer/behavior/ai-job-buttons";
import "capsomer/tokens.css";
import "capsomer/ai-job-buttons.css";
import { AiJobButtons } from "capsomer/react/ai-job-buttons";
Usage
import { AiJobButtons } from "capsomer/react/ai-job-buttons";
const fetcher = useFetcher<AiJobAnswer>();
<AiJobButtons
action="/ai-write"
buttons={[{ job: "continue", label: "Continue my draft" }, { job: "edit", label: "Suggest edits" }]}
fields={{ path }}
Form={fetcher.Form}
answer={fetcher.data}
pending={fetcher.state !== "idle"}
/>
Examples
Done on the subscription, with the draft to open and the API one button away
<div data-mount="done"></div>
Refused on the API: the error, and the subscription to try
<div data-mount="error"></div>
A job in flight
<div data-mount="pending"></div>
API reference
| Prop | Type | Required | Does |
|---|
action | string | Yes | Where the jobs post: the route that runs them on the engine the settings name. |
|---|
buttons | readonly AiJobButton[] | Yes | |
|---|
fields | Record<string, string> | | Hidden fields the route needs, such as a file's path. |
|---|
Form | FormComponent | | The form to post with: a fetcher's Form (useFetcher().Form), so the page stays where it is. A plain <form> without it. |
|---|
answer | AiJobAnswer | null | | The route's answer to the last post (fetcher.data, or the action's data with no script). |
|---|
pending | boolean | | A post in flight (fetcher.state !== "idle"). |
|---|
engineNames | Record<JobEngine, string> | | The engines' names, for the retry button: "Run it on the subscription". |
|---|
openLabel | string | | The words of the link to the result. |
|---|
Functions
| Function | Signature | Does |
|---|
otherEngine | (engine: JobEngine | null | undefined): JobEngine | The engine a retry runs on: the other one. A run with no engine (refused before it started) retries on the API, which answers at once. |
|---|
retryLabel | (engine: JobEngine | null | undefined, names?: Record<JobEngine, string>): string | "Run it on the subscription": offered after a run that said something, so a refused or slow run can go the other way without a trip to the settings. |
|---|
canRetry | (answer: AiJobAnswer | null | undefined): boolean | |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-ai-jobs | the root |
|---|
.cap-ai-jobs-row | a part |
|---|
Accessibility
| Key | Does |
|---|
| Tab | Each job, then the retry, then the link to the result |
| Enter or Space on a job | Posts it |
- The job buttons keep their names while in flight (the spinner covers the label).
- The answer is an alert, so it is announced when it arrives.
- The two forms are named ("AI jobs", "Run it again").