-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: drag-and-drop attachment reordering #709
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
787fc5b
750ba4d
95689d2
03b9dc7
2fbb57f
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -82,6 +82,8 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>( | |||||||||||||||
| const [slashSelectedIndex, setSlashSelectedIndex] = useState(0); | ||||||||||||||||
| const [attachments, setAttachments] = useState<Attachment[]>([]); | ||||||||||||||||
| const [attachmentError, setAttachmentError] = useState<string | null>(null); | ||||||||||||||||
| const [dragIndex, setDragIndex] = useState<number | null>(null); | ||||||||||||||||
| const [dragOverIndex, setDragOverIndex] = useState<number | null>(null); | ||||||||||||||||
| const inputRef = useRef<HTMLTextAreaElement>(null); | ||||||||||||||||
| const slashMenuRef = useRef<HTMLDivElement>(null); | ||||||||||||||||
| const fileInputRef = useRef<HTMLInputElement>(null); | ||||||||||||||||
|
|
@@ -382,6 +384,35 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>( | |||||||||||||||
| setAttachmentError(null); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Drag-and-drop reordering | ||||||||||||||||
| function handleDragStart(e: React.DragEvent, index: number): void { | ||||||||||||||||
| setDragIndex(index); | ||||||||||||||||
| e.dataTransfer.effectAllowed = "move"; | ||||||||||||||||
| e.dataTransfer.setData("text/plain", String(index)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| function handleDragOver(e: React.DragEvent, index: number): void { | ||||||||||||||||
| e.preventDefault(); | ||||||||||||||||
| e.dataTransfer.dropEffect = "move"; | ||||||||||||||||
| setDragOverIndex(index); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| function handleDrop(e: React.DragEvent, index: number): void { | ||||||||||||||||
| e.preventDefault(); | ||||||||||||||||
| if (dragIndex === null || dragIndex === index) return; | ||||||||||||||||
| setAttachments((prev) => { | ||||||||||||||||
| const next = [...prev]; | ||||||||||||||||
| const [moved] = next.splice(dragIndex, 1); | ||||||||||||||||
| next.splice(index, 0, moved); | ||||||||||||||||
| return next; | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| function handleDragEnd(): void { | ||||||||||||||||
| setDragIndex(null); | ||||||||||||||||
| setDragOverIndex(null); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Pre-send validation gate (#369): even with the queue model from | ||||||||||||||||
| // PR #379, we still block Send when readiness fails — a queued message | ||||||||||||||||
| // with a missing API key would just fail later. The !isLoading gate | ||||||||||||||||
|
|
@@ -455,12 +486,21 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>( | |||||||||||||||
| )} | ||||||||||||||||
| {(attachments.length > 0 || attachmentError) && ( | ||||||||||||||||
| <div className="chat-attachment-strip"> | ||||||||||||||||
| {attachments.map((att) => ( | ||||||||||||||||
| <AttachmentChip | ||||||||||||||||
| {attachments.map((att, i) => ( | ||||||||||||||||
| <div | ||||||||||||||||
| key={att.id} | ||||||||||||||||
| attachment={att} | ||||||||||||||||
| onRemove={() => removeAttachment(att.id)} | ||||||||||||||||
| /> | ||||||||||||||||
| className={`attachment-drag-wrapper${dragOverIndex === i ? " attachment-drag-over" : ""}${dragIndex === i ? " attachment-dragging" : ""}`} | ||||||||||||||||
|
Contributor
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.
When
Suggested change
|
||||||||||||||||
| draggable | ||||||||||||||||
| onDragStart={(e) => handleDragStart(e, i)} | ||||||||||||||||
| onDragOver={(e) => handleDragOver(e, i)} | ||||||||||||||||
| onDrop={(e) => handleDrop(e, i)} | ||||||||||||||||
| onDragEnd={handleDragEnd} | ||||||||||||||||
|
Comment on lines
+495
to
+497
Contributor
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.
Suggested change
|
||||||||||||||||
| > | ||||||||||||||||
| <AttachmentChip | ||||||||||||||||
| attachment={att} | ||||||||||||||||
| onRemove={() => removeAttachment(att.id)} | ||||||||||||||||
| /> | ||||||||||||||||
| </div> | ||||||||||||||||
| ))} | ||||||||||||||||
| {attachmentError && ( | ||||||||||||||||
| <div className="chat-attachment-error" role="alert"> | ||||||||||||||||
|
|
||||||||||||||||
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.
handleDropreorders the attachments but leavesdragIndexanddragOverIndexset. TheonDragEndevent does fire afteronDropper the HTML5 DnD spec and will clear them, but callinghandleDragEnd()directly insidehandleDropmakes the cleanup unconditional and symmetrical, and avoids any edge-case flash ifonDragEndis delayed or skipped in unusual browser environments.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!