Skip to content

Compare settings drafts with JSON.stringify - #31

Merged
calvintvu merged 1 commit into
mainfrom
settings-dirty-check-stringify
Aug 5, 2026
Merged

Compare settings drafts with JSON.stringify#31
calvintvu merged 1 commit into
mainfrom
settings-dirty-check-stringify

Conversation

@calvintvu

Copy link
Copy Markdown
Contributor

Context

settingsEqual listed all six fields of CoslashSettings by hand to decide
whether the Save button is enabled.

Both sides of that comparison come from initialSettingsDraft(response), and
every draft update in the dialog is a spread of that same object
({ ...draft, synthesis: { ...draft.synthesis, enabled } }). Spreads preserve
insertion order, and the server marshals the struct in a fixed field order, so
the two sides always share a key order. JSON.stringify compares them
directly.

Changes

  • Delete settingsEqual; compare with JSON.stringify at its one call site.

Stringify also covers the null cases the function's guard handled: two nulls
stringify alike, and a null and an object do not.

This removes a way to get the check wrong. A field added to CoslashSettings
is now compared automatically, where the hand-written list would ignore it
until someone remembered to add a line.

Worth stating the failure direction, since key-order comparison is
order-sensitive: if the orders ever diverged, stringify would report unequal
for identical settings. That shows "Unsaved changes" and permits a redundant
save. It cannot report equal for settings that differ, so the Save button
cannot be wrongly disabled.

Test

  • npx oxlint — passed (two pre-existing Fast Refresh warnings, unchanged)
  • npx prettier --check . — passed
  • npx vitest run — passed (2 files, 7 tests)
  • npm run build — passed

No unit test: the comparison is now an inline expression, and extracting it to
test would restore the function this PR removes. The frontend suite has no
React testing library and adding one for a two-line change did not seem worth
the dependency. Manual check for review: open Settings, toggle a value and back,
confirm the status text returns to "No changes" and Save disables.

settingsEqual listed all six fields by hand to decide whether the Save
button is enabled. Both sides are built by initialSettingsDraft from the
same response.settings, and every draft update is a spread of that
object, so their key order always matches and JSON.stringify compares
them directly.

Stringify also handles the null cases the guard covered: two nulls
stringify alike, a null and an object do not.

This removes a way to get the check wrong. A field added to
CoslashSettings is now compared automatically, where the hand-written
list would have ignored it until someone remembered to add a line.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@calvintvu calvintvu added enhancement New feature or request refactor labels Aug 5, 2026
@calvintvu
calvintvu merged commit 7d5b5b4 into main Aug 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants