mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
Resolve the selected group from /admin/groups/{groupId}, validate console paths, and use browser history for back navigation. Keep Groups active and let an active mobile-tab tap return to the list. Scope pending route loads to their group and retain the count beside long headings.
75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import type { Route } from "~/types";
|
|
|
|
export function useGroupRoutesApi() {
|
|
const { needLogin } = useAuthState();
|
|
const routes = ref<Route[]>([]);
|
|
const loading = ref(false);
|
|
const error = ref("");
|
|
/** Group whose routes are shown; responses for any other group are dropped. */
|
|
const groupId = ref<string | null>(null);
|
|
/** Group whose routes were last loaded (or saved) successfully. */
|
|
const loadedId = ref<string | null>(null);
|
|
// Bumped by every load/clear, so only the latest request may land.
|
|
let request = 0;
|
|
|
|
/**
|
|
* The shown routes are the group's real routes. Saving replaces a group's
|
|
* whole route list, so a write is only safe from here — never from the
|
|
* empty list of a load still running or one that failed.
|
|
*/
|
|
const ready = computed(() => !!groupId.value && loadedId.value === groupId.value && !error.value);
|
|
|
|
async function load(id: string): Promise<void> {
|
|
const mine = ++request;
|
|
if (groupId.value !== id) {
|
|
routes.value = [];
|
|
loadedId.value = null;
|
|
}
|
|
groupId.value = id;
|
|
loading.value = true;
|
|
error.value = "";
|
|
needLogin.value = false;
|
|
try {
|
|
const data = await apiFetch<{ routes?: Route[] }>(
|
|
`/admin/api/groups/${encodeURIComponent(id)}/routes`,
|
|
);
|
|
if (mine !== request) return;
|
|
routes.value = data.routes ?? [];
|
|
loadedId.value = id;
|
|
} catch (err) {
|
|
if (mine === request && !needLogin.value)
|
|
error.value = err instanceof Error ? err.message : String(err);
|
|
} finally {
|
|
if (mine === request) loading.value = false;
|
|
}
|
|
}
|
|
|
|
/** Forget the shown group (its page was left); late responses are ignored. */
|
|
function clear(): void {
|
|
request++;
|
|
groupId.value = null;
|
|
loadedId.value = null;
|
|
routes.value = [];
|
|
loading.value = false;
|
|
error.value = "";
|
|
}
|
|
|
|
async function save(id: string, next: Route[]): Promise<void> {
|
|
await apiFetch(`/admin/api/groups/${encodeURIComponent(id)}/routes`, {
|
|
method: "PUT",
|
|
body: JSON.stringify({ routes: next }),
|
|
});
|
|
// The page may have moved on to another group while the request ran.
|
|
if (groupId.value !== id) return;
|
|
// What was just written is the newest state: a load of this group that
|
|
// started earlier may return the old routes, so it must not land.
|
|
request++;
|
|
loading.value = false;
|
|
error.value = "";
|
|
routes.value = next;
|
|
loadedId.value = id;
|
|
}
|
|
|
|
return { routes, loading, needLogin, error, groupId, ready, load, clear, save };
|
|
}
|