Various AT Protocol integrations with obsidian
at filter-collections-toggle 90 lines 2.5 kB view raw
1import { Modal } from "obsidian"; 2import type AtmospherePlugin from "../main"; 3import { getPublications } from "../lib"; 4import { SiteStandardPublication } from "@atcute/standard-site"; 5import type { ResourceUri } from "@atcute/lexicons"; 6 7export type PublicationSelection = { 8 uri: ResourceUri; 9 publication: SiteStandardPublication.Main; 10}; 11 12export class SelectPublicationModal extends Modal { 13 plugin: AtmospherePlugin; 14 onSelect: (selection: PublicationSelection) => void; 15 16 constructor(plugin: AtmospherePlugin, onSelect: (selection: PublicationSelection) => void) { 17 super(plugin.app); 18 this.plugin = plugin; 19 this.onSelect = onSelect; 20 } 21 22 async onOpen() { 23 const { contentEl } = this; 24 contentEl.empty(); 25 contentEl.addClass("atmosphere-modal"); 26 27 contentEl.createEl("h2", { text: "Select publication" }); 28 29 if (!this.plugin.client) { 30 contentEl.createEl("p", { text: "Not logged in", cls: "atmosphere-error" }); 31 return; 32 } 33 34 const loading = contentEl.createEl("p", { text: "Loading publications..." }); 35 36 try { 37 const response = await getPublications(this.plugin.client, this.plugin.settings.did!); 38 loading.remove(); 39 40 let pubs = response.records 41 42 if (pubs.length === 0) { 43 contentEl.createEl("p", { text: "No publications found. Create one first." }); 44 return; 45 } 46 47 // Create a list of publications 48 const listContainer = contentEl.createEl("div", { cls: "atmosphere-collection-list" }); 49 50 for (const pub of pubs) { 51 const item = listContainer.createEl("div", { cls: "atmosphere-collection-item" }); 52 53 const publication = pub.value; 54 55 const info = item.createEl("div", { cls: "atmosphere-collection-item-info" }); 56 info.createEl("div", { text: publication.name, cls: "atmosphere-collection-item-name" }); 57 58 if (publication.description) { 59 info.createEl("div", { 60 text: publication.description, 61 cls: "atmosphere-collection-item-desc" 62 }); 63 } 64 65 info.createEl("div", { 66 text: publication.url, 67 cls: "atmosphere-collection-item-desc" 68 }); 69 70 item.addEventListener("click", () => { 71 this.onSelect({ 72 uri: pub.uri, 73 publication, 74 }); 75 this.close(); 76 }); 77 } 78 79 } catch (error) { 80 loading.remove(); 81 const message = error instanceof Error ? error.message : String(error); 82 contentEl.createEl("p", { text: `Error: ${message}`, cls: "atmosphere-error" }); 83 } 84 } 85 86 onClose() { 87 const { contentEl } = this; 88 contentEl.empty(); 89 } 90}