feat: enhance auto sync functionality to support multiple databases and improved user notifications

This commit is contained in:
Jiaxin Peng
2025-10-31 23:36:57 +00:00
parent 7c841f3db3
commit 83d4f1e48c

View File

@@ -226,41 +226,63 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
return; return;
} }
// Find which database this file belongs to by checking for NotionID-{abName} // Find all databases this file belongs to by checking for NotionID-{abName}
let foundDbDetails: DatabaseDetails | null = null; const foundDatabases: Array<{ dbDetails: DatabaseDetails, notionId: string }> = [];
let notionId: string | null = null;
for (const key in this.settings.databaseDetails) { for (const key in this.settings.databaseDetails) {
const dbDetails = this.settings.databaseDetails[key]; const dbDetails = this.settings.databaseDetails[key];
const notionIDKey = `NotionID-${dbDetails.abName}`; const notionIDKey = `NotionID-${dbDetails.abName}`;
if (frontMatter[notionIDKey]) { if (frontMatter[notionIDKey]) {
foundDbDetails = dbDetails; foundDatabases.push({
notionId = String(frontMatter[notionIDKey]); dbDetails: dbDetails,
break; notionId: String(frontMatter[notionIDKey])
});
} }
} }
// If no NotionID found, skip auto sync // If no NotionID found, notify user to upload manually first
if (!foundDbDetails || !notionId) { if (foundDatabases.length === 0) {
console.log(`[AutoSync] No NotionID found in ${file.path}, skipping auto sync`); console.log(`[AutoSync] No NotionID found in ${file.path}, skipping auto sync`);
new Notice(i18nConfig.AutoSyncNoNotionID, 4000);
return; return;
} }
console.log(`[AutoSync] ${new Date().toISOString()} Auto syncing ${file.basename} to ${foundDbDetails.fullName}`); // Notify user about multiple syncs if applicable
if (foundDatabases.length > 1) {
const message = i18nConfig.AutoSyncMultipleSync.replace('{count}', String(foundDatabases.length));
new Notice(message, 3000);
console.log(`[AutoSync] Found ${foundDatabases.length} NotionIDs in ${file.path}`);
}
// Trigger appropriate upload command based on database format // Sync to all found databases
if (foundDbDetails.format === 'next') { for (const { dbDetails, notionId } of foundDatabases) {
await uploadCommandNext(this, this.settings, foundDbDetails, this.app); console.log(`[AutoSync] ${new Date().toISOString()} Auto syncing ${file.basename} to ${dbDetails.fullName} (${dbDetails.abName})`);
} else if (foundDbDetails.format === 'general') {
await uploadCommandGeneral(this, this.settings, foundDbDetails, this.app); try {
} else if (foundDbDetails.format === 'custom') { // Trigger appropriate upload command based on database format
await uploadCommandCustom(this, this.settings, foundDbDetails, this.app); if (dbDetails.format === 'next') {
await uploadCommandNext(this, this.settings, dbDetails, this.app);
} else if (dbDetails.format === 'general') {
await uploadCommandGeneral(this, this.settings, dbDetails, this.app);
} else if (dbDetails.format === 'custom') {
await uploadCommandCustom(this, this.settings, dbDetails, this.app);
}
} catch (error) {
console.error(`[AutoSync] Error syncing to ${dbDetails.fullName}:`, error);
const message = i18nConfig.AutoSyncFailed
.replace('{database}', dbDetails.fullName)
.replace('{error}', error.message);
new Notice(message, 5000);
}
} }
} catch (error) { } catch (error) {
console.error(`[AutoSync] Error syncing file ${file.path}:`, error); console.error(`[AutoSync] Error syncing file ${file.path}:`, error);
new Notice(`Auto sync failed for ${file.basename}: ${error.message}`); const message = i18nConfig.AutoSyncError
.replace('{filename}', file.basename)
.replace('{error}', error.message);
new Notice(message);
} finally { } finally {
this.syncingFiles.delete(file.path); this.syncingFiles.delete(file.path);
} }