Newer
Older
smart-home-server / webclient-vue / src / stores / scanning.js
@Eugene Sukhodolskiy Eugene Sukhodolskiy 23 hours ago 1 KB Add script detail pages with scope grouping
import { defineStore } from "pinia";
import { devicesApi } from "../api/modules/devices";

export const useScanningStore = defineStore("scanning", {
  state: () => ({
    mode: "setup",
    devices: [],
    isLoading: false,
    error: null,
    _scanAbortController: null,
  }),

  getters: {
    total(state) {
      return state.devices.length;
    },
  },

  actions: {
    async scan() {
      this._scanAbortController?.abort();
      const controller = new AbortController();
      this._scanAbortController = controller;

      this.isLoading = true;
      this.error = null;

      const result =
        this.mode === "setup"
          ? await devicesApi.scanningSetup({ signal: controller.signal })
          : await devicesApi.scanningAll({ signal: controller.signal });

      this._scanAbortController = null;
      this.isLoading = false;

      if (!result.ok) {
        if (result.error?.type === "timeout") {
          return result;
        }
        this.error = result.error;
        return result;
      }

      this.devices = result.data?.data?.devices || [];
      return result;
    },

    setMode(mode) {
      this.mode = mode;
      this.devices = [];
      this.error = null;
    },

    async setupDevice(payload) {
      return devicesApi.setupNewDevice(payload);
    },
  },
});