Skip to content

Commit 032e709

Browse files
committed
优化访问量组件
1 parent 52b823d commit 032e709

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

src/components/react/HitsInfo.tsx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,63 @@ import React from "react";
22

33
export type Props = {
44
tag: string;
5-
label: string;
65
today: string;
76
total: string;
7+
ok: string;
8+
error: string;
9+
loading: string;
810
};
911

12+
type State = "ok" | "error" | "loading";
13+
1014
type Data = {
1115
today: string;
1216
total: string;
1317
};
1418

15-
const HitsInfo: React.FC<Props> = ({ tag, label, today: todayLabel, total: totalLabel }) => {
19+
const HitsInfo: React.FC<Props> = ({
20+
tag,
21+
today: todayLabel,
22+
total: totalLabel,
23+
ok,
24+
error,
25+
loading,
26+
}) => {
27+
const [state, setState] = React.useState<State>("loading");
1628
const [data, setData] = React.useState<Data | null>(null);
1729
React.useEffect(() => {
1830
const hitsUrl = new URL("https://hits.zkitefly.eu.org");
1931
hitsUrl.searchParams.set("tag", tag);
2032

2133
fetch(hitsUrl, { method: "HEAD" })
22-
.then(async (response) => {
23-
if (response.status !== 200) return;
24-
const { headers } = response;
25-
const total = headers.get("X-Total-Hits");
26-
const today = headers.get("X-Today-Hits");
27-
if (total !== null && today !== null) {
28-
setData({ total, today });
34+
.then((response) => {
35+
if (response.status === 200) {
36+
const { headers } = response;
37+
const total = headers.get("X-Total-Hits");
38+
const today = headers.get("X-Today-Hits");
39+
if (total !== null && today !== null) {
40+
setData({ total, today });
41+
setState("ok");
42+
return;
43+
}
2944
}
45+
setState("error");
3046
})
31-
.catch(() => {});
47+
.catch(() => {
48+
setState("error");
49+
});
3250
}, []);
3351

34-
if (!data) return <></>;
52+
if (data === null) {
53+
return <>{state === "ok" ? ok : state === "error" ? error : loading}</>;
54+
}
3555

3656
return (
37-
<div className="hits">
38-
<strong>{label}</strong> <span title={todayLabel}>{data.today}</span>
57+
<>
58+
<span title={todayLabel}>{data.today}</span>
3959
{" / "}
4060
<span title={totalLabel}>{data.total}</span>
41-
</div>
61+
</>
4262
);
4363
};
4464

src/components/starlight/PageTitle.astro

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,25 @@ const clientVars = autoRedirect ?? { delay: 0, href: "" };
3535
<DefaultPageTitle />
3636

3737
<div class="info">
38+
<div class="hits">
39+
<strong>{Astro.locals.t("page.hits.label")}</strong>
40+
<HitsInfo
41+
tag={tag}
42+
today={Astro.locals.t("page.hits.today")}
43+
total={Astro.locals.t("page.hits.total")}
44+
ok={Astro.locals.t("page.hits.ok")}
45+
error={Astro.locals.t("page.hits.error")}
46+
loading={Astro.locals.t("page.hits.loading")}
47+
client:only="react"
48+
/>
49+
</div>
3850
{
3951
showContributors && (
4052
<div class="contributors">
4153
<strong>{Astro.locals.t("page.contributors")}</strong> {contributors.join(" ")}
4254
</div>
4355
)
4456
}
45-
<HitsInfo
46-
tag={tag}
47-
label={Astro.locals.t("page.hits.label")}
48-
today={Astro.locals.t("page.hits.today")}
49-
total={Astro.locals.t("page.hits.total")}
50-
client:only="react"
51-
/>
5257
</div>
5358

5459
<script is:inline define:vars={clientVars}>

src/content.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export const collections = {
3737
"page.hits.label": z.string(),
3838
"page.hits.today": z.string(),
3939
"page.hits.total": z.string(),
40+
"page.hits.ok": z.string(),
41+
"page.hits.error": z.string(),
42+
"page.hits.loading": z.string(),
4043
}),
4144
}),
4245
}),

src/content/i18n/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"page.contributors": "Contributors",
3131
"page.hits.label": "Visits",
3232
"page.hits.today": "Today Visits",
33-
"page.hits.total": "Total Visits"
33+
"page.hits.total": "Total Visits",
34+
"page.hits.ok": "Data retrieved successfully",
35+
"page.hits.error": "Failed to retrieve data",
36+
"page.hits.loading": "Fetching data"
3437
}

src/content/i18n/zh-Hans.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"page.contributors": "贡献者",
3131
"page.hits.label": "访问量",
3232
"page.hits.today": "今日访问",
33-
"page.hits.total": "总访问"
33+
"page.hits.total": "总访问",
34+
"page.hits.ok": "获取数据成功",
35+
"page.hits.error": "获取数据失败",
36+
"page.hits.loading": "正在获取数据"
3437
}

src/content/i18n/zh-Hant.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"page.contributors": "貢獻者",
3131
"page.hits.label": "訪問量",
3232
"page.hits.today": "今日訪問",
33-
"page.hits.total": "總訪問"
33+
"page.hits.total": "總訪問",
34+
"page.hits.ok": "取得資料成功",
35+
"page.hits.error": "取得資料失敗",
36+
"page.hits.loading": "正在取得資料"
3437
}

0 commit comments

Comments
 (0)