Mark sync completed on desktop client, even when no files to send

Previously Sync spinner on desktop config screen would hang when no
files to send to server & the Sync button had been manually triggered
This commit is contained in:
Debanjum Singh Solanky 2023-10-18 01:00:41 -07:00
parent 71b0012e8c
commit 53abd1a506

View file

@ -111,10 +111,12 @@ function pushDataToKhoj (regenerate = false) {
const folders = store.get('folders') || []; const folders = store.get('folders') || [];
state = { completed: true } state = { completed: true }
// Collect paths of all configured files to index
for (const file of files) { for (const file of files) {
filesToPush.push(file.path); filesToPush.push(file.path);
} }
// Collect paths of all indexable files in configured folders
for (const folder of folders) { for (const folder of folders) {
const files = fs.readdirSync(folder.path, { withFileTypes: true }); const files = fs.readdirSync(folder.path, { withFileTypes: true });
for (const file of files) { for (const file of files) {
@ -129,11 +131,13 @@ function pushDataToKhoj (regenerate = false) {
for (const file of filesToPush) { for (const file of filesToPush) {
const stats = fs.statSync(file); const stats = fs.statSync(file);
if (!regenerate) { if (!regenerate) {
// Only push files that have been modified since last sync
if (stats.mtime.toISOString() < lastSync.find((syncedFile) => syncedFile.path === file)?.datetime) { if (stats.mtime.toISOString() < lastSync.find((syncedFile) => syncedFile.path === file)?.datetime) {
continue; continue;
} }
} }
// Collect all updated or newly created files since last sync to index on Khoj server
try { try {
let encoding = binaryFileTypes.includes(file.split('.').pop()) ? "binary" : "utf8"; let encoding = binaryFileTypes.includes(file.split('.').pop()) ? "binary" : "utf8";
let mimeType = filenameToMimeType(file) + (encoding === "utf8" ? "; charset=UTF-8" : ""); let mimeType = filenameToMimeType(file) + (encoding === "utf8" ? "; charset=UTF-8" : "");
@ -152,6 +156,7 @@ function pushDataToKhoj (regenerate = false) {
} }
} }
// Mark deleted files for removal from index on Khoj server
for (const syncedFile of lastSync) { for (const syncedFile of lastSync) {
if (!filesToPush.includes(syncedFile.path)) { if (!filesToPush.includes(syncedFile.path)) {
fileObj = new Blob([""], { type: filenameToMimeType(syncedFile.path) }); fileObj = new Blob([""], { type: filenameToMimeType(syncedFile.path) });
@ -159,6 +164,7 @@ function pushDataToKhoj (regenerate = false) {
} }
} }
// Send collected files to Khoj server for indexing
if (!!formData?.entries()?.next().value) { if (!!formData?.entries()?.next().value) {
const hostURL = store.get('hostURL') || KHOJ_URL; const hostURL = store.get('hostURL') || KHOJ_URL;
const headers = { const headers = {
@ -167,8 +173,6 @@ function pushDataToKhoj (regenerate = false) {
axios.post(`${hostURL}/api/v1/index/update?force=${regenerate}&client=desktop`, formData, { headers }) axios.post(`${hostURL}/api/v1/index/update?force=${regenerate}&client=desktop`, formData, { headers })
.then(response => { .then(response => {
console.log(response.data); console.log(response.data);
const win = BrowserWindow.getAllWindows()[0];
win.webContents.send('update-state', state);
let lastSync = []; let lastSync = [];
for (const file of filesToPush) { for (const file of filesToPush) {
lastSync.push({ lastSync.push({
@ -181,9 +185,16 @@ function pushDataToKhoj (regenerate = false) {
.catch(error => { .catch(error => {
console.error(error); console.error(error);
state['completed'] = false state['completed'] = false
})
.finally(() => {
// Syncing complete
const win = BrowserWindow.getAllWindows()[0]; const win = BrowserWindow.getAllWindows()[0];
win.webContents.send('update-state', state); if (win) win.webContents.send('update-state', state);
}); });
} else {
// Syncing complete
const win = BrowserWindow.getAllWindows()[0];
if (win) win.webContents.send('update-state', state);
} }
} }