Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666import type { TextComponent } from "obsidian";import { ButtonComponent, Modal, Platform, SecretComponent, Setting,} from "obsidian";import { fetchReleaseVersions, parseRepositoryAddress, type ReleaseVersion, scrubRepositoryUrl,} from "src/features/githubUtils";import { GHRateLimitError, GitHubResponseError,} from "src/utils/GitHubAPIErrors";import { TokenValidator } from "src/utils/TokenValidator";import { createGitHubResourceLink } from "src/utils/utils";import type BetaPlugins from "../features/BetaPlugins";import type BratPlugin from "../main";import { existBetaPluginInList, updatePluginTokenName } from "../settings";import { toastMessage } from "../utils/notifications";import { promotionalLinks } from "./Promotional";import { VersionSuggestModal } from "./VersionSuggestModal";
/** * Add a beta plugin to the list of plugins being tracked and updated */export default class AddNewPluginModal extends Modal { plugin: BratPlugin; betaPlugins: BetaPlugins; address: string; openSettingsTabAfterwards: boolean; readonly updateVersion: boolean; version: string; versionSetting: Setting | null = null;
// Repository Setting repositoryAddressEl: TextComponent | null = null;
// Token Validation secretName: string; validToken: boolean | undefined; tokenInputEl: SecretComponent | null = null; validateButton: ButtonComponent | null = null; validator: TokenValidator | null = null;
// Plugin install action enableAfterInstall: boolean; addPluginButton: ButtonComponent | null = null; cancelButton: ButtonComponent | null = null;
constructor( plugin: BratPlugin, betaPlugins: BetaPlugins, openSettingsTabAfterwards = false, updateVersion = false, prefillRepo = "", prefillVersion = "", prefillSecretName = "", ) { super(plugin.app); this.plugin = plugin; this.betaPlugins = betaPlugins; this.address = prefillRepo; this.version = prefillVersion; this.secretName = prefillSecretName; this.openSettingsTabAfterwards = openSettingsTabAfterwards; this.updateVersion = updateVersion; this.enableAfterInstall = plugin.settings.enableAfterInstall; }
async submitForm(): Promise<void> { if (this.address === "") return; const scrubbedAddress = scrubRepositoryUrl(this.address);
// If it's an existing frozen version plugin, update it instead of checking for duplicates const existingFrozenPlugin = this.plugin.settings.pluginSubListFrozenVersion.find( (p) => p.repo === scrubbedAddress, ); if (existingFrozenPlugin) { const result = await this.betaPlugins.addPlugin( scrubbedAddress, false, false, false, this.version, true, // Force reinstall this.enableAfterInstall, this.secretName, ); if (result) { this.close(); }
// Reset modal if we don't close (i.e. because plugin could not be installed) this.cancelButton?.setDisabled(false); this.addPluginButton?.setDisabled(false); this.addPluginButton?.setButtonText("Add Plugin"); this.versionSetting?.setDisabled(false);
return; }
if (!this.version && existBetaPluginInList(this.plugin, scrubbedAddress)) { toastMessage( this.plugin, "This plugin is already in the list for beta testing", 10, ); return; }
const result = await this.betaPlugins.addPlugin( scrubbedAddress, false, false, false, this.version, false, this.enableAfterInstall, this.secretName, ); if (result) { this.close(); }
// Reset modal if we don't close (i.e. because plugin could not be installed) this.cancelButton?.setDisabled(false); this.addPluginButton?.setDisabled(false); this.addPluginButton?.setButtonText("Add Plugin"); this.versionSetting?.setDisabled(false); }
private updateVersionDropdown( settingEl: Setting, versions: ReleaseVersion[], selected = "", ): void { let selectedVersion: string;
settingEl.clear(); if ( versions.length > 0 && !selected && this.plugin.settings.selectLatestPluginVersionByDefault ) { selectedVersion = "latest"; this.version = "latest"; } else { selectedVersion = selected; }
const VERSION_THRESHOLD = 20;
// With fewer than 20 versions, or on mobile, use a dropdown if (versions.length < VERSION_THRESHOLD || Platform.isMobile) { // Use dropdown for fewer versions settingEl.addDropdown((dropdown) => { dropdown.addOption("", "Select a version"); dropdown.addOption("latest", "Latest version"); for (const version of versions) { dropdown.addOption( version.version, `${version.version} ${version.prerelease ? "(Prerelease)" : ""}`, ); } dropdown.onChange((value: string) => { this.version = value; this.addPluginButton?.setDisabled(this.version === ""); }); dropdown.setValue(selectedVersion);
dropdown.selectEl.addClass("brat-version-selector"); dropdown.selectEl.style.width = "100%"; }); } else { // Use suggest modal for many versions settingEl.addButton((button) => { button .setButtonText( selectedVersion === "latest" ? "Latest version" : selectedVersion || "Select a version...", ) .setClass("brat-version-selector") .setClass("button") .onClick(() => { const latest: ReleaseVersion = { version: "latest", prerelease: false, }; const suggestedVersions: ReleaseVersion[] = [latest, ...versions]; const modal = new VersionSuggestModal( this.app, this.address, suggestedVersions, selectedVersion, (version: string) => { this.version = version; button.setButtonText( version === "latest" ? "Latest version" : version || "Select a version...", ); this.addPluginButton?.setDisabled(this.version === ""); }, ); modal.open(); }); }); } }
onOpen(): void { const heading = this.contentEl.createEl("h4"); if (this.address) { heading.appendText("Change plugin version: "); heading.appendChild(createGitHubResourceLink(this.address)); } else { heading.setText("Repository for beta plugin:"); }
this.contentEl.createEl("form", {}, (formEl) => { formEl.addClass("brat-modal");
if (!this.address || !this.updateVersion) { const repoSetting = new Setting(formEl).setClass("repository-setting");
repoSetting.then((setting) => { // Show as input field for new plugins setting.addText((addressEl) => { this.repositoryAddressEl = addressEl;
addressEl.setPlaceholder( "Repository (example: https://github.com/user/repo or https://tangled.org/owner/repo)", ); addressEl.setValue(this.address); addressEl.onChange((value) => { this.address = scrubRepositoryUrl(value.trim()); if ( this.version !== "" && (!this.address || !this.isSupportedRepositoryMatch(this.address)) ) { // Disable version dropdown if version is set and address is empty if (this.versionSetting) { this.updateVersionDropdown(this.versionSetting, []); this.versionSetting.settingEl.classList.add( "disabled-setting", ); this.versionSetting.setDisabled(true); addressEl.inputEl.classList.remove("valid-repository"); addressEl.inputEl.classList.remove("invalid-repository"); } }
// If the repository matches a supported pattern, enable the "Add Plugin" if (!this.version) { if (this.isSupportedRepositoryMatch(this.address)) this.addPluginButton?.setDisabled(false); else this.addPluginButton?.setDisabled(true); } });
addressEl.inputEl.addEventListener( "keydown", async (e: KeyboardEvent) => { if (e.key === "Enter") { if ( this.address && ((this.updateVersion && this.version !== "") || !this.updateVersion) ) { e.preventDefault(); this.addPluginButton?.setDisabled(true); this.cancelButton?.setDisabled(true); this.versionSetting?.setDisabled(true); void this.submitForm(); }
// Populate version dropdown await this.updateRepositoryVersionInfo( this.version, validationStatusEl, ); } }, );
// Update version dropdown when input loses focus addressEl.inputEl.addEventListener("blur", async () => { await this.updateRepositoryVersionInfo( this.version, validationStatusEl, ); });
// FIXME setting.setDesc("Repository"); addressEl.inputEl.style.width = "100%"; }); }); } // Add validation status element (as a separate element) // TODO: Find better way to build the modal const validationStatusEl = formEl.createDiv("validation-status"); if (!this.address) validationStatusEl.setText( "Enter a repository address to validate it.", );
// Then add version dropdown this.versionSetting = new Setting(formEl) .setClass("version-setting") .setClass("disabled-setting"); this.updateVersionDropdown(this.versionSetting, [], this.version); this.versionSetting.setDisabled(true);
// Token setting section const tokenElement = formEl.createDiv("token-setting"); new Setting(tokenElement) .setName("Repository Token") .setDesc("Select a secret as token for GitHub repositories (optional)") .addComponent((el) => new SecretComponent(this.plugin.app, el) .setValue(this.secretName) .onChange(async (selectedSecretName: string | null) => { // User selected a different secret name (can be null when cleared) this.secretName = selectedSecretName?.trim() || ""; if (!this.secretName) { if ( this.address && existBetaPluginInList(this.plugin, this.address) ) { updatePluginTokenName(this.plugin, this.address, ""); toastMessage( this.plugin, `Token setting cleared for ${this.address}`, 3, ); } void this.updateRepositoryVersionInfo( this.version, validationStatusEl, ); return; } const tokenValue = this.secretName ? this.plugin.app.secretStorage.getSecret(this.secretName) : null; if (tokenValue) { this.validToken = await this.validator?.validateToken( tokenValue, this.address, ); if (!this.validToken) { this.validateButton?.setButtonText("Invalid"); this.validateButton?.setDisabled(false); } else { this.validateButton?.setButtonText("Valid"); this.validateButton?.setDisabled(true);
// Update version dropdown when API key changes if (this.address) { await this.updateRepositoryVersionInfo( this.version, validationStatusEl, );
// Update the secret name for this plugin in the settings if it already exists there if (existBetaPluginInList(this.plugin, this.address)) { updatePluginTokenName( this.plugin, this.address, this.secretName, ); toastMessage( this.plugin, `Token setting updated for ${this.address}`, 3, ); } } } } }), );
// Initialize validator this.validator = new TokenValidator();
// Validate the current token if we have a secret name if (this.secretName) { const tokenValue = this.plugin.app.secretStorage.getSecret( this.secretName, ); if (tokenValue) { // Validate asynchronously on initial load void this.validator ?.validateToken(tokenValue, this.address) .then((isValid) => { this.validToken = isValid; if (this.validToken) { this.validateButton?.setButtonText("Valid"); this.validateButton?.setDisabled(true); } }); } }
formEl.createDiv("modal-button-container", (buttonContainerEl) => { buttonContainerEl.createEl( "label", { cls: "mod-checkbox", }, (labelEl) => { const checkboxEl = labelEl.createEl("input", { attr: { tabindex: -1 }, type: "checkbox", }); checkboxEl.checked = this.enableAfterInstall; checkboxEl.addEventListener("click", () => { this.enableAfterInstall = checkboxEl.checked; }); labelEl.appendText("Enable after installing the plugin"); }, );
this.cancelButton = new ButtonComponent(buttonContainerEl) .setButtonText("Never mind") .setClass("mod-cancel") .onClick(() => { this.close(); });
this.addPluginButton = new ButtonComponent(buttonContainerEl) .setButtonText( this.updateVersion ? this.address ? "Change version" : "Add plugin" : "Add plugin", ) .setCta() .onClick(() => { if (this.address !== "") { if ( (this.updateVersion && this.version !== "") || !this.updateVersion ) { // Submit the form this.addPluginButton?.setDisabled(true); this.addPluginButton?.setButtonText("Installing …"); this.cancelButton?.setDisabled(true); this.versionSetting?.setDisabled(true); void this.submitForm(); } } });
// Disable "Add Plugin" if adding a frozen version only if (this.updateVersion || this.address === "") this.addPluginButton?.setDisabled(true); });
const newDiv = formEl.createDiv(); newDiv.style.borderTop = "1px solid #ccc"; newDiv.style.marginTop = "30px"; const byTfThacker = newDiv.createSpan(); byTfThacker.createEl("a", { href: "https://bit.ly/o42-twitter", text: "TFTHacker", }); byTfThacker.appendText(" and "); byTfThacker.createEl("a", { href: "https://github.com/johannrichard", text: "johannrichard", }); byTfThacker.style.fontStyle = "italic"; newDiv.appendChild(byTfThacker); promotionalLinks(newDiv, false);
// Prevent default form submission on Enter key and button clicks, and ensure buttons don't trigger form submission const buttons = formEl.querySelectorAll("button"); for (const button of Array.from(buttons)) { // Set type to prevent form submission button.setAttribute("type", "button"); }
// Invoked when "Submit" button is clicked. formEl.addEventListener("submit", (e: Event) => { e.preventDefault(); e.stopPropagation(); }); });
if (this.address) { // If we have a prefilled repo, trigger the version dropdown update window.setTimeout(async () => { await this.updateRepositoryVersionInfo(this.version); }, 100); } }
/** * Update the repository validation and version dropdown * @param selectedVersion - The version to select in the dropdown * @param validateInputEl - The address input element * @param validationStatusEl - The error element (used for errors, incl. GitHub Rate limit) * @returns {Promise<void>} */ private async updateRepositoryVersionInfo( selectedVersion = "", validationStatusEl?: HTMLElement, ) { const validateInputEl = this.repositoryAddressEl; if (this.plugin.settings.debuggingMode) { console.log( `[BRAT] Updating version dropdown for ${this.address} with selected version ${selectedVersion}`, ); }
if (!this.address) { validationStatusEl?.setText("Repository address is required."); validationStatusEl?.addClass("validation-status-error"); return; }
validationStatusEl?.setText("Validating repository address..."); validationStatusEl?.removeClass("validation-status-error");
if (this.versionSetting && this.updateVersion) { // Clear the version dropdown this.updateVersionDropdown(this.versionSetting, []); } const scrubbedAddress = scrubRepositoryUrl(this.address);
try { // Get the actual token value from SecretStorage let tokenToUse = ""; if (this.secretName) { const tokenValue = this.plugin.app.secretStorage.getSecret( this.secretName, ); if (tokenValue) { tokenToUse = tokenValue; } } else if (this.plugin.settings.globalTokenName) { const globalToken = this.plugin.app.secretStorage.getSecret( this.plugin.settings.globalTokenName, ); if (globalToken) { tokenToUse = globalToken; } }
const versions = await fetchReleaseVersions( scrubbedAddress, this.plugin.settings.debuggingMode, tokenToUse, );
if (versions && versions.length > 0) { // Add valid-repository class validateInputEl?.inputEl.classList.remove("invalid-repository"); validateInputEl?.inputEl.classList.add("valid-repository"); validationStatusEl?.setText("");
if (this.versionSetting) { this.versionSetting.settingEl.classList.remove("disabled-setting"); this.versionSetting.setDisabled(false); // Add new dropdown to existing version setting this.updateVersionDropdown( this.versionSetting, versions, selectedVersion, ); } } else { // Add invalid-repository class validateInputEl?.inputEl.classList.remove("valid-repository"); validateInputEl?.inputEl.classList.add("invalid-repository"); validationStatusEl?.setText( "Error: No releases found in this repository.", ); validationStatusEl?.addClass("validation-status-error");
this.versionSetting?.settingEl.classList.add("disabled-setting"); this.versionSetting?.setDisabled(true); this.addPluginButton?.setDisabled(true); } } catch (error: unknown) { if (error instanceof GHRateLimitError) { // Add invalid-repository class validateInputEl?.inputEl.classList.remove("valid-repository"); validateInputEl?.inputEl.classList.add("validation-error"); validationStatusEl?.setText( `GitHub API rate limit exceeded. Try again in ${error.getMinutesToReset()} minutes.`, );
if (this.versionSetting) { this.versionSetting.settingEl.classList.add("disabled-setting"); this.versionSetting.setDisabled(true); this.addPluginButton?.setDisabled(true); }
toastMessage( this.plugin, `${error.message} Consider adding a personal access token in BRAT settings for higher limits. See documentation for details.`, 20, (): void => { window.open( "https://github.com/TfTHacker/obsidian42-brat/blob/main/BRAT-DEVELOPER-GUIDE.md#github-api-rate-limits", ); }, );
// toastMessage(this.plugin, `GitHub API rate limit exceeded. Try again in ${error.getMinutesToReset()} minutes.`, 10); }
if (error instanceof GitHubResponseError) { const gitHubError = error as GitHubResponseError; switch (gitHubError.status) { case 404: validationStatusEl?.setText( "Repository not found. Check the address or provide a valid token for access to a private repository.", ); break; case 403: validationStatusEl?.setText( "Access denied. Check your personal access token.", ); break; default: validationStatusEl?.setText(`Error: ${gitHubError.message}`); break; }
// Disable relevant settings validationStatusEl?.addClass("validation-status-error"); this.versionSetting?.setDisabled(true); this.addPluginButton?.setDisabled(true);
toastMessage(this.plugin, `${gitHubError.message} `, 20); } } }
onClose(): void { if (this.openSettingsTabAfterwards) { // @ts-expect-error this.plugin.app.setting.open(); // @ts-expect-error this.plugin.app.setting.openTabById(this.plugin.APP_ID); } }
private isSupportedRepositoryMatch(address: string): boolean { return parseRepositoryAddress(address) !== null; }}