File size: 1,729 Bytes
430354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { createStore } from "/js/AlpineStore.js";
import * as api from "/js/api.js";
import { toastFrontendError } from "/components/notifications/notification-store.js";
import { store as pluginSettingsStore } from "/components/plugins/plugin-settings-store.js";

const model = {
  tab: "",
  plugins: [],
  loading: false,

  resolveTab(element) {
    const host =
      element?.closest("x-component")
      || element?.parentElement?.closest("x-component");
    return host?.getAttribute("data-tab") || "";
  },

  async init(element) {
    this.tab = this.resolveTab(element);
    await this.load();
  },

  cleanup() {
    this.tab = "";
    this.plugins = [];
    this.loading = false;
  },

  async load() {
    if (!this.tab) {
      this.plugins = [];
      return;
    }

    this.loading = true;
    try {
      const response = await api.callJsonApi("plugins_list", {
        filter: { custom: true, builtin: true },
      });
      const plugins = Array.isArray(response?.plugins) ? response.plugins : [];
      this.plugins = plugins.filter((plugin) => {
        const sections = Array.isArray(plugin?.settings_sections)
          ? plugin.settings_sections
          : [];
        return plugin?.has_config_screen && sections.includes(this.tab);
      });
    } catch {
      this.plugins = [];
    } finally {
      this.loading = false;
    }
  },

  async openPluginConfig(plugin) {
    if (!plugin?.name) return;
    try {
      await pluginSettingsStore.openConfig(plugin.name);
    } catch (e) {
      const message = e instanceof Error ? e.message : String(e);
      void toastFrontendError(message, "Plugin Settings");
    }
  },
};

export const store = createStore("pluginsSubsectionPrototype", model);