agentleFS
epamepam/ketcher/.agents/skills/add-localstorage-setting/SKILL.md

add-localstorage-setting

Skill927 starsChanged 2 days agoApache-2.0View on GitHub

Persist a new setting in LocalStorage via the ketcher-opts mechanism and wire it into a React component in ketcher-react. Use when: a component needs to remember user preferences, UI state, or editor options across page reloads; adding a new field to SettingsService and reading/writing it through the useSettings hook; replacing direct localStorage access with the standard settings store.

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 3 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Only proceed once all four are clear.

    line 20
  2. Very specific:

    All persistent settings live under the ketcher-opts key in localStorage, managed through a centralized SettingsService in ketcher-core.

    line 26
  3. Very specific:

    Add a readonly field at the bottom of the interface, grouped by domain:

    line 43
  4. Very specific:

    Use the narrowest type possible (e.g.

    line 54
  5. Distinctive:

    Add inside the matching domain comment block (or create a new one):

    line 65
  6. Distinctive:

    Add next to the default value block:

    line 75
  7. Very specific:

    Call updateSettings(...) without awaiting unless you need the result.

    line 135
  8. Very specific:

    Do not read from or write to localStorage directly.

    line 136
  9. Very specific:

    If you need a local UI state that mirrors the setting, initialize it to [] / null / '' and populate it on first meaningful interaction …

    line 138

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Add a New LocalStorage Setting

Before You Start

This skill needs four pieces of information. If any are missing from the request, ask the user before writing any code:

#What you needExample
1Setting name (camelCase)colorPickerCustomColors
2TypeScript typereadonly string[], boolean, 'px' | 'pt'
3Default value[], false, 'px'
4Target component path and how it uses the setting (read-only? writes on which event?)src/script/ui/component/form/colorPicker/ColorPicker.tsx — reads on open, writes on Apply/Delete

Only proceed once all four are clear.


Overview

All persistent settings live under the ketcher-opts key in localStorage, managed through a centralized SettingsService in ketcher-core. React components read and write settings via the useSettings hook.

A complete addition touches four locations:

#FileWhat to add
1packages/ketcher-core/src/application/settings/types.tsField declaration in Settings interface
2packages/ketcher-core/src/application/settings/schema.tsDefault value in DEFAULT_SETTINGS + JSON Schema entry
3React component (or hook)Read via settings?.fieldName; write via updateSettings(...)
4Component test fileMock useSettings; assert updateSettings calls

Step 1 — Declare the field in the Settings interface

File: packages/ketcher-core/src/application/settings/types.ts

Add a readonly field at the bottom of the interface, grouped by domain:

// My feature
readonly myNewSetting: string;          // scalar
readonly myNewArraySetting: readonly string[];  // array — always readonly

Rules

  • Mark every field readonly (settings objects are frozen by the service).
  • Use the narrowest type possible (e.g. 'px' | 'pt' instead of string).
  • Arrays must be readonly T[], not T[].

Step 2 — Register the default value and JSON Schema

File: packages/ketcher-core/src/application/settings/schema.ts

2a — Default value in DEFAULT_SETTINGS

Add inside the matching domain comment block (or create a new one):

// My feature
myNewSetting: 'defaultValue',
myNewArraySetting: [],

2b — JSON Schema entry in SCHEMA.properties

Add next to the default value block:

// My feature
myNewSetting: { type: 'string' },
myNewArraySetting: { type: 'array', items: { type: 'string' } },

Common schema patterns:

TypeScript typeJSON Schema
string{ type: 'string' }
number{ type: 'number', minimum: 0, maximum: 100 }
boolean{ type: 'boolean' }
'a' | 'b' | 'c'{ enum: ['a', 'b', 'c'] }
readonly string[]{ type: 'array', items: { type: 'string' } }
Record<string, number>{ type: 'object' }

Step 3 — Read and write in a React component

3a — Import the hook

import { useSettings } from 'src/hooks';

3b — Destructure inside the component

const { settings, updateSettings } = useSettings();

3c — Read the setting

// settings is null until the service initializes — always use optional chaining
const value = settings?.myNewSetting ?? defaultFallback;

// For arrays — spread to get a mutable copy if needed
const items = [...(settings?.myNewArraySetting ?? [])];

3d — Write the setting

updateSettings is async and performs a deep merge — pass only the fields that changed:

// In an event handler or callback
updateSettings({ myNewSetting: newValue });

// For arrays, compute the new array first, then update state and service together
const newItems = computeNewItems(currentItems, input);
setLocalState(newItems);
updateSettings({ myNewArraySetting: newItems });

Important

  • Call updateSettings(...) without awaiting unless you need the result.
  • Do not read from or write to localStorage directly. Always go through updateSettings / settings.
  • settings is null on the first render (async init). Always guard with ?? fallback.
  • If you need a local UI state that mirrors the setting, initialize it to [] / null / '' and populate it on first meaningful interaction (e.g., when a picker opens), reading from settings at that point.

Step 4 — Test the component

4a — Mock useSettings at the top of the test file

useSettings is the storage boundary for this component test; see the mocking rule in testing.md.

import { useSettings } from 'src/hooks';
jest.mock('src/hooks', () => ({
  useSettings: jest.fn(),
}));

4b — Set up mock state in beforeEach

let mockSettings;
let mockUpdateSettings;

beforeEach(() => {
  mockSettings = { myNewSetting: 'default', myNewArraySetting: [] };
  mockUpdateSettings = jest.fn().mockResolvedValue({});
  useSettings.mockReturnValue({
    settings: mockSettings,
    updateSettings: mockUpdateSettings,
  });
});

4c — Test initial state (reading from service)

Set the desired value on mockSettings before rendering:

it('should display value from settings', () => {
  mockSettings.myNewSetting = 'custom';
  renderComponent();
  // assert UI reflects 'custom'
});

4d — Test persistence (writing to service)

Assert that updateSettings was called with the expected partial:

it('should persist new value via SettingsService', async () => {
  renderComponent();
  // perform user interaction that changes the setting
  expect(mockUpdateSettings).toHaveBeenCalledWith(
    expect.objectContaining({ myNewSetting: 'expectedValue' }),
  );
});

4e — Test service update between renders

To simulate the service returning different data (e.g., cross-tab sync), update mockSettings and call useSettings.mockReturnValue again before triggering a re-render:

mockSettings.myNewArraySetting = ['newValue'];
useSettings.mockReturnValue({
  settings: mockSettings,
  updateSettings: mockUpdateSettings,
});
// trigger re-render / re-open

Checklist

  • Field added to Settings interface (types.ts) as readonly
  • Default value added to DEFAULT_SETTINGS (schema.ts)
  • JSON Schema entry added to SCHEMA.properties (schema.ts)
  • Component reads via settings?.fieldName ?? fallback
  • Component writes via updateSettings({ fieldName: newValue })
  • No direct localStorage access in the component
  • Test mocks useSettings with jest.mock
  • Test verifies initial render from mockSettings
  • Test asserts mockUpdateSettings call arguments
  • TypeScript: no new compile errors in types.ts or schema.ts

Key Files Reference

PurposePath
Settings interfacepackages/ketcher-core/src/application/settings/types.ts
Defaults + schemapackages/ketcher-core/src/application/settings/schema.ts
Service implementationpackages/ketcher-core/src/application/settings/SettingsService.ts
LocalStorage adapterpackages/ketcher-core/src/application/settings/LocalStorageAdapter.ts
React hookpackages/ketcher-react/src/hooks/useSettings.ts
Hooks barrel exportpackages/ketcher-react/src/hooks/index.ts

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.