Skip to content

Preserve internal comment draft across reloads#1133

Draft
unasuke wants to merge 1 commit into
rubycentral:mainfrom
unasuke:preserve-internal-comment
Draft

Preserve internal comment draft across reloads#1133
unasuke wants to merge 1 commit into
rubycentral:mainfrom
unasuke:preserve-internal-comment

Conversation

@unasuke

@unasuke unasuke commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What

Add a comment-draft Stimulus controller that saves the internal comment textarea to localStorage (debounced on input) and restores it on reconnect, mirroring GitHub's issue/PR comment draft behavior.

Since CommentsController redirect_backs on both success and validation failure, turbo:submit-end can't distinguish them. Clear the draft optimistically on submit-start and restore it only on a failed request (success === false), which also avoids the re-render race where a reconnected controller would otherwise restore the just-submitted text.

Screenshots

before

461e092a0a3ee9a6185709a078d07df2.mp4

after

13d19a0db48ad8f8ef13bed389954e19.mp4

Add a `comment-draft` Stimulus controller that saves the internal
comment textarea to localStorage (debounced on input) and restores it
on reconnect, mirroring GitHub's issue/PR comment draft behavior.

Since CommentsController redirect_backs on both success and validation
failure, turbo:submit-end can't distinguish them. Clear the draft
optimistically on submit-start and restore it only on a failed request
(success === false), which also avoids the re-render race where a
reconnected controller would otherwise restore the just-submitted text.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@amatsuda

Copy link
Copy Markdown
Collaborator

@unasuke I suppose #1134 would solve 95% of your problem. Can you take a look at that patch? Still with the patch, the form content may disappear if the user hard-reloads the browser, but that's just how the browsers work. I don't think that's something the app developers should deal with.

amatsuda added a commit that referenced this pull request Jul 11, 2026
Star-rating clicks used to re-render the whole rating widget, clobbering
any internal-comment draft mid-typing (the motivating gap in PR #1133's
localStorage approach). The rating form now lives in a nested
rating_form frame; the controller responds with Turbo Streams so each
transition replaces exactly what it changes: score updates swap only the
rating form, while the first rating and rating removal still replace the
whole widget (they toggle the internal-comments block's visibility).

A localStorage layer for true page reloads (PR #1133) remains
complementary; with this change it no longer needs to compensate for
in-page re-renders.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@unasuke unasuke marked this pull request as draft July 11, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants