Files
KomediruzeckiandGitHub ceb43bb3a8 feat(forms): profiles, Settings and import say what is wrong at the field (slice 4b of 6) (#611)
Profiles, Settings and the Import page move onto the form kit from #602: a refused entry is said
under the field that is wrong, in the same words in cloud and local-first. Each runtime now runs one
set of rules from shared/ for a profile, a setting, a connected source, a learned category mapping,
an uploaded file and an export.

- A profile name that is blank, over 100 characters, or another profile's in other capitals is
  refused at name. Any profile but the last can be deleted, and the Danger Zone lists the page's
  profiles under their current names.
- PUT /api/settings refuses a key another route owns. The base currency select is labelled and
  says why at the select.
- Both runtimes read an uploaded file with shared/importUpload.ts: an upload is checked to be a
  complete file before it is read, every cell is text, and a file with no header row is refused at
  file. Local-first reads the file off the page's thread, with a time limit, and imports an
  uploaded file again. The email importer, /api/v1/import and MCP's upload use the same reader.
- A backup carries each profile's connected sources and only its own settings. A restore files
  each retirement plan and badge record under its profile, from either runtime's file, and
  restores profile names that differ only in case, the later one numbered.
- Clearing a profile deletes its connected sources on the Worker, and deleting an account runs the
  same deletes.
- Applying category mappings updates the linked goals. The Worker's full backup is indented when
  Settings asks. The Worker's reseed-demo answers 410.
- The contract suite settles 12 pins (43 to 31).

Slice 4b of 6 in docs/plans/2026-10-07-form-errors.md.
2026-10-09 21:18:26 +02:00

35 lines
1.6 KiB
TypeScript

/**
* The settings that belong to one profile: its retirement plan and its badge record.
*
* The Worker's settings table is keyed by (key, profile_id), so there each is a row of its
* profile under the plain key. Local-first keeps one settings row per key for the whole browser,
* so it names the profile in the key: `retirement_settings:<profile id>`. A backup moved from one
* runtime to the other is read through these, so that each setting stays with its profile.
*/
export const PROFILE_SETTING_KEYS = ['retirement_settings', 'achievements'] as const;
export type ProfileSettingKey = (typeof PROFILE_SETTING_KEYS)[number];
/** Whether `key` is one of these in the Worker's form: the plain key. */
export function isProfileSettingKey(key: string): key is ProfileSettingKey {
return (PROFILE_SETTING_KEYS as readonly string[]).includes(key);
}
/** Local-first's key for `key` of profile `profileId`. */
export function localProfileSettingKey(key: ProfileSettingKey, profileId: number): string {
return `${key}:${profileId}`;
}
/** A key in local-first's form split into the plain key and its profile, or null for any other. */
export function splitLocalProfileSettingKey(
key: string
): { key: ProfileSettingKey; profileId: number } | null {
const match = /^(retirement_settings|achievements):(\d+)$/.exec(key);
return match ? { key: match[1] as ProfileSettingKey, profileId: Number(match[2]) } : null;
}
/** Whether `key` is one of these in either form. */
export function belongsToOneProfile(key: string): boolean {
return isProfileSettingKey(key) || splitLocalProfileSettingKey(key) !== null;
}