mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-10 22:56:06 +00:00
941eba546d
Bring back the v2.9.x traffic column UX: used amount, color-coded progress bar, limit/infinity label, and hover popover with upload/download/remaining breakdown. Adds a shared ClientTrafficCell component, traffic display helpers, and unit tests.
65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import { ColorUtils } from '@/utils';
|
|
|
|
export interface TrafficDisplayInput {
|
|
up: number;
|
|
down: number;
|
|
total: number;
|
|
enabled: boolean;
|
|
trafficDiff: number;
|
|
}
|
|
|
|
export interface TrafficDisplay {
|
|
used: number;
|
|
remaining: number;
|
|
percent: number;
|
|
isUnlimited: boolean;
|
|
isDepleted: boolean;
|
|
strokeColor: string;
|
|
status: 'normal' | 'exception' | undefined;
|
|
}
|
|
|
|
const DISABLED_STROKE = {
|
|
light: '#bcbcbc',
|
|
dark: 'rgb(72, 84, 105)',
|
|
} as const;
|
|
|
|
const UNLIMITED_STROKE = '#722ed1';
|
|
|
|
export function computeTrafficDisplay(
|
|
input: TrafficDisplayInput,
|
|
isDark: boolean,
|
|
): TrafficDisplay {
|
|
const up = input.up || 0;
|
|
const down = input.down || 0;
|
|
const used = up + down;
|
|
const total = input.total || 0;
|
|
const isUnlimited = total <= 0;
|
|
|
|
let percent = 100;
|
|
if (!isUnlimited) {
|
|
percent = Math.min(100, Math.max(0, (used / total) * 100));
|
|
}
|
|
|
|
const isDepleted = !isUnlimited && used >= total;
|
|
const remaining = isUnlimited ? 0 : Math.max(0, total - used);
|
|
|
|
let strokeColor: string;
|
|
if (!input.enabled) {
|
|
strokeColor = isDark ? DISABLED_STROKE.dark : DISABLED_STROKE.light;
|
|
} else if (isUnlimited) {
|
|
strokeColor = UNLIMITED_STROKE;
|
|
} else {
|
|
strokeColor = ColorUtils.clientUsageColor({ up, down, total }, input.trafficDiff);
|
|
}
|
|
|
|
return {
|
|
used,
|
|
remaining,
|
|
percent,
|
|
isUnlimited,
|
|
isDepleted,
|
|
strokeColor,
|
|
status: isDepleted && input.enabled ? 'exception' : undefined,
|
|
};
|
|
}
|