Refactor data loading in server routes to use alias parameter
- 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:
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import {
|
import {
|
||||||
ApiError,
|
ApiError,
|
||||||
fetchStatsSummary,
|
fetchStatsSummary,
|
||||||
@@ -22,14 +21,14 @@
|
|||||||
let summary = $state<StatsSummaryData | null>(null);
|
let summary = $state<StatsSummaryData | null>(null);
|
||||||
let sys = $state<SystemInfoData | null>(null);
|
let sys = $state<SystemInfoData | null>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
try {
|
||||||
const [h, s, i] = await Promise.all([
|
const [h, s, i] = await Promise.all([
|
||||||
fetchTelemt<HealthData>(alias, 'health'),
|
fetchTelemt<HealthData>(a, 'health'),
|
||||||
fetchStatsSummary(alias),
|
fetchStatsSummary(a),
|
||||||
fetchTelemt<SystemInfoData>(alias, 'system/info')
|
fetchTelemt<SystemInfoData>(a, 'system/info')
|
||||||
]);
|
]);
|
||||||
health = h.data;
|
health = h.data;
|
||||||
summary = s.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>
|
</script>
|
||||||
|
|
||||||
<div class="mb-6 flex items-center justify-between gap-4">
|
<div class="mb-6 flex items-center justify-between gap-4">
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
||||||
import type { DcStatusData, RuntimeGatesData } from '$lib/api/telemt-v1.js';
|
import type { DcStatusData, RuntimeGatesData } from '$lib/api/telemt-v1.js';
|
||||||
import * as Card from '$lib/components/ui/card/index.js';
|
import * as Card from '$lib/components/ui/card/index.js';
|
||||||
@@ -18,14 +17,14 @@
|
|||||||
let dcs = $state<DcStatusData | null>(null);
|
let dcs = $state<DcStatusData | null>(null);
|
||||||
let pool = $state<unknown>(null);
|
let pool = $state<unknown>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
try {
|
||||||
const [g, d, p] = await Promise.all([
|
const [g, d, p] = await Promise.all([
|
||||||
fetchTelemt<RuntimeGatesData>(alias, 'runtime/gates'),
|
fetchTelemt<RuntimeGatesData>(a, 'runtime/gates'),
|
||||||
fetchTelemt<DcStatusData>(alias, 'stats/dcs'),
|
fetchTelemt<DcStatusData>(a, 'stats/dcs'),
|
||||||
fetchTelemt<unknown>(alias, 'runtime/me_pool_state')
|
fetchTelemt<unknown>(a, 'runtime/me_pool_state')
|
||||||
]);
|
]);
|
||||||
gates = g.data;
|
gates = g.data;
|
||||||
dcs = d.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) {
|
function onOff(v: boolean | undefined) {
|
||||||
if (v === undefined) return '—';
|
if (v === undefined) return '—';
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
||||||
import * as Card from '$lib/components/ui/card/index.js';
|
import * as Card from '$lib/components/ui/card/index.js';
|
||||||
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
|
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
|
||||||
@@ -13,11 +12,11 @@
|
|||||||
let err = $state<string | null>(null);
|
let err = $state<string | null>(null);
|
||||||
let data = $state<Record<string, unknown> | null>(null);
|
let data = $state<Record<string, unknown> | null>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
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;
|
data = r.data ?? null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e instanceof ApiError ? e.message : String(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>
|
</script>
|
||||||
|
|
||||||
<div class="mb-6 flex items-center justify-between">
|
<div class="mb-6 flex items-center justify-between">
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
||||||
import type { SystemInfoData } from '$lib/api/telemt-v1.js';
|
import type { SystemInfoData } from '$lib/api/telemt-v1.js';
|
||||||
import * as Card from '$lib/components/ui/card/index.js';
|
import * as Card from '$lib/components/ui/card/index.js';
|
||||||
@@ -14,11 +13,11 @@
|
|||||||
let err = $state<string | null>(null);
|
let err = $state<string | null>(null);
|
||||||
let sys = $state<SystemInfoData | null>(null);
|
let sys = $state<SystemInfoData | null>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
try {
|
||||||
const r = await fetchTelemt<SystemInfoData>(alias, 'system/info');
|
const r = await fetchTelemt<SystemInfoData>(a, 'system/info');
|
||||||
sys = r.data;
|
sys = r.data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e instanceof ApiError ? e.message : String(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>
|
</script>
|
||||||
|
|
||||||
<div class="mb-6 flex items-center justify-between">
|
<div class="mb-6 flex items-center justify-between">
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
import { ApiError, fetchTelemt } from '$lib/api/client.js';
|
||||||
import * as Card from '$lib/components/ui/card/index.js';
|
import * as Card from '$lib/components/ui/card/index.js';
|
||||||
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
|
import { Alert, AlertDescription, AlertTitle } from '$lib/components/ui/alert/index.js';
|
||||||
@@ -13,11 +12,11 @@
|
|||||||
let err = $state<string | null>(null);
|
let err = $state<string | null>(null);
|
||||||
let data = $state<Record<string, unknown> | null>(null);
|
let data = $state<Record<string, unknown> | null>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
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;
|
data = r.data ?? null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e instanceof ApiError ? e.message : String(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>
|
</script>
|
||||||
|
|
||||||
<div class="mb-6 flex items-center justify-between">
|
<div class="mb-6 flex items-center justify-between">
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import {
|
import {
|
||||||
ApiError,
|
ApiError,
|
||||||
createUser,
|
createUser,
|
||||||
@@ -49,13 +48,13 @@
|
|||||||
let busy = $state(false);
|
let busy = $state(false);
|
||||||
let formErr = $state<string | null>(null);
|
let formErr = $state<string | null>(null);
|
||||||
|
|
||||||
async function load() {
|
async function loadFor(a: string) {
|
||||||
loading = true;
|
loading = true;
|
||||||
err = null;
|
err = null;
|
||||||
try {
|
try {
|
||||||
const [res, h] = await Promise.all([
|
const [res, h] = await Promise.all([
|
||||||
fetchUsersList(alias),
|
fetchUsersList(a),
|
||||||
fetchTelemt<HealthData>(alias, 'health')
|
fetchTelemt<HealthData>(a, 'health')
|
||||||
]);
|
]);
|
||||||
users = res.data ?? [];
|
users = res.data ?? [];
|
||||||
configRevision = res.revision;
|
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) {
|
function openEdit(u: UserInfo) {
|
||||||
editUser = u;
|
editUser = u;
|
||||||
|
|||||||
Reference in New Issue
Block a user