Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

feat: Add sorting options (traffic and connections) to the country traffic list and refine its responsive layout.

luolei78966cb6441f
diff --git a/apps/web/app/[locale]/page.tsx b/apps/web/app/[locale]/page.tsx
index 3cbcc7c..a040189 100644
--- a/apps/web/app/[locale]/page.tsx
+++ b/apps/web/app/[locale]/page.tsx
@@ -10,6 +10,8 @@ import {
Server,
Route,
Activity,
+ BarChart3,
+ Link2,
RefreshCw,
Radio,
ChevronDown,
@@ -204,17 +206,52 @@ const CountriesContent = memo(function CountriesContent({
countryData: CountryStats[];
}) {
const t = useTranslations("countries");
+ const [sortBy, setSortBy] = useState<"traffic" | "connections">("traffic");
return (
<div className="space-y-6">
<WorldTrafficMap data={countryData} />
<Card>
- <CardHeader>
- <CardTitle className="text-lg font-semibold">
- {t("details")}
- </CardTitle>
+ <CardHeader className="pb-3">
+ <div className="flex items-center justify-between gap-3">
+ <CardTitle className="text-lg font-semibold">
+ {t("details")}
+ </CardTitle>
+ <div className="flex items-center gap-0.5 bg-muted/50 rounded-lg p-0.5">
+ <Button
+ variant="ghost"
+ size="icon"
+ className={cn(
+ "h-7 w-7 rounded-md transition-all",
+ sortBy === "traffic"
+ ? "bg-background shadow-sm text-primary"
+ : "text-muted-foreground hover:text-foreground",
+ )}
+ onClick={() => setSortBy("traffic")}
+ title={t("sortByTraffic")}
+ aria-label={t("sortByTraffic")}
+ >
+ <BarChart3 className="w-4 h-4" />
+ </Button>
+ <Button
+ variant="ghost"
+ size="icon"
+ className={cn(
+ "h-7 w-7 rounded-md transition-all",
+ sortBy === "connections"
+ ? "bg-background shadow-sm text-primary"
+ : "text-muted-foreground hover:text-foreground",
+ )}
+ onClick={() => setSortBy("connections")}
+ title={t("sortByConnections")}
+ aria-label={t("sortByConnections")}
+ >
+ <Link2 className="w-4 h-4" />
+ </Button>
+ </div>
+ </div>
</CardHeader>
<CardContent>
- <CountryTrafficList data={countryData} />
+ <CountryTrafficList data={countryData} sortBy={sortBy} />
</CardContent>
</Card>
</div>
@@ -637,7 +674,7 @@ export default function DashboardPage() {
<header className="sticky top-0 z-40 border-b border-border/40 bg-background/80 backdrop-blur-md">
<div className="flex items-center justify-between h-14 px-4 lg:px-6">
<div className="flex items-center gap-3">
- {/* Mobile: Logo, Desktop: Page Title */}
+ {/* Mobile: Logo */}
<div className="flex items-center gap-2">
<div className="lg:hidden w-8 h-8 rounded-lg bg-primary/10 flex items-center justify-center overflow-hidden">
<Image
@@ -648,9 +685,6 @@ export default function DashboardPage() {
className="w-full h-full object-cover"
/>
</div>
- <h2 className="hidden lg:block font-semibold">
- {t(activeTab)}
- </h2>
</div>
{/* Backend Selector */}
diff --git a/apps/web/components/country-traffic-list.tsx b/apps/web/components/country-traffic-list.tsx
index 54f23e8..06f081e 100644
--- a/apps/web/components/country-traffic-list.tsx
+++ b/apps/web/components/country-traffic-list.tsx
@@ -1,13 +1,14 @@
"use client";
import { useMemo } from "react";
-import { Link2, ArrowDown, ArrowUp } from "lucide-react";
+import { Link2 } from "lucide-react";
import { CountryFlag } from "@/components/country-flag";
import { formatBytes, formatNumber } from "@/lib/utils";
import type { CountryStats } from "@clashmaster/shared";
interface CountryTrafficListProps {
data: CountryStats[];
+ sortBy?: "traffic" | "connections";
}
// Continent colors
@@ -21,11 +22,10 @@ function getContinentColor(continent: string): string {
return CONTINENT_COLORS[continent] || CONTINENT_COLORS.Unknown;
}
-function formatBytesNoWrap(value: number): string {
- return formatBytes(value).replace(" ", "\u00A0");
-}
-
-export function CountryTrafficList({ data }: CountryTrafficListProps) {
+export function CountryTrafficList({
+ data,
+ sortBy = "traffic",
+}: CountryTrafficListProps) {
const countries = useMemo(() => {
if (!data) return [];
return data
@@ -35,8 +35,13 @@ export function CountryTrafficList({ data }: CountryTrafficListProps) {
color: getContinentColor(country.continent),
total: country.totalDownload + country.totalUpload,
}))
- .sort((a, b) => b.total - a.total);
- }, [data]);
+ .sort((a, b) => {
+ if (sortBy === "connections") {
+ return b.totalConnections - a.totalConnections;
+ }
+ return b.total - a.total;
+ });
+ }, [data, sortBy]);
const totalTraffic = useMemo(() => {
return countries.reduce((sum, c) => sum + c.total, 0);
@@ -44,7 +49,7 @@ export function CountryTrafficList({ data }: CountryTrafficListProps) {
if (countries.length === 0) {
return (
- <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-3">
+ <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-3">
{[...Array(6)].map((_, i) => (
<div key={i} className="p-3 rounded-xl border border-border/30 bg-muted/50 h-24" />
))}
@@ -53,31 +58,34 @@ export function CountryTrafficList({ data }: CountryTrafficListProps) {
}
return (
- <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-3">
+ <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-3">
{countries.map((country) => {
const percentage = totalTraffic > 0 ? (country.total / totalTraffic) * 100 : 0;
return (
<div
key={country.country}
- className="p-3 rounded-xl border border-border/50 bg-card/30 hover:bg-card/60 transition-colors"
+ className="p-3 rounded-xl border border-border/50 bg-card/50 hover:bg-card transition-colors"
>
- {/* Header: Flag + Name */}
+ {/* Header: Flag + Name + Total */}
<div className="flex items-center gap-2 mb-2">
<CountryFlag country={country.country} className="h-4 w-6" />
- <p className="font-medium text-sm truncate" title={country.countryName}>
+ <p className="flex-1 font-medium text-sm truncate" title={country.countryName || country.country}>
{country.countryName || country.country}
</p>
+ <span className="text-base font-bold tabular-nums whitespace-nowrap shrink-0 sm:hidden">
+ {formatBytes(country.total)}
+ </span>
</div>
{/* Traffic Stats */}
<div className="space-y-1.5">
- {/* Total */}
- <div className="flex items-baseline justify-between">
+ {/* Desktop total + share (keep previous layout) */}
+ <div className="hidden sm:flex items-baseline justify-between">
<span className="text-base sm:text-lg font-bold tabular-nums whitespace-nowrap">
- {formatBytesNoWrap(country.total)}
+ {formatBytes(country.total)}
</span>
- <span className="text-xs text-muted-foreground">
+ <span className="text-xs text-muted-foreground tabular-nums whitespace-nowrap">
{percentage.toFixed(1)}%
</span>
</div>
@@ -90,23 +98,24 @@ export function CountryTrafficList({ data }: CountryTrafficListProps) {
/>
</div>
- {/* DL/UL row */}
- <div className="flex items-center justify-between text-xs">
- <span className="flex items-center gap-0.5 text-blue-500 whitespace-nowrap">
- <ArrowDown className="w-3 h-3" />
- {formatBytesNoWrap(country.totalDownload)}
- </span>
- <span className="flex items-center gap-0.5 text-purple-500 whitespace-nowrap">
- <ArrowUp className="w-3 h-3" />
- {formatBytesNoWrap(country.totalUpload)}
+ {/* Metrics + Share */}
+ <div className="flex items-end justify-between text-xs">
+ <div className="flex items-center gap-2 sm:flex-col sm:items-start sm:gap-1 min-w-0">
+ <span className="text-blue-500 tabular-nums whitespace-nowrap">
+ ↓ {formatBytes(country.totalDownload)}
+ </span>
+ <span className="text-purple-500 tabular-nums whitespace-nowrap">
+ ↑ {formatBytes(country.totalUpload)}
+ </span>
+ <span className="inline-flex items-center gap-1 text-muted-foreground tabular-nums whitespace-nowrap">
+ <Link2 className="w-3 h-3" />
+ <span>{formatNumber(country.totalConnections)}</span>
+ </span>
+ </div>
+ <span className="text-xs text-muted-foreground tabular-nums whitespace-nowrap shrink-0 sm:hidden">
+ {percentage.toFixed(1)}%
</span>
</div>
-
- {/* Connections */}
- <div className="flex items-center gap-1 text-xs text-muted-foreground">
- <Link2 className="w-3 h-3" />
- <span>{formatNumber(country.totalConnections)}</span>
- </div>
</div>
</div>
);
diff --git a/apps/web/messages/en.json b/apps/web/messages/en.json
index a14e016..5757efd 100644
--- a/apps/web/messages/en.json
+++ b/apps/web/messages/en.json
@@ -202,7 +202,9 @@
"distribution": "Regional Traffic Distribution",
"continents": "Continents:",
"noData": "No data",
- "connections": "connections"
+ "connections": "connections",
+ "sortByTraffic": "Sort by traffic",
+ "sortByConnections": "Sort by connections"
},
"proxies": {
"title": "Proxies",
diff --git a/apps/web/messages/zh.json b/apps/web/messages/zh.json
index 09a45c0..922a51b 100644
--- a/apps/web/messages/zh.json
+++ b/apps/web/messages/zh.json
@@ -201,7 +201,9 @@
"distribution": "地区流量分布",
"continents": "大洲:",
"noData": "暂无数据",
- "connections": "连接"
+ "connections": "连接",
+ "sortByTraffic": "按流量排序",
+ "sortByConnections": "按连接数排序"
},
"proxies": {
"title": "代理节点",