Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reply storage is updated with the new content on file upload #1585

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

aegroto
Copy link

@aegroto aegroto commented Nov 12, 2024

Description

Images and other file uploads update the draft reply, fixes #1522

Screenshots

Unfortunately it's difficult to show the difference using screenshots, maybe I can try with a screen recording later?

Additional Context

Nothing to report.

Checklist

Are your changes backwards compatible? Please answer below: Yes

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below: 8. Throughly tested manually on dummy posts, no automatic tests.

For frontend changes: Tested on mobile? Please answer below: Yes, they work.

Did you introduce any new environment variables? If so, call them out explicitly here: No

@ekzyis ekzyis self-requested a review November 12, 2024 20:02
@ekzyis
Copy link
Member

ekzyis commented Nov 12, 2024

Hey, thanks for the PR! Videos are nice, especially for UI/UX stuff but no need here anymore, I saw that this fixes it:

2024-11-12.21-43-17.mp4

This is a nice way to fix it as simple as possible but ideally, I'd like to see a fix that triggers a change event so we can reuse the logic in this function:

const onChangeInner = useCallback((e) => {
field?.onChange(e)
if (storageKey) {
window.localStorage.setItem(storageKey, e.target.value)
}
if (onChange) {
onChange(formik, e)
}
}, [field?.onChange, storageKey, onChange])

I tried the following when #1522 was created but for some reason, the callback doesn't run (I just tried again).

diff --git a/components/form.js b/components/form.js
index a67853f8..c38d6142 100644
--- a/components/form.js
+++ b/components/form.js
@@ -370,6 +370,7 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, onKe
                 const s3Keys = [...text.matchAll(AWS_S3_URL_REGEXP)].map(m => Number(m[1]))
                 updateUploadFees({ variables: { s3Keys } })
                 setSubmitDisabled?.(false)
+                innerRef.current.dispatchEvent(new Event('input', { bubbles: true, value: text }))
               }}
               onError={({ name }) => {
                 let text = innerRef.current.value

Can you check if you can find out how to get this to work or at least why it doesn't work? It would be a cleaner solution because it reuses code and makes changing the input via image upload consistent with any other change since there's more code in that callback than just window.localStorage.setItem.

components/form.js Outdated Show resolved Hide resolved
@aegroto
Copy link
Author

aegroto commented Nov 13, 2024

Hello, thanks for the detailed review! I acknowledge that a solution calling the an event would be better. Just wondering, may moving the whole logic the local storage from MarkdownInput to the parent component, in the onChange event, work for you?

@aegroto
Copy link
Author

aegroto commented Nov 13, 2024

I have converted the code to work with events by following this snippet: facebook/react#10135 (comment)

The issue may be related to the fact that change events are dropped if the text is not changed in practice, therefore the setNativeValue function uses some sort of reflection to bypass this mechanism.

I admit this looks kind of hacky to me and would prefer a React-based solution instead, but if you'd like to work with events this is a deal.

@ekzyis ekzyis self-requested a review November 13, 2024 15:12
Copy link
Member

@ekzyis ekzyis left a comment

Choose a reason for hiding this comment

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

Wow, awesome that you found out why it wasn't working!

Added some suggestions how this could be even improved.

Requesting changes because the Formik handler should definitely be called imo.

components/form.js Show resolved Hide resolved
components/form.js Outdated Show resolved Hide resolved
components/form.js Outdated Show resolved Hide resolved
@ekzyis ekzyis added the bug label Nov 13, 2024
@aegroto
Copy link
Author

aegroto commented Nov 13, 2024

Thanks for your feedback, I have integrated your changes and I confirm everything is still working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

images in drafts are deleted when navigating to a link in preview
2 participants