Skip to content

AI job buttons

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";

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

AiJobButtons

PropTypeRequiredDoes
actionstringYesWhere the jobs post: the route that runs them on the engine the settings name.
buttonsreadonly AiJobButton[]Yes
fieldsRecord<string, string>Hidden fields the route needs, such as a file's path.
FormFormComponentThe form to post with: a fetcher's Form (useFetcher().Form), so the page stays where it is. A plain <form> without it.
answerAiJobAnswer | nullThe route's answer to the last post (fetcher.data, or the action's data with no script).
pendingbooleanA post in flight (fetcher.state !== "idle").
engineNamesRecord<JobEngine, string>The engines' names, for the retry button: "Run it on the subscription".
openLabelstringThe words of the link to the result.

Functions

FunctionSignatureDoes
otherEngine(engine: JobEngine | null | undefined): JobEngineThe 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

ClassIs
.cap-ai-jobsthe root
.cap-ai-jobs-rowa part

Accessibility

KeyDoes
TabEach job, then the retry, then the link to the result
Enter or Space on a jobPosts 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").