Refactor data loading in server routes to use alias parameter
Publish telemt-api gateway Docker image / test (push) Successful in 25s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m14s

- Replaced `onMount` with `$effect` for loading data in multiple server route components, improving reactivity and ensuring data is fetched based on the current alias.
- Introduced a new `loadFor` function to handle data fetching with the alias parameter, enhancing code clarity and maintainability.
- Updated all relevant components to utilize the new loading mechanism, ensuring consistent behavior across the application.
This commit is contained in:
Denozordec
2026-03-30 12:01:25 +07:00
parent 0885597ac0
commit 42cb85308e
6 changed files with 87 additions and 29 deletions
+15 -6
View File
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import {
ApiError,
fetchStatsSummary,
@@ -22,14 +21,14 @@
let summary = $state<StatsSummaryData | null>(null);
let sys = $state<SystemInfoData | null>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const [h, s, i] = await Promise.all([
fetchTelemt<HealthData>(alias, 'health'),
fetchStatsSummary(alias),
fetchTelemt<SystemInfoData>(alias, 'system/info')
fetchTelemt<HealthData>(a, 'health'),
fetchStatsSummary(a),
fetchTelemt<SystemInfoData>(a, 'system/info')
]);
health = h.data;
summary = s.data;
@@ -41,7 +40,17 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
</script>
<div class="mb-6 flex items-center justify-between gap-4">
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { ApiError, fetchTelemt } from '$lib/api/client.js';
import type { DcStatusData, RuntimeGatesData } from '$lib/api/telemt-v1.js';
import * as Card from '$lib/components/ui/card/index.js';
@@ -18,14 +17,14 @@
let dcs = $state<DcStatusData | null>(null);
let pool = $state<unknown>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const [g, d, p] = await Promise.all([
fetchTelemt<RuntimeGatesData>(alias, 'runtime/gates'),
fetchTelemt<DcStatusData>(alias, 'stats/dcs'),
fetchTelemt<unknown>(alias, 'runtime/me_pool_state')
fetchTelemt<RuntimeGatesData>(a, 'runtime/gates'),
fetchTelemt<DcStatusData>(a, 'stats/dcs'),
fetchTelemt<unknown>(a, 'runtime/me_pool_state')
]);
gates = g.data;
dcs = d.data;
@@ -37,7 +36,17 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
function onOff(v: boolean | undefined) {
if (v === undefined) return '—';
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { ApiError, fetchTelemt } from '$lib/api/client.js';
import * as Card from '$lib/components/ui/card/index.js';
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
@@ -13,11 +12,11 @@
let err = $state<string | null>(null);
let data = $state<Record<string, unknown> | null>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const r = await fetchTelemt<Record<string, unknown>>(alias, 'security/posture');
const r = await fetchTelemt<Record<string, unknown>>(a, 'security/posture');
data = r.data ?? null;
} catch (e) {
err = e instanceof ApiError ? e.message : String(e);
@@ -26,7 +25,17 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
</script>
<div class="mb-6 flex items-center justify-between">
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { ApiError, fetchTelemt } from '$lib/api/client.js';
import type { SystemInfoData } from '$lib/api/telemt-v1.js';
import * as Card from '$lib/components/ui/card/index.js';
@@ -14,11 +13,11 @@
let err = $state<string | null>(null);
let sys = $state<SystemInfoData | null>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const r = await fetchTelemt<SystemInfoData>(alias, 'system/info');
const r = await fetchTelemt<SystemInfoData>(a, 'system/info');
sys = r.data;
} catch (e) {
err = e instanceof ApiError ? e.message : String(e);
@@ -27,7 +26,17 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
</script>
<div class="mb-6 flex items-center justify-between">
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { ApiError, fetchTelemt } from '$lib/api/client.js';
import * as Card from '$lib/components/ui/card/index.js';
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
@@ -13,11 +12,11 @@
let err = $state<string | null>(null);
let data = $state<Record<string, unknown> | null>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const r = await fetchTelemt<Record<string, unknown>>(alias, 'stats/upstreams');
const r = await fetchTelemt<Record<string, unknown>>(a, 'stats/upstreams');
data = r.data ?? null;
} catch (e) {
err = e instanceof ApiError ? e.message : String(e);
@@ -26,7 +25,17 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
</script>
<div class="mb-6 flex items-center justify-between">
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { onMount } from 'svelte';
import {
ApiError,
createUser,
@@ -49,13 +48,13 @@
let busy = $state(false);
let formErr = $state<string | null>(null);
async function load() {
async function loadFor(a: string) {
loading = true;
err = null;
try {
const [res, h] = await Promise.all([
fetchUsersList(alias),
fetchTelemt<HealthData>(alias, 'health')
fetchUsersList(a),
fetchTelemt<HealthData>(a, 'health')
]);
users = res.data ?? [];
configRevision = res.revision;
@@ -67,7 +66,21 @@
}
}
onMount(load);
$effect(() => {
const a = alias;
if (!a) return;
createOpen = false;
editUser = null;
deleteUserRow = null;
formErr = null;
void loadFor(a);
});
async function load() {
const a = alias;
if (!a) return;
await loadFor(a);
}
function openEdit(u: UserInfo) {
editUser = u;