feat: add confirmation modal for enabling attachment sync in SurfSense plugin

This commit is contained in:
Anish Sarkar 2026-04-22 22:01:14 +05:30
parent 6eeaa2db4d
commit 23a52b6c63
2 changed files with 73 additions and 0 deletions

View file

@ -0,0 +1,61 @@
import { type App, Modal, Setting } from "obsidian";
/**
* Confirmation modal shown before enabling attachment sync.
* Attachment files can be large and increase sync latency/cost.
*/
export class AttachmentsConfirmModal extends Modal {
private resolver: ((confirmed: boolean) => void) | null = null;
constructor(app: App) {
super(app);
}
onOpen(): void {
this.setTitle("Enable attachment sync?");
this.contentEl.empty();
new Setting(this.contentEl).setDesc(
"Syncing attachments (images, PDFs, and other non-Markdown files) can make indexing slower, especially on large vaults.",
);
new Setting(this.contentEl).setDesc(
"You can disable this anytime in settings if syncing becomes too slow.",
);
new Setting(this.contentEl)
.addButton((btn) =>
btn
.setButtonText("Cancel")
.onClick(() => this.resolveAndClose(false)),
)
.addButton((btn) =>
btn
.setButtonText("Enable")
.setCta()
.onClick(() => this.resolveAndClose(true)),
);
}
onClose(): void {
this.contentEl.empty();
if (this.resolver) {
this.resolver(false);
this.resolver = null;
}
}
waitForConfirmation(): Promise<boolean> {
this.open();
return new Promise<boolean>((resolve) => {
this.resolver = resolve;
});
}
private resolveAndClose(confirmed: boolean): void {
if (this.resolver) {
this.resolver(confirmed);
this.resolver = null;
}
this.close();
}
}

View file

@ -7,6 +7,7 @@ import {
setIcon,
} from "obsidian";
import { AuthError } from "./api-client";
import { AttachmentsConfirmModal } from "./attachments-confirm-modal";
import { normalizeFolder, parseExcludePatterns } from "./excludes";
import { FolderSuggestModal } from "./folder-suggest-modal";
import type SurfSensePlugin from "./main";
@ -210,6 +211,17 @@ export class SurfSenseSettingTab extends PluginSettingTab {
toggle
.setValue(settings.includeAttachments)
.onChange(async (value) => {
const isEnabling =
value && !this.plugin.settings.includeAttachments;
if (isEnabling) {
const confirmed = await new AttachmentsConfirmModal(
this.app,
).waitForConfirmation();
if (!confirmed) {
this.display();
return;
}
}
this.plugin.settings.includeAttachments = value;
await this.plugin.saveSettings();
}),