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
- Distinctive: line 20
Only proceed once all four are clear.
- Very specific: line 26
All persistent settings live under the ketcher-opts key in localStorage, managed through a centralized SettingsService in ketcher-core.
- Very specific: line 43
Add a readonly field at the bottom of the interface, grouped by domain:
- Very specific: line 54
Use the narrowest type possible (e.g.
- Distinctive: line 65
Add inside the matching domain comment block (or create a new one):
- Distinctive: line 75
Add next to the default value block:
- Very specific: line 135
Call updateSettings(...) without awaiting unless you need the result.
- Very specific: line 136
Do not read from or write to localStorage directly.
- Very specific: line 138
If you need a local UI state that mirrors the setting, initialize it to [] / null / '' and populate it on first meaningful interaction …
≈ “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.
Files
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 need | Example |
| 1 | Setting name (camelCase) | colorPickerCustomColors |
| 2 | TypeScript type | readonly string[], boolean, 'px' | 'pt' |
| 3 | Default value | [], false, 'px' |
| 4 | Target 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:
| # | File | What to add |
| 1 | packages/ketcher-core/src/application/settings/types.ts | Field declaration in Settings interface |
| 2 | packages/ketcher-core/src/application/settings/schema.ts | Default value in DEFAULT_SETTINGS + JSON Schema entry |
| 3 | React component (or hook) | Read via settings?.fieldName; write via updateSettings(...) |
| 4 | Component test file | Mock 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 readonlyRules
- Mark every field
readonly(settings objects are frozen by the service). - Use the narrowest type possible (e.g.
'px' | 'pt'instead ofstring). - Arrays must be
readonly T[], notT[].
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 type | JSON 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
localStoragedirectly. Always go throughupdateSettings/settings. settingsisnullon 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 fromsettingsat 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-openChecklist
- Field added to
Settingsinterface (types.ts) asreadonly - 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
localStorageaccess in the component - Test mocks
useSettingswithjest.mock - Test verifies initial render from
mockSettings - Test asserts
mockUpdateSettingscall arguments - TypeScript: no new compile errors in
types.tsorschema.ts
Key Files Reference
| Purpose | Path |
| Settings interface | packages/ketcher-core/src/application/settings/types.ts |
| Defaults + schema | packages/ketcher-core/src/application/settings/schema.ts |
| Service implementation | packages/ketcher-core/src/application/settings/SettingsService.ts |
| LocalStorage adapter | packages/ketcher-core/src/application/settings/LocalStorageAdapter.ts |
| React hook | packages/ketcher-react/src/hooks/useSettings.ts |
| Hooks barrel export | packages/ketcher-react/src/hooks/index.ts |
In this repository24 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- codebase-designSkill
- code-reviewSkill
- commitSkill
- create-releaseSkill
- create-verification-skillSkill
- diagnosing-bugsSkill
- generate-autotestsSkill
- ketcher-flowSkill
- ketcher-prSkill
- maintain-verification-skillSkill
- openspec-apply-changeSkill
- openspec-archive-changeSkill
- openspec-exploreSkill
- openspec-proposeSkill
- openspec-sync-specsSkill
- openspec-update-changeSkill
- prepare-releaseSkill
- review-prSkill
- split-componentSkill
- tddSkill
- verify-ketcherSkill
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
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.

