Skip to content

Commit 29269aa

Browse files
committed
Further refactoring of the bulk / folder import logic
1 parent 4798f11 commit 29269aa

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { SearchModalOptions } from './utils/ModalHelper';
2626
import { ModalHelper, ModalResultCode } from './utils/ModalHelper';
2727
import type { CreateNoteOptions } from './utils/Utils';
2828
import { dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString, unCamelCase, hasTemplaterPlugin, useTemplaterPluginInFile } from './utils/Utils';
29+
import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod';
2930

3031
export type Metadata = Record<string, unknown>;
3132

@@ -646,7 +647,7 @@ export default class MediaDbPlugin extends Plugin {
646647
if (!lookupValue || typeof lookupValue !== 'string') {
647648
erroredFiles.push({ filePath: file.path, error: `metadata field '${fieldName}' not found, empty, or not a string` });
648649
continue;
649-
} else if (lookupMethod == 'id') {
650+
} else if (lookupMethod === BulkImportLookupMethod.ID) {
650651
try {
651652
const model = await this.apiManager.queryDetailedInfoById(lookupValue, selectedAPI);
652653
if (model) {
@@ -658,7 +659,7 @@ export default class MediaDbPlugin extends Plugin {
658659
erroredFiles.push({ filePath: file.path, error: `${e}` });
659660
continue;
660661
}
661-
} else if (lookupMethod == 'title') {
662+
} else if (lookupMethod === BulkImportLookupMethod.TITLE) {
662663
let results: MediaTypeModel[] = [];
663664
try {
664665
results = await this.apiManager.query(lookupValue, [selectedAPI]);
@@ -705,6 +706,9 @@ export default class MediaDbPlugin extends Plugin {
705706
await this.createMediaDbNotes(detailedResults, appendContent ? file : undefined);
706707

707708
selectModal.close();
709+
} else {
710+
erroredFiles.push({ filePath: file.path, error: `invalid lookup type` });
711+
continue;
708712
}
709713
}
710714
}

src/modals/MediaDbFolderImportModal.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { App, ButtonComponent } from 'obsidian';
22
import { DropdownComponent, Modal, Setting, TextComponent, ToggleComponent } from 'obsidian';
33
import type MediaDbPlugin from '../main';
44
import type { APIModel } from 'src/api/APIModel';
5+
import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod';
56

67
export class MediaDbFolderImportModal extends Modal {
78
plugin: MediaDbPlugin;
@@ -17,7 +18,7 @@ export class MediaDbFolderImportModal extends Modal {
1718
this.plugin = plugin;
1819
this.onSubmit = onSubmit;
1920
this.selectedApi = plugin.apiManager.apis[0].apiName;
20-
this.lookupMethod = '';
21+
this.lookupMethod = BulkImportLookupMethod.TITLE;
2122
this.fieldName = '';
2223
this.appendContent = false;
2324
}
@@ -32,14 +33,16 @@ export class MediaDbFolderImportModal extends Modal {
3233

3334
contentEl.createEl('h2', { text: 'Import folder as Media DB entries' });
3435

35-
const onAPIChange = (value: string) => {
36-
this.selectedApi = value;
37-
};
38-
const apiOptions = this.plugin.apiManager.apis.map((api: APIModel) => {
39-
return { value: api.apiName, display: api.apiName };
40-
});
41-
42-
this.createDropdownEl(contentEl, 'API to search', onAPIChange, apiOptions);
36+
this.createDropdownEl(
37+
contentEl,
38+
'API to search',
39+
(value: string) => {
40+
this.selectedApi = value;
41+
},
42+
this.plugin.apiManager.apis.map((api: APIModel) => {
43+
return { value: api.apiName, display: api.apiName };
44+
}),
45+
);
4346

4447
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
4548
contentEl.createEl('h3', { text: 'Append note content to Media DB entry?' });
@@ -64,14 +67,17 @@ export class MediaDbFolderImportModal extends Modal {
6467
text: 'Choose whether to search the API by title (can return multiple results) or lookup directly using an ID (returns at most one result), and specify the name of the frontmatter property which contains the title or ID of the media.',
6568
});
6669

67-
const onlookupMethodChange = (value: string) => {
68-
this.lookupMethod = value;
69-
};
70-
const lookupMethodOptions = [
71-
{ value: 'title', display: 'Title' },
72-
{ value: 'id', display: 'ID' },
73-
];
74-
this.createDropdownEl(contentEl, 'Lookup media by', onlookupMethodChange, lookupMethodOptions);
70+
this.createDropdownEl(
71+
contentEl,
72+
'Lookup media by',
73+
(value: string) => {
74+
this.lookupMethod = value;
75+
},
76+
[
77+
{ value: BulkImportLookupMethod.TITLE, display: 'Title' },
78+
{ value: BulkImportLookupMethod.ID, display: 'ID' },
79+
],
80+
);
7581

7682
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
7783

@@ -108,7 +114,7 @@ export class MediaDbFolderImportModal extends Modal {
108114
});
109115
}
110116

111-
createDropdownEl(parentEl: HTMLElement, label: string, onChange: { (value: string): void }, options: { value: string; display: string }[]): void {
117+
createDropdownEl(parentEl: HTMLElement, label: string, onChange: (value: string) => void, options: { value: string; display: string }[]): void {
112118
const wrapperEl = parentEl.createEl('div', { cls: 'media-db-plugin-list-wrapper' });
113119
const labelWrapperEl = wrapperEl.createEl('div', { cls: 'media-db-plugin-list-text-wrapper' });
114120
labelWrapperEl.createEl('span', { text: label, cls: 'media-db-plugin-list-text' });
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum BulkImportLookupMethod {
2+
ID = 'id',
3+
TITLE = 'title',
4+
}

0 commit comments

Comments
 (0)