generated from tpl/obsidian-sample-plugin
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import type { Goodreads, SearchResult } from "@data-sources/Goodreads";
|
|
import { GoodreadsSearchModal, GoodreadsSearchSuggestModal } from "@ui/modals";
|
|
import { App, Notice } from "obsidian";
|
|
import { Command } from "./Command";
|
|
import type { Book } from "@src/types";
|
|
|
|
export class SearchGoodreadsCommand extends Command {
|
|
constructor(
|
|
private readonly app: App,
|
|
private readonly goodreads: Goodreads,
|
|
private readonly cb: (book: Book) => void
|
|
) {
|
|
super("search-goodreads", "Search Goodreads");
|
|
}
|
|
|
|
async callback() {
|
|
let results: SearchResult[];
|
|
try {
|
|
// eslint-disable-next-line prefer-const
|
|
results = await GoodreadsSearchModal.createAndOpen(
|
|
this.app,
|
|
this.goodreads
|
|
);
|
|
} catch (error) {
|
|
console.error("Failed to search Goodreads:", error);
|
|
new Notice(
|
|
"Failed to search Goodreads. Check console for details."
|
|
);
|
|
return;
|
|
}
|
|
|
|
const selectedResult = await GoodreadsSearchSuggestModal.createAndOpen(
|
|
this.app,
|
|
results
|
|
);
|
|
if (!selectedResult) {
|
|
new Notice("No book selected.");
|
|
return;
|
|
}
|
|
|
|
let book: Book;
|
|
try {
|
|
// eslint-disable-next-line prefer-const
|
|
book = await this.goodreads.getBookByLegacyId(
|
|
selectedResult.legacyId
|
|
);
|
|
} catch (error) {
|
|
console.error("Failed to get book:", error);
|
|
new Notice("Failed to get book. Check console for details.");
|
|
return;
|
|
}
|
|
|
|
this.cb(book);
|
|
}
|
|
}
|