forked from MODSetter/SurfSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
380 lines (350 loc) · 10.3 KB
/
Copy pathsettings.ts
File metadata and controls
380 lines (350 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import {
type App,
Notice,
Platform,
PluginSettingTab,
Setting,
setIcon,
} from "obsidian";
import { AuthError } from "./api-client";
import { normalizeFolder, parseExcludePatterns } from "./excludes";
import { FolderSuggestModal } from "./folder-suggest-modal";
import type SurfSensePlugin from "./main";
import type { SearchSpace } from "./types";
/** Plugin settings tab. */
export class SurfSenseSettingTab extends PluginSettingTab {
private readonly plugin: SurfSensePlugin;
private searchSpaces: SearchSpace[] = [];
private loadingSpaces = false;
constructor(app: App, plugin: SurfSensePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
const settings = this.plugin.settings;
this.renderConnectionHeading(containerEl);
new Setting(containerEl)
.setName("Server URL")
.setDesc(
"https://surfsense.com for SurfSense Cloud, or your self-hosted URL.",
)
.addText((text) =>
text
.setPlaceholder("https://surfsense.com")
.setValue(settings.serverUrl)
.onChange(async (value) => {
const next = value.trim();
const previous = this.plugin.settings.serverUrl;
if (previous !== "" && next !== previous) {
this.plugin.settings.searchSpaceId = null;
this.plugin.settings.connectorId = null;
}
this.plugin.settings.serverUrl = next;
await this.plugin.saveSettings();
}),
);
new Setting(containerEl)
.setName("API token")
.setDesc(
"Paste your Surfsense API token (expires after 24 hours; re-paste when you see an auth error).",
)
.addText((text) => {
text.inputEl.type = "password";
text.inputEl.autocomplete = "off";
text.inputEl.spellcheck = false;
text
.setPlaceholder("Paste token")
.setValue(settings.apiToken)
.onChange(async (value) => {
const next = value.trim();
const previous = this.plugin.settings.apiToken;
if (previous !== "" && next !== previous) {
this.plugin.settings.searchSpaceId = null;
this.plugin.settings.connectorId = null;
}
this.plugin.settings.apiToken = next;
await this.plugin.saveSettings();
});
})
.addButton((btn) =>
btn
.setButtonText("Verify")
.setCta()
.onClick(async () => {
btn.setDisabled(true);
try {
await this.plugin.api.verifyToken();
new Notice("Surfsense: token verified.");
await this.refreshSearchSpaces();
this.display();
} catch (err) {
this.handleApiError(err);
} finally {
btn.setDisabled(false);
}
}),
);
new Setting(containerEl)
.setName("Search space")
.setDesc(
"Which Surfsense search space this vault syncs into. Reload after changing your token.",
)
.addDropdown((drop) => {
drop.addOption("", this.loadingSpaces ? "Loading…" : "Select a search space");
for (const space of this.searchSpaces) {
drop.addOption(String(space.id), space.name);
}
if (settings.searchSpaceId !== null) {
drop.setValue(String(settings.searchSpaceId));
}
drop.onChange(async (value) => {
this.plugin.settings.searchSpaceId = value ? Number(value) : null;
this.plugin.settings.connectorId = null;
await this.plugin.saveSettings();
if (this.plugin.settings.searchSpaceId !== null) {
try {
await this.plugin.engine.ensureConnected();
await this.plugin.engine.maybeReconcile(true);
new Notice("Surfsense: vault connected.");
this.display();
} catch (err) {
this.handleApiError(err);
}
}
});
})
.addExtraButton((btn) =>
btn
.setIcon("refresh-ccw")
.setTooltip("Reload search spaces")
.onClick(async () => {
await this.refreshSearchSpaces();
this.display();
}),
);
new Setting(containerEl).setName("Vault").setHeading();
new Setting(containerEl)
.setName("Sync interval")
.setDesc(
"How often to check for changes made outside Obsidian. Set to off to only sync manually.",
)
.addDropdown((drop) => {
const options: Array<[number, string]> = [
[0, "Off"],
[5, "5 minutes"],
[10, "10 minutes"],
[15, "15 minutes"],
[30, "30 minutes"],
[60, "60 minutes"],
[120, "2 hours"],
[360, "6 hours"],
[720, "12 hours"],
[1440, "24 hours"],
];
for (const [value, label] of options) {
drop.addOption(String(value), label);
}
drop.setValue(String(settings.syncIntervalMinutes));
drop.onChange(async (value) => {
this.plugin.settings.syncIntervalMinutes = Number(value);
await this.plugin.saveSettings();
this.plugin.restartReconcileTimer();
});
});
this.renderFolderList(
containerEl,
"Include folders",
"Folders to sync (leave empty to sync entire vault).",
settings.includeFolders,
(next) => {
this.plugin.settings.includeFolders = next;
},
);
this.renderFolderList(
containerEl,
"Exclude folders",
"Folders to exclude from sync (takes precedence over includes).",
settings.excludeFolders,
(next) => {
this.plugin.settings.excludeFolders = next;
},
);
new Setting(containerEl)
.setName("Advanced exclude patterns")
.setDesc(
"Glob fallback for power users. One pattern per line, supports * and **. Lines starting with # are comments. Applied on top of the folder lists above.",
)
.addTextArea((area) => {
area.inputEl.rows = 4;
area
.setPlaceholder(".trash\n_attachments\ntemplates/**")
.setValue(settings.excludePatterns.join("\n"))
.onChange(async (value) => {
this.plugin.settings.excludePatterns = parseExcludePatterns(value);
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Include attachments")
.setDesc(
"Also sync non-Markdown files such as images and PDFs.",
)
.addToggle((toggle) =>
toggle
.setValue(settings.includeAttachments)
.onChange(async (value) => {
this.plugin.settings.includeAttachments = value;
await this.plugin.saveSettings();
}),
);
if (Platform.isMobileApp) {
new Setting(containerEl)
.setName("Sync only on WiFi")
.setDesc(
"Pause automatic syncing on cellular. Note: only Android can detect network type — on iOS this toggle has no effect.",
)
.addToggle((toggle) =>
toggle
.setValue(settings.wifiOnly)
.onChange(async (value) => {
this.plugin.settings.wifiOnly = value;
await this.plugin.saveSettings();
}),
);
}
new Setting(containerEl)
.setName("Force sync")
.setDesc("Manually re-index the entire vault now.")
.addButton((btn) =>
btn.setButtonText("Update").onClick(async () => {
btn.setDisabled(true);
try {
await this.plugin.engine.maybeReconcile(true);
new Notice("Surfsense: re-sync requested.");
} catch (err) {
this.handleApiError(err);
} finally {
btn.setDisabled(false);
}
}),
);
new Setting(containerEl)
.addButton((btn) =>
btn
.setButtonText("View sync status")
.setCta()
.onClick(() => this.plugin.openStatusModal()),
)
.addButton((btn) =>
btn.setButtonText("Open releases").onClick(() => {
window.open(
"https://github.com/MODSetter/SurfSense/releases?q=obsidian",
"_blank",
);
}),
);
}
private renderConnectionHeading(containerEl: HTMLElement): void {
const heading = new Setting(containerEl).setName("Connection").setHeading();
heading.nameEl.addClass("surfsense-connection-heading");
const indicator = heading.nameEl.createSpan({
cls: "surfsense-connection-indicator",
});
const visual = this.getConnectionVisual();
indicator.addClass(`surfsense-connection-indicator--${visual.tone}`);
setIcon(indicator, visual.icon);
indicator.setAttr("aria-label", visual.label);
indicator.setAttr("title", visual.label);
}
private getConnectionVisual(): {
icon: string;
label: string;
tone: "ok" | "syncing" | "warn" | "err" | "muted";
} {
const settings = this.plugin.settings;
const kind = this.plugin.lastStatus.kind;
if (kind === "auth-error") {
return { icon: "lock", label: "Token invalid or expired", tone: "err" };
}
if (kind === "error") {
return { icon: "alert-circle", label: "Connection error", tone: "err" };
}
if (kind === "offline") {
return { icon: "wifi-off", label: "Server unreachable", tone: "warn" };
}
if (!settings.apiToken) {
return { icon: "circle", label: "Missing API token", tone: "muted" };
}
if (!settings.searchSpaceId) {
return { icon: "circle", label: "Pick a search space", tone: "muted" };
}
if (!settings.connectorId) {
return { icon: "circle", label: "Not connected yet", tone: "muted" };
}
if (kind === "syncing" || kind === "queued") {
return { icon: "refresh-ccw", label: "Connected and syncing", tone: "syncing" };
}
return { icon: "check-circle", label: "Connected", tone: "ok" };
}
private async refreshSearchSpaces(): Promise<void> {
this.loadingSpaces = true;
try {
this.searchSpaces = await this.plugin.api.listSearchSpaces();
} catch (err) {
this.handleApiError(err);
this.searchSpaces = [];
} finally {
this.loadingSpaces = false;
}
}
private renderFolderList(
containerEl: HTMLElement,
title: string,
desc: string,
current: string[],
write: (next: string[]) => void,
): void {
const setting = new Setting(containerEl).setName(title).setDesc(desc);
const persist = async (next: string[]): Promise<void> => {
const dedup = Array.from(new Set(next.map(normalizeFolder)));
write(dedup);
await this.plugin.saveSettings();
this.display();
};
setting.addButton((btn) =>
btn
.setButtonText("Add folder")
.setCta()
.onClick(() => {
new FolderSuggestModal(
this.app,
(picked) => {
void persist([...current, picked]);
},
current,
).open();
}),
);
for (const folder of current) {
new Setting(containerEl).setName(folder || "/").addExtraButton((btn) =>
btn
.setIcon("cross")
.setTooltip("Remove")
.onClick(() => {
void persist(current.filter((f) => f !== folder));
}),
);
}
}
private handleApiError(err: unknown): void {
if (err instanceof AuthError) {
new Notice(`SurfSense: ${err.message}`);
return;
}
new Notice(
`SurfSense: request failed — ${(err as Error).message ?? "unknown error"}`,
);
}
}