icebox/views/index.html

217 lines
6.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebRTC File Transfer</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
}
.file-list {
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
max-height: 300px;
overflow-y: auto;
}
.file-item {
padding: 10px;
border-bottom: 1px solid #eee;
cursor: pointer;
display: flex;
justify-content: space-between;
}
.file-item:hover {
background-color: #f5f5f5;
}
.file-item:last-child {
border-bottom: none;
}
.progress-container {
margin: 20px 0;
display: none;
}
.progress-bar {
width: 100%;
background-color: #f0f0f0;
border-radius: 4px;
overflow: hidden;
}
.progress {
height: 20px;
background-color: #4CAF50;
width: 0%;
transition: width 0.3s ease;
}
.progress-text {
margin-top: 5px;
font-size: 14px;
color: #666;
}
.action-buttons {
margin: 20px 0;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
}
.success {
background-color: #e8f5e9;
color: #2e7d32;
}
.error {
background-color: #ffebee;
color: #c62828;
}
</style>
</head>
<body>
<h1>WebRTC File Transfer</h1>
<div class="action-buttons">
<button id="refreshBtn">Refresh File List</button>
</div>
<h2>Available Files</h2>
<div class="file-list" id="fileList">
<div class="file-item">Loading files...</div>
</div>
<div class="progress-container" id="progressContainer">
<h3 id="currentFileName">Downloading file...</h3>
<div class="progress-bar">
<div class="progress" id="progressBar"></div>
</div>
<div class="progress-text" id="progressText">0%</div>
</div>
<div id="statusContainer"></div>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/index.js"></script>
<script>
// Will be replaced with actual implementation from compiled client.ts
document.addEventListener('DOMContentLoaded', () => {
// Elements
const fileListEl = document.getElementById('fileList');
const progressContainer = document.getElementById('progressContainer');
const progressBar = document.getElementById('progressBar');
const progressText = document.getElementById('progressText');
const currentFileName = document.getElementById('currentFileName');
const refreshBtn = document.getElementById('refreshBtn');
const statusContainer = document.getElementById('statusContainer');
// Create client instance
const client = new WebRTCFileClient();
// Set up event handlers
client.onProgress((progress) => {
progressContainer.style.display = 'block';
progressBar.style.width = `${progress}%`;
progressText.innerText = `${Math.round(progress)}%`;
});
client.onComplete((file, fileName) => {
showStatus('success', `File "${fileName}" downloaded successfully!`);
client.saveFile(file, fileName);
progressContainer.style.display = 'none';
});
client.onError((error) => {
showStatus('error', `Error: ${error}`);
progressContainer.style.display = 'none';
});
client.onFilesList((files) => {
if (files.length === 0) {
fileListEl.innerHTML = '<div class="file-item">No files available</div>';
return;
}
fileListEl.innerHTML = '';
files.forEach(file => {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
// Create file info elements
const nameEl = document.createElement('span');
nameEl.textContent = file.name;
const sizeEl = document.createElement('span');
sizeEl.textContent = formatFileSize(file.size);
fileItem.appendChild(nameEl);
fileItem.appendChild(sizeEl);
fileItem.addEventListener('click', () => {
currentFileName.textContent = `Downloading ${file.name}`;
progressContainer.style.display = 'block';
progressBar.style.width = '0%';
progressText.innerText = '0%';
client.requestFile(file.name);
});
fileListEl.appendChild(fileItem);
});
});
// Get initial file list
client.getFilesList();
// Set up refresh button
refreshBtn.addEventListener('click', () => {
fileListEl.innerHTML = '<div class="file-item">Loading files...</div>';
client.getFilesList();
});
// Helper function to show status messages
function showStatus(type, message) {
const statusEl = document.createElement('div');
statusEl.className = `status ${type}`;
statusEl.textContent = message;
statusContainer.innerHTML = '';
statusContainer.appendChild(statusEl);
// Clear the status after 5 seconds
setTimeout(() => {
statusEl.remove();
}, 5000);
}
// Helper function to format file size
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
});
</script>
</body>
</html>