Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Stats page #3531

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 65 additions & 12 deletions frontend/pages/statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { HiCloudArrowDown, HiCalendar, HiListBullet } from "react-icons/hi2"
import ListBox from "../src/components/application/ListBox"
import { i18n, useTranslation } from "next-i18next"
import { useTheme } from "next-themes"
import { getIntlLocale, registerIsoCountriesLocales } from "../src/localize"
import {
getIntlLocale,
getLanguageFlag,
getLanguageName,
getLocale,
Language,
registerIsoCountriesLocales,
} from "../src/localize"
import { Category, categoryToName } from "src/types/Category"
import { useRouter } from "next/router"
import { useQuery } from "@tanstack/react-query"
Expand All @@ -24,14 +31,33 @@ import {
RotatedAxisTick,
FlathubTooltip,
} from "src/chartComponents"
import { useState } from "react"
import { useEffect, useRef, useState } from "react"
import { ChartContainer, ChartConfig } from "@/components/ui/chart"
import ReactCountryFlag from "react-country-flag"

const countries = registerIsoCountriesLocales()

const DownloadsPerCountry = ({ stats }: { stats: StatsResult }) => {
const { t } = useTranslation()

const ref = useRef<HTMLDivElement>(null)

const [width, setWidth] = useState(0)

const updateDimensions = () => {
if (ref.current) setWidth(ref.current.clientWidth)
}

useEffect(() => {
if (ref?.current) {
window.addEventListener("resize", updateDimensions)
setWidth(ref.current.offsetWidth)
}
return () => {
window.removeEventListener("resize", updateDimensions)
}
}, [ref, ref.current?.offsetWidth, setWidth])

let country_data: { country: string; value: number }[] = []
if (stats.countries) {
for (const [key, value] of Object.entries(stats.countries)) {
Expand Down Expand Up @@ -67,16 +93,43 @@ const DownloadsPerCountry = ({ stats }: { stats: StatsResult }) => {
<h2 className="mb-6 mt-12 text-2xl font-bold">
{t("downloads-per-country")}
</h2>
<div className={`flex justify-center ${styles.map}`}>
<WorldMap
color="hsl(var(--color-primary))"
backgroundColor="hsl(var(--bg-color-secondary))"
borderColor="hsl(var(--text-primary))"
size="responsive"
data={country_data}
tooltipTextFunction={getLocalizedText}
rtl={i18n.dir() === "rtl"}
/>
<div className="flex" ref={ref}>
<div
className={`flex justify-center items-center gap-5 ${styles.map}`}
>
<WorldMap
color="hsl(var(--color-primary))"
backgroundColor="hsl(var(--bg-color-secondary))"
borderColor="hsl(var(--text-primary))"
size={width / 2}
data={country_data}
tooltipTextFunction={getLocalizedText}
rtl={i18n.dir() === "rtl"}
/>
<div className="overflow-y-auto max-h-[500px] flex flex-col rounded-xl bg-flathub-white p-4 shadow-md dark:bg-flathub-arsenic">
{country_data
.toSorted((a, b) => b.value - a.value)
.map(({ country, value }, i) => {
const translatedCountryName = countries.getName(
country,
i18n.language,
)
return (
<div
key={country}
className="flex gap-4 items-center justify-between px-4 py-2"
>
<div className="text-lg font-semibold">{i + 1}.</div>
<div className="flex gap-2 items-center">
<ReactCountryFlag countryCode={country} />
<div>{translatedCountryName}</div>
</div>
<div>{value.toLocaleString(i18n.language)}</div>
</div>
)
})}
</div>
</div>
</div>
</>
)
Expand Down
Loading