Skip to content

Conversation

oliveror
Copy link
Member

Merge after #114. Built on the reordering branch (drafts restore rows with the new stable keys); once #114 is merged this PR shows only its own commit.

Changes

  • "Save draft" only showed a message; the draft was lost on navigation or reload. Drafts are now stored in this browser's localStorage (apps/web/src/lib/recipeDrafts.ts), scoped by user and recipe id (new for a new recipe).
  • Opening the form with a saved draft shows Restore draft / Discard draft.
  • Outdated drafts: if the recipe was changed after the draft was saved (updatedAt), the message becomes a warning ("Outdated draft found") explaining that restoring replaces the form with the older draft.
  • Missing images: on restore, the draft's image is probed; if it no longer loads (unused uploads are cleaned up after 24 h), the current image is kept and the user is told.
  • The draft is cleared after a successful save, and all of a user's drafts are cleared on sign-out (shared computers).
  • Damaged or unknown stored drafts are ignored and removed; if storage is unavailable, saving a draft shows a clear error and the form keeps working.
  • README limitation updated.

Validation

  • npm run check passes (219 unit tests), incl. new tests: save → remount (reload) → restore restores title and steps; discard removes it; outdated draft warning; draft cleared after "Save changes"; per-user/per-recipe storage; damaged drafts; sign-out clears only that user's drafts.

AI usage

Written with Claude Code (Anthropic) to address a teammate's review of the final-delivery findings. Please review the diff as you would any other contribution.

Closes #105

Rows could only be reordered by dragging (fails WCAG 2.2 SC 2.5.7) and
used array indexes as React keys, so DOM state could stay behind when
rows moved. Each row now has a stable key, and Move up / Move down
buttons that keep focus on the moved row (or its other button at the
first/last place) and announce the new position in a live region.
Drag-and-drop still works. On phones the row buttons move to their own
line under the fields. The test named 'button-based reordering' (which
actually dragged) is renamed and a keyboard test is added.

Closes #104
"Save draft" only showed a message; the draft was lost on navigation or
reload. Drafts are now stored in localStorage per user and recipe
(lib/recipeDrafts.ts). Opening the form offers to restore or discard a
saved draft, warns when the recipe changed after the draft was saved,
leaves out an uploaded image that no longer loads, and clears the draft
after a successful save. Signing out removes the user's drafts.

Closes #105
@oliveror oliveror requested a review from a team as a code owner November 16, 2026 17:54
The test timed out on the CI runner (5056 ms of 5000 ms) because it typed
every field character by character. It now fills fields with
fireEvent.change and keeps real keyboard interaction for the moves it
tests; it runs in about 0.4 s locally.

Refs #104
Keeps the draft tests well under the CI runner's 5 s test limit, like the
reordering test.

Refs #105
Copy link
Member

@olestan olestan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The draft persistence implementation introduces a few issues that should be addressed before merging:

  • Image restoration race condition (RecipeCreatePage.tsx:215–221): Users can save before the image finishes loading, potentially losing the draft image. A late callback can also overwrite a newly selected image.
  • Incomplete draft validation (recipeDrafts.ts:65–80): Malformed stored fields can pass validation and cause runtime errors when restoring or saving a recipe.
  • Misleading save confirmation (RecipeCreatePage.tsx): Changing tags, images, or adding/removing steps doesn't reset the "Draft saved" status, potentially misleading users into thinking their latest changes are stored.
  • Sign-out cleanup race condition (store.ts:89–97): The user ID is retrieved after the async sign-out request, risking incorrect draft cleanup if authentication state changes.
  • Multi-tab draft loss (RecipeCreatePage.tsx:224–227): Discarding an older draft in one tab can silently delete a newer draft saved in another tab.

I'd recommend fixing the image restoration and validation issues before merging, and addressing the remaining cases to avoid unexpected data loss and misleading feedback.

- Restoring a draft image blocks Save draft and submit until the image
  check finishes; choosing or removing an image cancels the check, so a
  late answer can no longer replace the user's newer choice.
- Drafts are validated field by field when loaded; malformed ones are
  dropped instead of breaking restore or save.
- "Draft saved" is only shown while the form still matches what was saved,
  so changing tags, the image, or adding/removing/moving rows hides it.
- Sign-out reads the user id before awaiting the request.
- Discard and successful save only remove a draft version this tab offered
  or saved; a newer draft from another tab is kept (and the user is told).
@oliveror
Copy link
Member Author

Thanks, all five were real. Fixed in the latest commit:

  • Image restoration race: while a restored draft's image is being checked, Save draft and Publish/Save changes are disabled and the notice says "Checking its image…". Choosing or removing an image cancels the check through a token, so a late onload can no longer replace the user's newer choice.

  • Draft validation: loadDraft() now checks every field the form reads:

    • all text fields are strings
    • visibility is public or private
    • imageUrl is a string or null
    • each ingredient is an object with string name, amount and unit, and an optional string note
    • steps are strings
    • savedAt and baseUpdatedAt are valid

    A draft that fails any check is removed.

  • Misleading "Draft saved": the message is only shown while the form still matches the snapshot taken at save time. Any later change hides it: tags, image, text, or adding, removing or moving rows. It reappears if the form returns to exactly what was saved.

  • Sign-out cleanup: the user id is read before await signOutUser().

  • Multi-tab loss: discard, and the cleanup after a successful save, only remove a draft version this tab offered or saved (matched on savedAt). If another tab saved a newer draft, it is kept, and the notice says so.

Tests: 16 new ones (page, recipeDrafts, store), all of which fail on the previous commit:

  • image check blocks saving
  • a late check after removing the image is ignored
  • "Draft saved" hides after tag, step-added and step-removed changes
  • discard keeps a newer draft from another tab
  • 10 malformed-draft cases
  • version-aware clearDraft
  • sign-out while the current user changes mid-request

npm run check (236 unit tests) passes.

…ent-drafts

# Conflicts:
#	apps/web/src/features/recipes/RecipeCreatePage.css
#	apps/web/src/features/recipes/RecipeCreatePage.test.tsx
#	apps/web/src/features/recipes/RecipeCreatePage.tsx
#	apps/web/src/store.ts
Copy link
Member

@olestan olestan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the previous review findings. The original issues appear resolved.

Two minor follow-ups worth considering:

  • Image restoration: If the image check never completes, Save draft and Publish/Save changes can remain disabled. A timeout or skip option would improve resilience.
  • Cross-tab sign-out: Another open tab can retain stale authentication state and recreate drafts after sign-out. Consider synchronizing logout across tabs to strengthen the shared-computer privacy behavior.

Neither needs to block this PR.

…ent-drafts

# Conflicts:
#	apps/web/src/features/recipes/RecipeCreatePage.test.tsx
@oliveror oliveror merged commit ff45ebe into dev Nov 17, 2026
2 checks passed
@oliveror
Copy link
Member Author

Merged after updating from dev, which brought in #113's edit-page titles. The only conflict was in RecipeCreatePage.test.tsx, where both sides added a test block, so I kept both. CI passed.

The two non-blocking follow-ups from the last review are not in this PR and are still open:

  • a timeout or skip for the restored-draft image check, so Save draft and Publish can't stay disabled if the check never completes
  • syncing sign-out across open tabs, so another tab can't recreate drafts after sign-out

@oliveror oliveror deleted the feature/recipe-persistent-drafts branch November 20, 2026 15:31
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants