let backupDirId = ''; let shareDirId = ''; let departmentDirId = ''; document.addEventListener('DOMContentLoaded', async function () { setInterval(loadReceivedFiles, 3000); // Call loadReceivedFiles every 3000 ms (3 seconds) const backupButton = document.getElementById('backup-dir'); const shareButton = document.getElementById('share-dir'); const departmentButton = document.getElementById('department-dir'); const changeInfoButton = document.getElementById('change-info'); const shareFileButton = document.getElementById('share-file'); const logoutButton = document.getElementById('logout'); const restoreBackupButton = document.getElementById('restore-backup'); backupButton.addEventListener('click', async function () { await setPath('backupDirectory'); }); shareButton.addEventListener('click', async function () { await setPath('shareDirectory'); }); departmentButton.addEventListener('click', async function () { await setPath('departmentDirectory'); }); changeInfoButton.addEventListener('click', function () { fadeOut('profile'); }); shareFileButton.addEventListener('click', function () { fadeOut('share_file'); }); logoutButton.addEventListener('click', async function () { fadeOut('login'); }); // Add event listener for the restore backup button restoreBackupButton.addEventListener('click', async function () { await restoreBackup(); }); }); async function initialSetup(){ await checkAndSetAllDirectories(); await loadReceivedFiles(); await fetchUserInfo(); } async function restoreBackup() { const backupDirectory = await window.databaseAPI.isBackupSet(); if (backupDirectory) { await window.uiAPI.showAlert('You have already set a backup directory. You cannot restore again.'); return; } // Prompt the user to choose the destination for the restored backup const destinationPath = await window.uiAPI.selectDirectory(); if (!destinationPath) { return; // User canceled the directory selection } // Call the IPC method to initiate the backup retrieval process window.uiAPI.startBackupRetrieval(destinationPath); // Switch the content to the 'backup_retrieve' page fadeOut('backup_retrieve'); } async function checkAndSetAllDirectories() { const directorySchemes = await window.databaseAPI.getLocalResources(); backupDirId = directorySchemes.backup.id; shareDirId = directorySchemes.shared.id; departmentDirId = directorySchemes.department.id; await attachNotificationButton(backupDirId, 'Set your backup directory!', 'backup_alert', 'alert'); await attachNotificationButton(shareDirId, 'Set your share directory!', 'share_alert', 'alert'); await attachNotificationButton(departmentDirId, 'Set your department directory!', 'department_alert', 'alert'); } async function checkPathExistence(id) { const dirInfo = await window.databaseAPI.getDirectoryInfo(id); return dirInfo.path !== '' } async function setPath(id){ const path = await window.uiAPI.selectDirectory(); if (path === undefined) return false; return await window.databaseAPI.writeDirectoryPath(id, path); } async function attachNotificationButton(entryId, buttonText, buttonId, buttonName) { const path = await checkPathExistence(entryId); if (!path) { const notificationsDiv = document.getElementById('notifications'); const button = document.createElement('button'); button.className='alert'; button.id = buttonId; button.name = buttonName; button.textContent = buttonText; button.addEventListener('click', async function () { if(await setPath(entryId)) button.remove(); }); notificationsDiv.appendChild(button); } } async function fetchUserInfo() { const usernameField = document.getElementById('username-field'); // Read the user credentials from the userConfig let userInfo = await window.databaseAPI.getUserInfo(); if (userInfo && userInfo.name) { usernameField.textContent = userInfo.name; return; } // Update the greeting with the fetched user's name if (usernameField) { usernameField.textContent = userInfo.name; } else { console.error("Username field is not available in the DOM."); } } async function loadReceivedFiles() { // Read the shareDirectory from applicationInfo const shareDirData = await window.databaseAPI.getDirectoryInfo(shareDirId); console.log('Loading received files:', shareDirData); const notificationsDiv = document.getElementById('notifications'); const existingButtons = Array.from(notificationsDiv.children).map(button => button.getAttribute('data-filepath')); // Iterate over each user and their files in the structure Object.keys(shareDirData.structure).forEach(userName => { const userFiles = shareDirData.structure[userName]; // Iterate over each file of the user Object.keys(userFiles).forEach(fileName => { const filePath = userFiles[fileName]; // Check if a button for this file path already exists if (!existingButtons.includes(filePath)) { const button = document.createElement('button'); button.setAttribute('data-filepath', filePath); // Set a custom attribute to track the file path button.className = 'notification'; button.name = 'notification'; button.textContent = `You received a file "${fileName}" from ${userName}`; // Display the userName and file name button.addEventListener('click', () => handleFileReceivedButtonPressed(filePath, button)); notificationsDiv.appendChild(button); } }); }); } async function handleFileReceivedButtonPressed(filePath, button) { console.log('Notification button clicked!'); // Open the file in the file explorer await window.uiAPI.showFileInExplorer(filePath) .then(() => console.log('File explorer opened for: ' + filePath)) .catch(error => console.error('Error opening file explorer:', error)); // Remove the button after opening the file button.remove(); }