fix(online-history): default to one-day history

This commit is contained in:
hamster1963 committed 2026-10-22 17:09:24 +08:00
1 parent cc33841ede
commit 512ef01b34
3 files changed
+13 -9

No files matched your search

+1 -1
View File
@@ -245,7 +245,7 @@ export default function ServerDetailOnlineHistory({
}) { }) {
const { t } = useTranslation(); const { t } = useTranslation();
const { lastData } = useWebSocketContext(); const { lastData } = useWebSocketContext();
const [days, setDays] = useState(DAY_OPTIONS[1]); const [days, setDays] = useState(DAY_OPTIONS[0]);
const [guestMaxDays, setGuestMaxDays] = useState<number | null>(null); const [guestMaxDays, setGuestMaxDays] = useState<number | null>(null);
const [periodMode, setPeriodMode] = useState<PeriodMode>("online"); const [periodMode, setPeriodMode] = useState<PeriodMode>("online");
const [visibleDays, setVisibleDays] = useState(PERIODS_INITIAL_DAYS); const [visibleDays, setVisibleDays] = useState(PERIODS_INITIAL_DAYS);
@@ -30,7 +30,7 @@ function Metric({ footer = true }: { footer?: boolean }) {
); );
} }
export default function OnlineHistorySkeleton({ days = 7 }: { days?: number }) { export default function OnlineHistorySkeleton({ days = 1 }: { days?: number }) {
const { t } = useTranslation(); const { t } = useTranslation();
const dayCount = Math.min(31, Math.max(1, days) + 1); const dayCount = Math.min(31, Math.max(1, days) + 1);
const cardClassName = cn({ const cardClassName = cn({
@@ -82,7 +82,7 @@ describe("ServerDetailOnlineHistory", () => {
screen.getByRole("status", { name: "serverOnlineHistory.loading" }), screen.getByRole("status", { name: "serverOnlineHistory.loading" }),
).toHaveAttribute("aria-busy", "true"); ).toHaveAttribute("aria-busy", "true");
expect(screen.getByTestId("online-history-loading")).toBeInTheDocument(); expect(screen.getByTestId("online-history-loading")).toBeInTheDocument();
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 7); expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 1);
}); });
it("keeps existing content instead of flashing the skeleton while changing days", async () => { it("keeps existing content instead of flashing the skeleton while changing days", async () => {
@@ -299,7 +299,7 @@ describe("ServerDetailOnlineHistory", () => {
await user.click(screen.getByText("serverOnlineHistory.days30")); await user.click(screen.getByText("serverOnlineHistory.days30"));
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledTimes(1); expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledTimes(1);
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 7); expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 1);
}); });
it("shows the login hint when the API rejects anonymous guests", async () => { it("shows the login hint when the API rejects anonymous guests", async () => {
@@ -314,30 +314,34 @@ describe("ServerDetailOnlineHistory", () => {
).toBeInTheDocument(); ).toBeInTheDocument();
}); });
it("falls back to one day and retries automatically for guests", async () => { it("falls back to one day when guests request a longer window", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
apiMocks.fetchServerOnlineHistory apiMocks.fetchServerOnlineHistory
.mockResolvedValueOnce(onlineHistoryResponse())
.mockRejectedValueOnce( .mockRejectedValueOnce(
new Error("unauthorized: only 1d data available for guests"), new Error("unauthorized: only 1d data available for guests"),
) )
.mockResolvedValue(onlineHistoryResponse()); .mockResolvedValue(onlineHistoryResponse(1));
renderWithQuery(<ServerDetailOnlineHistory server_id="7" />); renderWithQuery(<ServerDetailOnlineHistory server_id="7" />);
// 首次 7d 被拒后自动以 1d 重试并渲染数据 // 首屏直接请求 1d;手动选择 7d 被拒后自动回退到 1d。
await screen.findByText("serverOnlineHistory.dailyOnline"); await screen.findByText("serverOnlineHistory.dailyOnline");
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(1, 7, 1);
await user.click(screen.getByText("serverOnlineHistory.days7"));
await waitFor(() => { await waitFor(() => {
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith( expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(
2, 3,
7, 7,
1, 1,
); );
}); });
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(2, 7, 7);
// 更长窗口被锁定(游客上限 1 天),点击不再发起新请求 // 更长窗口被锁定(游客上限 1 天),点击不再发起新请求
const calls = apiMocks.fetchServerOnlineHistory.mock.calls.length; const calls = apiMocks.fetchServerOnlineHistory.mock.calls.length;
await user.click(screen.getByText("serverOnlineHistory.days30")); await user.click(screen.getByText("serverOnlineHistory.days30"));
expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(2, 7, 1); expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(3, 7, 1);
expect(apiMocks.fetchServerOnlineHistory.mock.calls.length).toBe(calls); expect(apiMocks.fetchServerOnlineHistory.mock.calls.length).toBe(calls);
}); });