mirror of
https://github.com/luckjiawei/podux.git
synced 2026-11-30 00:56:47 +00:00
⚡️ Performance optimization.
This commit is contained in:
1 parent
c119ae3d2f
commit
d29e37101c
5 files changed
+67
-37
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Flex, Text } from "@radix-ui/themes";
|
||||
import { motion, AnimatePresence } from "framer-motion";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@@ -152,25 +152,31 @@ export function LatencyChart({ serverId }: LatencyChartProps) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [animKey, setAnimKey] = useState(0);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const res = await apiGet(`/api/servers/probe-history?serverId=${serverId}`);
|
||||
if (!res.ok) return;
|
||||
const points: ProbePoint[] = await res.json();
|
||||
setData(toChartPoints(points));
|
||||
setAnimKey((k) => k + 1);
|
||||
} catch {
|
||||
// silent
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [serverId]);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const res = await apiGet(`/api/servers/probe-history?serverId=${serverId}`);
|
||||
if (!res.ok || cancelled) return;
|
||||
const points: ProbePoint[] = await res.json();
|
||||
if (cancelled) return;
|
||||
setData(toChartPoints(points));
|
||||
setAnimKey((k) => k + 1);
|
||||
} catch {
|
||||
// silent
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
load();
|
||||
const interval = setInterval(load, 30_000);
|
||||
return () => clearInterval(interval);
|
||||
}, [load]);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [serverId]);
|
||||
|
||||
const isEmpty = !loading && data.length === 0;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Flex, Text, Tooltip, Badge } from "@radix-ui/themes";
|
||||
import { motion } from "framer-motion";
|
||||
import { Icon } from "@iconify/react";
|
||||
@@ -80,25 +80,31 @@ export function ProbeHistory({ serverId }: ProbeHistoryProps) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [animKey, setAnimKey] = useState(0);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const res = await apiGet(`/api/servers/probe-history?serverId=${serverId}`);
|
||||
if (!res.ok) return;
|
||||
const points: ProbePoint[] = await res.json();
|
||||
setBuckets(buildBuckets(points));
|
||||
setAnimKey((k) => k + 1);
|
||||
} catch {
|
||||
// silent
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [serverId]);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const res = await apiGet(`/api/servers/probe-history?serverId=${serverId}`);
|
||||
if (!res.ok || cancelled) return;
|
||||
const points: ProbePoint[] = await res.json();
|
||||
if (cancelled) return;
|
||||
setBuckets(buildBuckets(points));
|
||||
setAnimKey((k) => k + 1);
|
||||
} catch {
|
||||
// silent
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
load();
|
||||
const interval = setInterval(load, 30_000);
|
||||
return () => clearInterval(interval);
|
||||
}, [load]);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [serverId]);
|
||||
|
||||
// Stats
|
||||
const valid = buckets.filter((b) => b.avg !== null);
|
||||
|
||||
@@ -22,8 +22,12 @@ export function ServerLogViewer({ serverId, height = 300, showHeading = true }:
|
||||
const token = pb.authStore.token;
|
||||
const es = new EventSource(`/api/frpc/logs/stream?id=${serverId}&token=${token}`);
|
||||
|
||||
const MAX_LOGS = 500;
|
||||
es.onmessage = (event) => {
|
||||
setLogs((prev) => [...prev, event.data]);
|
||||
setLogs((prev) => {
|
||||
const next = [...prev, event.data];
|
||||
return next.length > MAX_LOGS ? next.slice(next.length - MAX_LOGS) : next;
|
||||
});
|
||||
};
|
||||
|
||||
es.onerror = () => {
|
||||
|
||||
@@ -57,7 +57,8 @@ export function ServerDetailPage() {
|
||||
} finally {
|
||||
if (showLoading) setLoading(false);
|
||||
}
|
||||
}, [id, t]);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [id]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchServer();
|
||||
@@ -144,6 +145,10 @@ export function ServerDetailPage() {
|
||||
visible={mounted}
|
||||
extra={
|
||||
<Flex gap="3" align="center">
|
||||
<Button variant="soft" color="gray" onClick={() => navigate("/servers")}>
|
||||
<Icon icon="lucide:arrow-left" width="16" height="16" />
|
||||
{t("server.backToServers")}
|
||||
</Button>
|
||||
{server.bootStatus === "running" ? (
|
||||
<Button size="2" color="red" variant="soft" onClick={handleStop} disabled={actionLoading}>
|
||||
{actionLoading ? <Icon icon="lucide:loader-2" className="animate-spin" /> : <Icon icon="lucide:square" width="16" height="16" />}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { useState, useEffect, useCallback, useRef } from "react";
|
||||
import pb from "../../lib/pocketbase";
|
||||
import { apiPost } from "../../lib/api";
|
||||
import { toast } from "sonner";
|
||||
@@ -9,6 +9,12 @@ export function useServerProxies(serverId: string | undefined) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [togglingId, setTogglingId] = useState<string | null>(null);
|
||||
const mountedRef = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true;
|
||||
return () => { mountedRef.current = false; };
|
||||
}, []);
|
||||
|
||||
const fetchProxies = useCallback(async (isManual = false) => {
|
||||
if (!serverId) return;
|
||||
@@ -18,11 +24,14 @@ export function useServerProxies(serverId: string | undefined) {
|
||||
filter: `serverId = "${serverId}"`,
|
||||
sort: "-created",
|
||||
});
|
||||
if (!mountedRef.current) return;
|
||||
setProxies(result);
|
||||
} catch (err) {
|
||||
if (!mountedRef.current) return;
|
||||
if ((err as Record<string, unknown>)?.isAbort) return;
|
||||
toast.error(err instanceof Error ? err.message : "Failed to fetch proxies");
|
||||
} finally {
|
||||
if (!mountedRef.current) return;
|
||||
setLoading(false);
|
||||
if (isManual) setRefreshing(false);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user