2026-03-28 14:45:49 -07:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { ref } from "vue";
|
2026-04-09 00:25:21 +00:00
|
|
|
import { User } from "@/type/api";
|
2026-04-28 17:06:21 +00:00
|
|
|
import { SSEManager, type SSEMessageResource } from "@/SSEManager";
|
2026-04-02 01:07:55 +00:00
|
|
|
import { useSessionStore } from "@/store/session";
|
2026-03-28 14:45:49 -07:00
|
|
|
|
2026-03-31 15:10:32 +00:00
|
|
|
export const useUserStore = defineStore("users", () => {
|
2026-03-28 14:45:49 -07:00
|
|
|
// State
|
2026-04-02 19:36:49 +00:00
|
|
|
const _all = ref<User[] | null>(null);
|
2026-03-31 14:52:53 +00:00
|
|
|
const _byID = ref<Map<number, User>>(new Map());
|
2026-03-28 14:45:49 -07:00
|
|
|
const error = ref(null);
|
2026-04-02 01:07:55 +00:00
|
|
|
const loading = ref(false);
|
|
|
|
|
const ongoingFetch = ref<Promise<User[]> | null>(null);
|
2026-03-28 14:45:49 -07:00
|
|
|
|
|
|
|
|
// 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:user")) {
|
2026-03-28 14:45:49 -07:00
|
|
|
fetchAll();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Actions
|
2026-04-02 19:36:49 +00:00
|
|
|
function byID(id: number): User | null {
|
2026-03-28 18:06:14 -07:00
|
|
|
const result = _byID.value.get(id);
|
2026-04-02 19:36:49 +00:00
|
|
|
if (!result) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-28 18:06:14 -07:00
|
|
|
console.log("user", id, result);
|
|
|
|
|
return result;
|
2026-03-28 14:45:49 -07:00
|
|
|
}
|
2026-04-02 19:36:49 +00:00
|
|
|
async function byURI(uri: string): Promise<User | null> {
|
|
|
|
|
const all = await withAll();
|
|
|
|
|
const result = all.find((u: User) => u.uri == uri);
|
|
|
|
|
return result || null;
|
|
|
|
|
}
|
2026-03-28 18:06:14 -07:00
|
|
|
async function fetchAll(): Promise<User[]> {
|
2026-04-02 01:07:55 +00:00
|
|
|
const sessionStore = useSessionStore();
|
|
|
|
|
const session = await sessionStore.get();
|
2026-03-28 14:45:49 -07:00
|
|
|
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.user}?${params}`);
|
2026-03-28 14:45:49 -07:00
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
2026-04-02 01:07:55 +00:00
|
|
|
const users = await response.json();
|
2026-04-02 19:36:49 +00:00
|
|
|
_all.value = users;
|
2026-04-02 01:07:55 +00:00
|
|
|
for (const u of users) {
|
2026-03-28 14:45:49 -07:00
|
|
|
_byID.value.set(u.id, u);
|
|
|
|
|
}
|
2026-04-02 01:07:55 +00:00
|
|
|
return users;
|
2026-03-28 14:45:49 -07:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error loading users:", err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-02 01:07:55 +00:00
|
|
|
async function withAll(): Promise<User[]> {
|
2026-04-02 19:36:49 +00:00
|
|
|
if (_all.value != null) {
|
|
|
|
|
return _all.value;
|
2026-04-02 01:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ongoingFetch.value !== null) {
|
|
|
|
|
return ongoingFetch.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ongoingFetch.value = fetchAll().finally(() => {
|
|
|
|
|
ongoingFetch.value = null;
|
|
|
|
|
});
|
|
|
|
|
return ongoingFetch.value;
|
|
|
|
|
}
|
2026-03-31 14:52:53 +00:00
|
|
|
async function fetchOne(id: number) {
|
|
|
|
|
const session = useSessionStore();
|
|
|
|
|
if (session.urls == null) {
|
2026-03-28 14:45:49 -07:00
|
|
|
throw new Error("can't fetch without user URL data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
try {
|
2026-03-31 14:52:53 +00:00
|
|
|
const response = await fetch(`${session.urls.api.user}/${id}`);
|
2026-03-28 14:45:49 -07:00
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
_byID.value.set(data.id, data);
|
|
|
|
|
return data;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error loading users:", err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
// Actions
|
|
|
|
|
byID,
|
2026-04-02 19:36:49 +00:00
|
|
|
byURI,
|
2026-03-28 14:45:49 -07:00
|
|
|
fetchAll,
|
|
|
|
|
fetchOne,
|
2026-04-02 01:07:55 +00:00
|
|
|
withAll,
|
2026-03-28 14:45:49 -07:00
|
|
|
};
|
|
|
|
|
});
|