2026-03-22 09:54:02 +00:00
|
|
|
import { defineStore } from "pinia";
|
2026-04-08 17:49:32 +00:00
|
|
|
import { ref } from "vue";
|
2026-04-09 00:25:21 +00:00
|
|
|
import { Signal } from "@/type/api";
|
2026-04-28 17:06:21 +00:00
|
|
|
import { SSEManager, type SSEMessageResource } from "@/SSEManager";
|
2026-03-31 14:52:53 +00:00
|
|
|
import { useSessionStore } from "@/store/session";
|
2026-03-22 09:54:02 +00:00
|
|
|
|
|
|
|
|
export const useSignalStore = defineStore("signal", () => {
|
|
|
|
|
// State
|
|
|
|
|
const all = ref<Signal[] | null>(null);
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
const error = ref(null);
|
|
|
|
|
|
|
|
|
|
// Subscription
|
2026-04-28 17:06:21 +00:00
|
|
|
SSEManager.subscribe((msg: SSEMessageResource) => {
|
2026-04-02 21:31:31 +00:00
|
|
|
if (msg.resource.startsWith("sync:signal")) {
|
2026-03-22 09:54:02 +00:00
|
|
|
fetchAll();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Actions
|
|
|
|
|
async function fetchAll() {
|
2026-03-31 14:52:53 +00:00
|
|
|
const session = useSessionStore();
|
|
|
|
|
if (session.urls == null) {
|
2026-03-22 09:54:02 +00:00
|
|
|
throw new Error("can't fetch without user URL data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
try {
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.append("sort", "-created");
|
|
|
|
|
//if (typeFilter.value) params.append("type", typeFilter.value);
|
|
|
|
|
|
2026-03-31 14:52:53 +00:00
|
|
|
const response = await fetch(`${session.urls.api.signal}?${params}`);
|
2026-03-22 09:54:02 +00:00
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
all.value = data.signals;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error loading signals:", err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
// State
|
|
|
|
|
all,
|
|
|
|
|
// Actions
|
|
|
|
|
fetchAll,
|
|
|
|
|
};
|
|
|
|
|
});
|