forked from snxraven/LinkUp-P2P-Chat
Add back file menu
This commit is contained in:
parent
084cb02a11
commit
c530a8d69c
5
app.js
5
app.js
@ -1116,6 +1116,7 @@ function addMessage(name, message, avatar, topic) {
|
||||
|
||||
const messageText = document.createElement('div');
|
||||
messageText.classList.add('message-text');
|
||||
|
||||
messageText.innerHTML = message;
|
||||
|
||||
messageContent.appendChild(senderName);
|
||||
@ -1162,7 +1163,6 @@ async function sendMessage(e) {
|
||||
const timestamp = Date.now();
|
||||
|
||||
console.log("Sending message to current topic:", topic); // Add logging
|
||||
|
||||
if (message.startsWith('~')) {
|
||||
await handleCommand(message, {
|
||||
eventEmitter,
|
||||
@ -1173,7 +1173,8 @@ async function sendMessage(e) {
|
||||
leaveRoom,
|
||||
createRoom,
|
||||
listFiles,
|
||||
deleteFile
|
||||
deleteFile,
|
||||
servePort
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
45
commands.js
45
commands.js
@ -11,7 +11,7 @@ if (fs.existsSync(agentAvatarPath)) {
|
||||
}
|
||||
|
||||
export default async function handleCommand(command, context) {
|
||||
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom, listFiles } = context;
|
||||
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom, listFiles, deleteFile, servePort } = context;
|
||||
|
||||
console.log("Context received in handleCommand:", context); // Add logging
|
||||
|
||||
@ -53,9 +53,52 @@ export default async function handleCommand(command, context) {
|
||||
const files = await listFiles();
|
||||
const fileList = files.length > 0 ? files.map(file => `- ${file}`).join('\n') : 'No files available';
|
||||
addMessage('LinkUp', `Available files:\n${fileList}`, agentAvatar, currentTopic);
|
||||
|
||||
// Render the file list with delete buttons
|
||||
renderFileList(files, deleteFile, servePort);
|
||||
break;
|
||||
default:
|
||||
addMessage('LinkUp', `Unknown command: ${cmd}`, agentAvatar, currentTopic);
|
||||
console.log('Unknown command:', command);
|
||||
}
|
||||
}
|
||||
|
||||
function renderFileList(files, deleteFile, servePort) {
|
||||
const container = document.querySelector('#messages');
|
||||
if (!container) {
|
||||
console.error('Element #messages not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const fileList = document.createElement('ul');
|
||||
|
||||
files.forEach(file => {
|
||||
const listItem = document.createElement('li');
|
||||
const fileButton = document.createElement('button');
|
||||
fileButton.textContent = file.trim();
|
||||
fileButton.onclick = () => downloadFile(file.trim(), servePort);
|
||||
|
||||
const deleteButton = document.createElement('button');
|
||||
deleteButton.textContent = 'Delete';
|
||||
deleteButton.onclick = async () => {
|
||||
await deleteFile(file);
|
||||
listItem.remove();
|
||||
};
|
||||
|
||||
listItem.appendChild(fileButton);
|
||||
listItem.appendChild(deleteButton);
|
||||
fileList.appendChild(listItem);
|
||||
});
|
||||
|
||||
container.appendChild(fileList);
|
||||
}
|
||||
|
||||
function downloadFile(filename, servePort) {
|
||||
const url = `http://localhost:${servePort}/files/${filename}`;
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user