forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
86 lines (76 loc) · 2.17 KB
/
Copy pathstats.js
File metadata and controls
86 lines (76 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { useContext } from "react";
import getConfig from "next/config";
import jwt from "jsonwebtoken";
import styled from "styled-components";
import css from "@styled-system/css";
import SEO from "../components/SEO";
import Text from "../components/Text";
import { withAuthServerSideProps } from "../utils/withAuth";
import LocaleContext from "../utils/LocaleContext";
const StyledTable = styled.table(() =>
css({
borderCollapse: "collapse",
"&, td": {
border: "1px solid",
borderColor: "border",
},
td: {
px: 4,
py: 3,
},
})
);
const Stats = ({ stats, userRole }) => {
const { getLocaleString } = useContext(LocaleContext);
if (userRole !== "admin") {
return <Text>{getLocaleString("statYouNotPermission")}</Text>;
}
console.log(stats);
return (
<>
<SEO title={getLocaleString("statTrackerStat")} />
<Text as="h1" mb={5}>
{getLocaleString("statTrackerStat")}
</Text>
<StyledTable>
{Object.entries(stats).map(([key, value]) => {
const localeKey = key.charAt(0).toUpperCase() + key.slice(1);
return (
<tr key={`stat-${key}`}>
<td>{getLocaleString(`stat${localeKey}`)}</td>
<td>{value}</td>
</tr>
);
})}
</StyledTable>
</>
);
};
export const getServerSideProps = withAuthServerSideProps(
async ({ token, fetchHeaders }) => {
if (!token) return { props: {} };
const {
publicRuntimeConfig: { SQ_API_URL },
serverRuntimeConfig: { SQ_JWT_SECRET },
} = getConfig();
const { role } = jwt.verify(token, SQ_JWT_SECRET);
if (role !== "admin") return { props: { reports: [], userRole: role } };
try {
const statsRes = await fetch(`${SQ_API_URL}/admin/stats`, {
headers: fetchHeaders,
});
if (
statsRes.status === 403 &&
(await statsRes.text()) === "User is banned"
) {
throw "banned";
}
const stats = await statsRes.json();
return { props: { stats, userRole: role } };
} catch (e) {
if (e === "banned") throw "banned";
return { props: {} };
}
}
);
export default Stats;