| 1 | import { createStore } from "/js/AlpineStore.js"; |
| 2 | import * as api from "/js/api.js"; |
| 3 | import { toastFrontendError } from "/components/notifications/notification-store.js"; |
| 4 | import { store as pluginSettingsStore } from "/components/plugins/plugin-settings-store.js"; |
| 5 | |
| 6 | const model = { |
| 7 | tab: "", |
| 8 | plugins: [], |
| 9 | loading: false, |
| 10 | |
| 11 | resolveTab(element) { |
| 12 | const host = |
| 13 | element?.closest("x-component") |
| 14 | || element?.parentElement?.closest("x-component"); |
| 15 | return host?.getAttribute("data-tab") || ""; |
| 16 | }, |
| 17 | |
| 18 | async init(element) { |
| 19 | this.tab = this.resolveTab(element); |
| 20 | await this.load(); |
| 21 | }, |
| 22 | |
| 23 | cleanup() { |
| 24 | this.tab = ""; |
| 25 | this.plugins = []; |
| 26 | this.loading = false; |
| 27 | }, |
| 28 | |
| 29 | async load() { |
| 30 | if (!this.tab) { |
| 31 | this.plugins = []; |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | this.loading = true; |
| 36 | try { |
| 37 | const response = await api.callJsonApi("plugins_list", { |
| 38 | filter: { custom: true, builtin: true }, |
| 39 | }); |
| 40 | const plugins = Array.isArray(response?.plugins) ? response.plugins : []; |
| 41 | this.plugins = plugins.filter((plugin) => { |
| 42 | const sections = Array.isArray(plugin?.settings_sections) |
| 43 | ? plugin.settings_sections |
| 44 | : []; |
| 45 | return plugin?.has_config_screen && sections.includes(this.tab); |
| 46 | }); |
| 47 | } catch { |
| 48 | this.plugins = []; |
| 49 | } finally { |
| 50 | this.loading = false; |
| 51 | } |
| 52 | }, |
| 53 | |
| 54 | async openPluginConfig(plugin) { |
| 55 | if (!plugin?.name) return; |
| 56 | try { |
| 57 | await pluginSettingsStore.openConfig(plugin.name); |
| 58 | } catch (e) { |
| 59 | const message = e instanceof Error ? e.message : String(e); |
| 60 | void toastFrontendError(message, "Plugin Settings"); |
| 61 | } |
| 62 | }, |
| 63 | }; |
| 64 | |
| 65 | export const store = createStore("pluginsSubsectionPrototype", model); |