Skip to content

Commit d6bcb3a

Browse files
authored
Merge pull request #493 from Testaustime/add-you-badge
Add you badge to leaderboards and friends pages
2 parents 87b6a8c + 6b97c86 commit d6bcb3a

File tree

9 files changed

+48
-16
lines changed

9 files changed

+48
-16
lines changed

.storybook/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ const config: StorybookConfig = {
1313
},
1414
};
1515

16-
export default config
16+
export default config;

public/locales/en/common.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
"yourFriends": "Your friends"
8383
},
8484
"leaderboards": {
85-
"admin": "Admin",
8685
"create": "Create",
8786
"createNewLeaderboard": "Create new leaderboard",
8887
"deleteLeaderboard": "Delete leaderboard",
@@ -307,5 +306,9 @@
307306
"text": "Not working?",
308307
"link": "Copy authentication token on settings page."
309308
}
309+
},
310+
"badges": {
311+
"admin": "Admin",
312+
"you": "You"
310313
}
311314
}

public/locales/fi/common.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
"yourFriends": "Kaverisi"
8383
},
8484
"leaderboards": {
85-
"admin": "Ylläpitäjä",
8685
"create": "Luo",
8786
"createNewLeaderboard": "Luo uusi tulostaulu",
8887
"deleteLeaderboard": "Poista tulostaulu",
@@ -307,5 +306,9 @@
307306
"text": "Eikö toimi?",
308307
"link": "Kopioi tunnistautumistunnus asetukset-sivulla."
309308
}
309+
},
310+
"badges": {
311+
"admin": "Ylläpitäjä",
312+
"you": "Sinä"
310313
}
311314
}

src/app/[locale]/leaderboards/[name]/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { PromoteUserButton } from "./PromoteUserButton";
2323
import { KickUserButton } from "./KickUserButton";
2424
import { GetLeaderboardError } from "../../../../types";
2525
import { getPreferences } from "../../../../utils/cookieUtils";
26+
import { YOU_BADGE_COLOR } from "../../../../utils/constants";
2627

2728
export default async function LeaderboardPage({
2829
params: { locale, name },
@@ -115,9 +116,12 @@ export default async function LeaderboardPage({
115116
</TableTd>
116117
<TableTd>
117118
{member.username}
118-
{member.admin && (
119-
<Badge ml="sm">{t("leaderboards.admin")}</Badge>
119+
{member.username === me.username && (
120+
<Badge ml="sm" color={YOU_BADGE_COLOR}>
121+
{t("badges.you")}
122+
</Badge>
120123
)}
124+
{member.admin && <Badge ml="sm">{t("badges.admin")}</Badge>}
121125
</TableTd>
122126
<TableTd>
123127
{prettyDuration(member.time_coded, maxTimeUnit)}

src/app/[locale]/leaderboards/page.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { CreateNewLeaderboardButton } from "./CreateNewLeaderboardButton";
77
import { JoinLeaderboardButton } from "./JoinLeaderboardButton";
88
import { getMyLeaderboards } from "../../../api/leaderboardApi";
99
import { getPreferences } from "../../../utils/cookieUtils";
10+
import { getMe } from "../../../api/usersApi";
1011

1112
export type LeaderboardsPageProps = {
1213
initialLeaderboards: LeaderboardData[];
@@ -46,6 +47,22 @@ export default async function LeaderboardsPage({
4647
}
4748
}
4849

50+
const me = await getMe();
51+
52+
if (!me) {
53+
redirect("/login");
54+
}
55+
56+
if ("error" in me) {
57+
if (me.error === "Unauthorized") {
58+
redirect("/login");
59+
} else if (me.error === "Too many requests") {
60+
redirect("/rate-limited");
61+
} else {
62+
throw new Error(JSON.stringify(me));
63+
}
64+
}
65+
4966
const { maxTimeUnit } = getPreferences();
5067

5168
return (
@@ -60,6 +77,7 @@ export default async function LeaderboardsPage({
6077
<LeaderboardsList
6178
leaderboards={leaderboardList}
6279
maxTimeUnit={maxTimeUnit}
80+
meUsername={me.username}
6381
/>
6482
</>
6583
);

src/components/friends/FriendListRow.module.css

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/friends/FriendListRow.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Button, HoverCard, TableTd, TableTr } from "@mantine/core";
2-
import styles from "./FriendListRow.module.css";
1+
import { Badge, Button, HoverCard, TableTd, TableTr } from "@mantine/core";
32
import { CurrentActivity } from "../CurrentActivity/CurrentActivity";
43
import { BlinkingDot } from "../CurrentActivity/BlinkingDot";
54
import { CurrentActivityDisplay } from "../CurrentActivity/CurrentActivityDisplay";
@@ -12,6 +11,7 @@ import { useTranslation } from "react-i18next";
1211
import { showNotification } from "@mantine/notifications";
1312
import { logOutAndRedirect } from "../../utils/authUtils";
1413
import { useState } from "react";
14+
import { YOU_BADGE_COLOR } from "../../utils/constants";
1515

1616
type FriendListRowProps = {
1717
isMe: boolean;
@@ -37,11 +37,12 @@ export const FriendListRow = ({
3737
const [isDeleting, setIsDeleting] = useState(false);
3838

3939
return (
40-
<TableTr className={isMe ? styles.tableRow : undefined}>
40+
<TableTr>
4141
<TableTd>{index + 1}</TableTd>
4242
<TableTd>
4343
<div style={{ display: "flex", alignItems: "center", gap: 8 }}>
4444
{username}
45+
{isMe && <Badge color={YOU_BADGE_COLOR}>{t("badges.you")}</Badge>}
4546
{status && (
4647
<HoverCard>
4748
<HoverCard.Target>

src/components/leaderboard/LeaderboardsList.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { getOrdinalSuffix } from "../../utils/stringUtils";
66
import { Leaderboard } from "../../types";
77
import { useTranslation } from "react-i18next";
88
import Link from "next/link";
9+
import { YOU_BADGE_COLOR } from "../../utils/constants";
910

1011
interface LeaderboardsListProps {
1112
leaderboards: Leaderboard[];
1213
maxTimeUnit: TimeUnit;
14+
meUsername: string;
1315
}
1416

1517
export const LeaderboardsList = ({
1618
leaderboards,
1719
maxTimeUnit,
20+
meUsername,
1821
}: LeaderboardsListProps) => {
1922
const { t } = useTranslation();
2023

@@ -38,12 +41,17 @@ export const LeaderboardsList = ({
3841
<Table.Td>
3942
{leaderboard.name}
4043
{leaderboard.me.admin && (
41-
<Badge ml="sm">{t("leaderboards.admin")}</Badge>
44+
<Badge ml="sm">{t("badges.admin")}</Badge>
4245
)}
4346
</Table.Td>
4447
<Table.Td>
4548
{topMember.username} (
4649
{prettyDuration(topMember.time_coded, maxTimeUnit)})
50+
{topMember.username === meUsername && (
51+
<Badge color={YOU_BADGE_COLOR} ml="sm">
52+
{t("badges.you")}
53+
</Badge>
54+
)}
4755
</Table.Td>
4856
<Table.Td>
4957
{yourPosition}

src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export const timeInHoursCookieName = "testaustime-time-in-hours";
66

77
export const DEFAULT_DAY_RANGE = "week";
88
export const DEFAULT_TIME_IN_HOURS = false;
9+
10+
export const YOU_BADGE_COLOR = "green" as const;

0 commit comments

Comments
 (0)