Skip to content

Commit c3f5cd1

Browse files
authored
Show user score in header (#1969)
Closes #564 I am going through old issues and trying to finish them. Add user score to header and account menu ![image](https://user-images.githubusercontent.com/24505302/222948537-fa434d5f-1ca5-4c63-aad5-207200f7171b.png) This is hidden on narrow screens ![image](https://user-images.githubusercontent.com/24505302/222948656-ed31b49e-f2df-4ff9-a931-1cbded4a203e.png) The score could also be seen in the user menu (a bit redundant for large screens, but ok). ![image](https://user-images.githubusercontent.com/24505302/222948919-9015138f-89b5-4b26-a2df-a1a061c378e7.png)
1 parent 90bc0c9 commit c3f5cd1

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

website/src/components/Header/Header.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import Image from "next/image";
44
import Link from "next/link";
55
import { useSession } from "next-auth/react";
66
import { useTranslation } from "next-i18next";
7-
import { Flags } from "react-feature-flags";
87
import { LanguageSelector } from "src/components/LanguageSelector";
98

109
import { ColorModeToggler } from "./ColorModeToggler";
1110
import { UserMenu } from "./UserMenu";
11+
import { UserScore } from "./UserScore";
1212

1313
function AccountButton() {
1414
const { data: session } = useSession();
@@ -44,12 +44,12 @@ export function Header() {
4444
</Link>
4545

4646
<Flex alignItems="center" gap={["2", "4"]}>
47-
<Flags authorizedFlags={["flagTest"]}>
48-
<Text>FlagTest</Text>
49-
</Flags>
5047
<LanguageSelector />
5148
<AccountButton />
5249
<UserMenu />
50+
<div className="hidden md:block">
51+
<UserScore />
52+
</div>
5353
<ColorModeToggler />
5454
</Flex>
5555
</Box>

website/src/components/Header/UserMenu.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import NextLink from "next/link";
1717
import { signOut, useSession } from "next-auth/react";
1818
import { useTranslation } from "next-i18next";
1919
import React, { ElementType, useCallback } from "react";
20+
import { UserScore } from "src/components/Header/UserScore";
2021
import { useHasAnyRole } from "src/hooks/auth/useHasAnyRole";
2122

2223
interface MenuOption {
@@ -70,15 +71,15 @@ export function UserMenu() {
7071
return (
7172
<Menu>
7273
<MenuButton border="solid" borderRadius="full" borderWidth="thin" borderColor={borderColor}>
73-
<Box display="flex" alignItems="center" gap="3" p="1" paddingRight={[1, 1, 1, 6, 6]}>
74+
<Box display="flex" alignItems="center" gap="3" p="1">
7475
<Avatar size="sm" src={session.user.image!} />
75-
<Text data-cy="username" className="hidden lg:flex">
76+
<Text data-cy="username" className="hidden lg:flex ltr:pr-2 rtl:pl-2">
7677
{session.user.name || "New User"}
7778
</Text>
7879
</Box>
7980
</MenuButton>
8081
<MenuList p="2" borderRadius="xl" shadow="none">
81-
<Box display="flex" flexDirection="column" alignItems="center" borderRadius="md" p="4">
82+
<Box display="flex" flexDirection="column" alignItems="center" borderRadius="md" p="1" gap="2">
8283
<Text>
8384
{session.user.name}
8485
{isAdminOrMod ? (
@@ -87,6 +88,7 @@ export function UserMenu() {
8788
</Badge>
8889
) : null}
8990
</Text>
91+
<UserScore />
9092
</Box>
9193
<MenuDivider />
9294
<MenuGroup>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { HStack } from "@chakra-ui/react";
2+
import { Icon } from "@chakra-ui/react";
3+
import { Star } from "lucide-react";
4+
import { useTranslation } from "react-i18next";
5+
import { useUserScore } from "src/hooks/ui/useUserScore";
6+
7+
export const UserScore = () => {
8+
const score = useUserScore();
9+
const { t } = useTranslation("leaderboard");
10+
11+
if (!Number.isFinite(score)) {
12+
return null;
13+
}
14+
15+
return (
16+
<HStack gap="1" title={t("score")}>
17+
<>{score}</>
18+
<Icon as={Star} fill="gold" w="5" h="5" color="gold" />
19+
</HStack>
20+
);
21+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useSession } from "next-auth/react";
2+
import { get } from "src/lib/api";
3+
import { LeaderboardEntity, LeaderboardTimeFrame } from "src/types/Leaderboard";
4+
import uswSWRImmutable from "swr/immutable";
5+
6+
export const useUserScore = () => {
7+
const { status } = useSession();
8+
const isLoggedIn = status === "authenticated";
9+
const { data: entries } = uswSWRImmutable<Partial<{ [time in LeaderboardTimeFrame]: LeaderboardEntity }>>(
10+
isLoggedIn && "/api/user_stats",
11+
get,
12+
{
13+
dedupingInterval: 1000 * 60, // once per minute
14+
keepPreviousData: true,
15+
}
16+
);
17+
const score: number | undefined = entries?.total?.leader_score;
18+
return score;
19+
};

0 commit comments

Comments
 (0)