fix(dm): add image paste support to DM chat and fix realtime DM updates

This commit is contained in:
2026-07-06 13:44:41 +00:00
parent 0c26e2402b
commit c2fb3165f4
3 changed files with 127 additions and 31 deletions
+53
View File
@@ -149,6 +149,58 @@ export function DMChat() {
}
};
const handlePaste = async (e: React.ClipboardEvent<HTMLInputElement>) => {
const items = e.clipboardData.items;
let imageFile: File | null = null;
for (let i = 0; i < items.length; i++) {
if (items[i].type.startsWith('image/')) {
imageFile = items[i].getAsFile();
break;
}
}
if (imageFile) {
e.preventDefault();
const formData = new FormData();
formData.append('file', imageFile);
try {
const res = await fetch('/api/v1/upload', {
method: 'POST',
body: formData,
credentials: 'include'
});
if (!res.ok) throw new Error('Upload failed');
const data = await res.json();
const imageUrl = data.url;
// Insert into input
const imgMarkdown = `![image](${imageUrl})`;
const inputEl = inputRef.current;
if (inputEl) {
const start = inputEl.selectionStart || 0;
const end = inputEl.selectionEnd || 0;
const newValue = input.slice(0, start) + imgMarkdown + input.slice(end);
setInput(newValue);
setTimeout(() => {
inputEl.focus();
inputEl.setSelectionRange(start + imgMarkdown.length, start + imgMarkdown.length);
}, 0);
} else {
setInput(prev => prev + (prev.length > 0 && !prev.endsWith(' ') ? ' ' : '') + imgMarkdown);
}
} catch (err) {
console.error('Failed to upload image:', err);
}
}
};
const selfDM = conversation && conversation.members.length === 1 && conversation.members[0].id === currentUser?.id;
const title = selfDM
? "Notes"
@@ -215,6 +267,7 @@ export function DMChat() {
}
}
}}
onPaste={handlePaste}
placeholder="type a message..."
className="terminal-input w-full"
disabled={!id}