-
-
Notifications
You must be signed in to change notification settings - Fork 181
feat: 优化认证错误和用户/密钥状态显示 (Issue #425) #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,13 +92,41 @@ export interface KeyRowItemProps { | |
| }; | ||
| } | ||
|
|
||
| const EXPIRING_SOON_MS = 72 * 60 * 60 * 1000; // 72小时 | ||
|
|
||
| function splitGroups(value?: string | null): string[] { | ||
| return (value ?? "") | ||
| .split(",") | ||
| .map((g) => g.trim()) | ||
| .filter(Boolean); | ||
|
Comment on lines
97
to
101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| function formatExpiry(expiresAt: string | null | undefined, locale: string): string { | ||
| if (!expiresAt) return "-"; | ||
| const date = new Date(expiresAt); | ||
| // 如果解析失败(如"永不过期"等翻译文本),直接返回原文本 | ||
| if (Number.isNaN(date.getTime())) return expiresAt; | ||
| return formatDate(date, "yyyy-MM-dd", locale); | ||
| } | ||
|
|
||
| function getKeyExpiryStatus( | ||
| status: "enabled" | "disabled", | ||
| expiresAt: string | null | undefined | ||
| ): { label: string; variant: "default" | "secondary" | "destructive" | "outline" } { | ||
| if (status === "disabled") return { label: "disabled", variant: "secondary" }; | ||
| if (!expiresAt) return { label: "active", variant: "default" }; | ||
|
|
||
| const date = new Date(expiresAt); | ||
| if (Number.isNaN(date.getTime())) return { label: "active", variant: "default" }; | ||
|
|
||
| const now = Date.now(); | ||
| const expTs = date.getTime(); | ||
|
|
||
| if (expTs <= now) return { label: "expired", variant: "destructive" }; | ||
| if (expTs - now <= EXPIRING_SOON_MS) return { label: "expiringSoon", variant: "outline" }; | ||
| return { label: "active", variant: "default" }; | ||
| } | ||
|
|
||
| export function KeyRowItem({ | ||
| keyData, | ||
| userProviderGroup: _userProviderGroup, | ||
|
|
@@ -148,6 +176,9 @@ export function KeyRowItem({ | |
| const keyGroups = splitGroups(keyData.providerGroup); | ||
| const effectiveGroups = keyGroups.length > 0 ? keyGroups : [translations.defaultGroup]; | ||
| const visibleGroups = effectiveGroups.slice(0, 1); | ||
|
|
||
| // 计算 key 过期状态 | ||
| const keyExpiryStatus = getKeyExpiryStatus(localStatus, localExpiresAt); | ||
| const remainingGroups = Math.max(0, effectiveGroups.length - visibleGroups.length); | ||
| const effectiveGroupText = effectiveGroups.join(", "); | ||
|
|
||
|
|
@@ -194,13 +225,6 @@ export function KeyRowItem({ | |
| } | ||
| }; | ||
|
|
||
| const formatExpiry = (expiresAt: string | null | undefined): string => { | ||
| if (!expiresAt) return "-"; | ||
| const date = new Date(expiresAt); | ||
| if (Number.isNaN(date.getTime())) return "-"; | ||
| return formatDate(date, "yyyy-MM-dd", locale); | ||
| }; | ||
|
|
||
| const handleQuickRenewConfirm = async ( | ||
| _keyId: number, | ||
| expiresAt: Date, | ||
|
|
@@ -297,11 +321,8 @@ export function KeyRowItem({ | |
| <div className="min-w-0"> | ||
| <div className="flex items-center gap-2 min-w-0"> | ||
| <div className="truncate font-medium">{keyData.name}</div> | ||
| <Badge | ||
| variant={localStatus === "enabled" ? "default" : "secondary"} | ||
| className="text-[10px]" | ||
| > | ||
| {localStatus === "enabled" ? translations.status.enabled : translations.status.disabled} | ||
| <Badge variant={keyExpiryStatus.variant} className="text-[10px] shrink-0"> | ||
| {tKeyStatus(keyExpiryStatus.label)} | ||
| </Badge> | ||
| </div> | ||
| </div> | ||
|
|
@@ -444,7 +465,7 @@ export function KeyRowItem({ | |
| setQuickRenewOpen(true); | ||
| }} | ||
| > | ||
| {formatExpiry(localExpiresAt)} | ||
| {formatExpiry(localExpiresAt, locale)} | ||
| </div> | ||
|
|
||
| {/* 操作 */} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { Switch } from "@/components/ui/switch"; | |
| import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; | ||
| import { useRouter } from "@/i18n/routing"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { getContrastTextColor, getGroupColor } from "@/lib/utils/color"; | ||
| import { formatDate } from "@/lib/utils/date-format"; | ||
| import type { UserDisplay } from "@/types/user"; | ||
| import { KeyRowItem } from "./key-row-item"; | ||
|
|
@@ -66,6 +67,30 @@ export interface UserKeyTableRowProps { | |
| } | ||
|
|
||
| const DEFAULT_GRID_COLUMNS_CLASS = "grid-cols-[minmax(260px,1fr)_120px_repeat(6,90px)_80px]"; | ||
| const EXPIRING_SOON_MS = 72 * 60 * 60 * 1000; // 72小时 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const MAX_VISIBLE_GROUPS = 2; // 最多显示的分组数量 | ||
|
|
||
| function splitGroups(value?: string | null): string[] { | ||
| return (value ?? "") | ||
| .split(",") | ||
| .map((g) => g.trim()) | ||
| .filter(Boolean); | ||
|
Comment on lines
+73
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| function getExpiryStatus( | ||
| isEnabled: boolean, | ||
| expiresAt: Date | null | undefined | ||
| ): { label: string; variant: "default" | "secondary" | "destructive" | "outline" } { | ||
| const now = Date.now(); | ||
| const expTs = expiresAt?.getTime(); | ||
| const hasExpiry = typeof expTs === "number" && Number.isFinite(expTs); | ||
|
|
||
| if (!isEnabled) return { label: "disabled", variant: "secondary" }; | ||
| if (hasExpiry && expTs <= now) return { label: "expired", variant: "destructive" }; | ||
| if (hasExpiry && expTs - now <= EXPIRING_SOON_MS) | ||
| return { label: "expiringSoon", variant: "outline" }; | ||
| return { label: "active", variant: "default" }; | ||
| } | ||
|
|
||
| function normalizeLimitValue(value: unknown): number | null { | ||
| const raw = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN; | ||
|
|
@@ -129,6 +154,14 @@ export function UserKeyTableRow({ | |
|
|
||
| const expiresText = formatExpiry(localExpiresAt ?? null, locale); | ||
|
|
||
| // 计算用户过期状态 | ||
| const expiryStatus = getExpiryStatus(localIsEnabled, localExpiresAt ?? null); | ||
|
|
||
| // 处理 Provider Group:拆分成数组 | ||
| const userGroups = splitGroups(user.providerGroup); | ||
| const visibleGroups = userGroups.slice(0, MAX_VISIBLE_GROUPS); | ||
| const remainingGroupsCount = Math.max(0, userGroups.length - MAX_VISIBLE_GROUPS); | ||
|
|
||
| const limit5h = normalizeLimitValue(user.limit5hUsd); | ||
| const limitDaily = normalizeLimitValue(user.dailyQuota); | ||
| const limitWeekly = normalizeLimitValue(user.limitWeeklyUsd); | ||
|
|
@@ -222,11 +255,34 @@ export function UserKeyTableRow({ | |
| {isExpanded ? translations.collapse : translations.expand} | ||
| </span> | ||
| <span className="font-medium truncate">{user.name}</span> | ||
| {!localIsEnabled && ( | ||
| <Badge variant={expiryStatus.variant} className="text-[10px] shrink-0"> | ||
| {tUserStatus(expiryStatus.label)} | ||
| </Badge> | ||
| {visibleGroups.map((group) => { | ||
| const bgColor = getGroupColor(group); | ||
| return ( | ||
| <Badge | ||
| key={group} | ||
| className="text-[10px] shrink-0" | ||
| style={{ | ||
| backgroundColor: bgColor, | ||
| color: getContrastTextColor(bgColor), | ||
| }} | ||
| > | ||
| {group} | ||
| </Badge> | ||
| ); | ||
| })} | ||
| {remainingGroupsCount > 0 && ( | ||
| <Badge variant="secondary" className="text-[10px] shrink-0"> | ||
| {translations.userStatus?.disabled || "Disabled"} | ||
| +{remainingGroupsCount} | ||
| </Badge> | ||
| )} | ||
| {user.tags && user.tags.length > 0 && ( | ||
| <span className="text-xs text-muted-foreground truncate"> | ||
| [{user.tags.join(", ")}] | ||
| </span> | ||
| )} | ||
| {user.note ? ( | ||
| <span className="text-xs text-muted-foreground truncate">{user.note}</span> | ||
| ) : null} | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
常量
EXPIRING_SOON_MS在user-key-table-row.tsx中也有定义。为了避免重复和确保一致性,建议将其提取到一个共享的工具文件中(例如src/lib/constants.ts或src/lib/utils/time.ts)。