Skip to content
English

Commands

Commands are functions in your app that you run from the desktop app: reset a flag, seed test data, force a logout. You fill in their arguments in a form and see what they returned or threw.

Using Commands in the desktop app

Accepts boolean On by default

Call registerCommand() anywhere in your app, before or after init(). The handler may be sync or async.

commands.ts
import { registerCommand } from '@xerlock/client';
export const unregisterOpenCase = registerCommand({
name: 'openCase',
description: 'Open a new case file',
args: {
title: { type: 'string' },
},
handler: ({ title }) => `Case opened: ${title}`,
});

Each entry in args is one argument, shown in the order you declare it. The handler’s argument object is typed from args, with no annotations needed.

commands.ts
import { registerCommand } from '@xerlock/client';
export const unregisterSeed = registerCommand({
name: 'seedSuspects',
description: 'Add test suspects to a case',
args: {
caseId: {
type: 'string',
description: 'The case they belong to',
},
count: {
type: 'number',
default: 5,
},
detained: {
type: 'boolean',
default: false,
},
evidence: {
type: 'json',
optional: true,
},
motive: {
type: 'select',
options: ['greed', 'revenge'],
default: 'greed',
},
},
// caseId: string, count: number, detained: boolean, evidence?: JsonValue, motive: 'greed' | 'revenge'
handler: ({ caseId, count, detained, evidence, motive }) => ({
caseId,
count,
detained,
evidence,
motive,
}),
});
type Shown as The handler receives
'string' A text field string
'number' A text field A finite number
'boolean' A switch boolean
'json' A JSON editor Any JSON value
'select' A dropdown, which you can search when it has 9+ options One of options, as a union
  • Every argument also takes description, optional and default.
  • An argument is required unless it is optional or has a default.
  • The form starts with each default filled in. A select starts on its first option when it has no default.
  • An optional argument left empty is left out of the handler’s object.

The app checks the arguments before the handler runs, and rejects missing, unknown and wrongly typed values.

Whatever the handler returns, or resolves to, is shown as the command’s response. Values with no JSON form are shown as readable previews: a Date as its ISO string, a Map as an object, a Set as an array, a function as ƒ name(). If the handler throws, or its promise rejects, the error’s message is shown instead.

Register a command inside a component to read or change its state from the desktop app. registerCommand() returns a function that removes the command, so return it as the effect’s cleanup:

use-suspects.ts
import { registerCommand } from '@xerlock/client';
import { useEffect, useState } from 'react';
export const useSuspects = () => {
const [suspects, setSuspects] = useState<string[]>([]);
useEffect(() => {
const unregister = registerCommand({
name: 'addSuspect',
description: 'Add a suspect to the case board',
args: {
name: { type: 'string' },
},
handler: ({ name }) => {
const next = [...suspects, name];
setSuspects(next);
return next;
},
});
return unregister;
}, [suspects]);
return suspects;
};

List the state the handler reads in the effect’s dependencies, so the command always sees the latest value. Registering a name again replaces the earlier command, so re-registering on each change, or on a hot reload, never leaves duplicates. The remove function from a replaced registration does nothing, so a stale cleanup can’t remove the newer command.

Pass commands: false to hide the Commands panel for this app. Your registerCommand() calls can stay.

  • The Commands panel is missing: check that commands isn’t false.
  • “No commands on file”: no registerCommand() call has run yet. Check that the file calling it is imported.