-
Notifications
You must be signed in to change notification settings - Fork 2
Message edit save button #75
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
Changes from 4 commits
c927465
5923464
6021c52
54ec812
5e2756b
b214ca8
79d0b8a
8078cfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -383,6 +383,8 @@ function MessageForms({ | |
| const deleteSafeDelayMs = 150 | ||
| const [confirmDelete, setConfirmDelete] = useState(false) | ||
| const [canDelete, setCanDelete] = useState(false) | ||
| const [savedContent, setSavedContent] = useState(message.content) | ||
| const [currentContent, setCurrentContent] = useState(message.content) | ||
| const formRef = useRef<HTMLFormElement | null>(null) | ||
| const textareaRef = useRef<HTMLTextAreaElement | null>(null) | ||
| const [updateContentForm, updateContentFields] = useForm({ | ||
|
|
@@ -408,6 +410,8 @@ function MessageForms({ | |
| const sendIsPending = sendNowFetcher.state !== 'idle' | ||
| const deleteIsPending = deleteFetcher.state !== 'idle' | ||
| const textareaProps = getTextareaProps(updateContentFields.content) | ||
| const hasEdits = currentContent !== savedContent | ||
| const showSaveButton = hasEdits || updateIsPending | ||
|
|
||
| useEffect(() => { | ||
| if (confirmDelete) { | ||
|
|
@@ -419,6 +423,19 @@ function MessageForms({ | |
| setCanDelete(false) | ||
| }, [confirmDelete, deleteSafeDelayMs]) | ||
|
|
||
| useEffect(() => { | ||
| setSavedContent(message.content) | ||
| setCurrentContent(message.content) | ||
| }, [message.content]) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cursor, ditch this useEffect in favor of just setting the current content in the event handler There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Walkthrough
Summary
Testing
|
||
|
|
||
| useEffect(() => { | ||
| if (updateContentFetcher.data?.result.status === 'success') { | ||
| const nextContent = textareaRef.current?.value ?? currentContent | ||
| setSavedContent(nextContent) | ||
| setCurrentContent(nextContent) | ||
| } | ||
| }, [currentContent, updateContentFetcher.data?.result.status]) | ||
|
|
||
| const handleSendNow = () => { | ||
| setConfirmDelete(false) | ||
| const formData = new FormData() | ||
|
|
@@ -429,7 +446,19 @@ function MessageForms({ | |
|
|
||
| const handleEditMessage = () => { | ||
| setConfirmDelete(false) | ||
| setTimeout(() => textareaRef.current?.focus(), 0) | ||
| requestAnimationFrame(() => { | ||
| const textarea = textareaRef.current | ||
| if (!textarea) return | ||
| textarea.focus() | ||
| const caretPosition = textarea.value.length | ||
| textarea.setSelectionRange(caretPosition, caretPosition) | ||
| }) | ||
|
kentcdodds marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const handleContentInput: React.FormEventHandler<HTMLTextAreaElement> = ( | ||
| event, | ||
| ) => { | ||
| setCurrentContent(event.currentTarget.value) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cursoragent just make this inline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Summary
Testing
|
||
| } | ||
|
|
||
| const handleDeleteSelect = (event: Event) => { | ||
|
|
@@ -461,19 +490,21 @@ function MessageForms({ | |
| <span>{headerText}</span> | ||
| </div> | ||
| <div className="flex items-center gap-2"> | ||
| <StatusButton | ||
| form={updateContentForm.id} | ||
| status={updateIsPending ? 'pending' : 'idle'} | ||
| className="h-11 w-11 gap-0 text-[hsl(var(--palette-cream))] hover:bg-[hsl(var(--palette-cream))/0.15] sm:h-10 sm:w-10" | ||
| size="icon" | ||
| variant="ghost" | ||
| type="submit" | ||
| name="intent" | ||
| value={updateMessageContentActionIntent} | ||
| > | ||
| <Icon name="check" size="sm" /> | ||
| <span className="sr-only">Save</span> | ||
| </StatusButton> | ||
| {showSaveButton ? ( | ||
| <StatusButton | ||
| form={updateContentForm.id} | ||
| status={updateIsPending ? 'pending' : 'idle'} | ||
| className="h-11 w-11 gap-0 text-[hsl(var(--palette-cream))] hover:bg-[hsl(var(--palette-cream))/0.15] sm:h-10 sm:w-10" | ||
| size="icon" | ||
| variant="ghost" | ||
| type="submit" | ||
| name="intent" | ||
| value={updateMessageContentActionIntent} | ||
| > | ||
| <Icon name="check" size="sm" /> | ||
| <span className="sr-only">Save</span> | ||
| </StatusButton> | ||
| ) : null} | ||
| <DropdownMenu | ||
| onOpenChange={(open) => { | ||
| if (!open) setConfirmDelete(false) | ||
|
|
@@ -532,6 +563,7 @@ function MessageForms({ | |
| </label> | ||
| <textarea | ||
| {...textareaProps} | ||
| onInput={handleContentInput} | ||
| ref={textareaRef} | ||
| className="mt-4 w-full resize-none bg-transparent text-sm leading-relaxed text-[hsl(var(--palette-cream))] placeholder:text-[hsl(var(--palette-cream))]/80 focus-visible:outline-none" | ||
| rows={4} | ||
|
|
||



There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursor,
message.contentwill always be the saved content (react router revalidates on changes), so we don't need state for this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking a look!