useNodeList.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Lightweight composable that fetches the node list once on mount and
  2. // exposes id→name + id→online lookups. Used by the Inbounds page so it
  3. // can render a Node selector and a Node column without pulling the
  4. // full pages/nodes/useNodes.js (which polls and owns CRUD state).
  5. import { onMounted, ref, computed } from 'vue';
  6. import { HttpUtil } from '@/utils';
  7. export function useNodeList() {
  8. const nodes = ref([]);
  9. const fetched = ref(false);
  10. async function refresh() {
  11. const msg = await HttpUtil.get('/panel/api/nodes/list');
  12. if (msg?.success) {
  13. nodes.value = Array.isArray(msg.obj) ? msg.obj : [];
  14. }
  15. fetched.value = true;
  16. }
  17. // Indexed by id for O(1) UI lookups (Node column on N-row tables).
  18. const byId = computed(() => {
  19. const m = new Map();
  20. for (const n of nodes.value) m.set(n.id, n);
  21. return m;
  22. });
  23. function nameFor(id) {
  24. if (id == null) return null;
  25. return byId.value.get(id)?.name || null;
  26. }
  27. function isOnline(id) {
  28. if (id == null) return true;
  29. const n = byId.value.get(id);
  30. return n != null && n.enable && n.status === 'online';
  31. }
  32. const hasActive = computed(() => nodes.value.some((n) => n.enable));
  33. onMounted(refresh);
  34. return { nodes, fetched, refresh, byId, nameFor, isOnline, hasActive };
  35. }