diff --git a/pcweb/components/docpage/navbar/buttons/github.py b/pcweb/components/docpage/navbar/buttons/github.py index 7e8d2183c1..a6da2e5891 100644 --- a/pcweb/components/docpage/navbar/buttons/github.py +++ b/pcweb/components/docpage/navbar/buttons/github.py @@ -1,6 +1,7 @@ import reflex as rx -from pcweb.constants import GITHUB_STARS, GITHUB_URL +from pcweb.constants import GITHUB_URL from pcweb.components.icons.icons import get_icon +from pcweb.github import GithubStarState def github() -> rx.Component: @@ -8,7 +9,7 @@ def github() -> rx.Component: rx.flex( get_icon(icon="github_navbar", class_name="shrink-0 !text-slate-9"), rx.text( - f"{GITHUB_STARS/1000:.1f}K", + GithubStarState.stars, class_name="font-small", ), class_name="text-slate-9 flex-row gap-2 hover:bg-slate-3 flex justify-center rounded-[10px] border border-slate-5 bg-slate-1 transition-bg cursor-pointer shadow-large py-0.5 px-3 items-center h-8", diff --git a/pcweb/github.py b/pcweb/github.py new file mode 100644 index 0000000000..fcace5c671 --- /dev/null +++ b/pcweb/github.py @@ -0,0 +1,30 @@ +"""Github stars count for the reflex repository.""" + +import asyncio +import httpx + +import reflex as rx +import contextlib + +REFLEX_STAR_COUNT = 0 +GITHUB_API_URL = "https://api.github.com/repos/reflex-dev/reflex" + + +async def fetch_count(): + """Fetch the stars count of the reflex repository.""" + try: + while True: + with contextlib.suppress(Exception): + global REFLEX_STAR_COUNT + data = httpx.get(GITHUB_API_URL).json() + count = int(data["stargazers_count"]) + REFLEX_STAR_COUNT = round(count / 1000) + await asyncio.sleep(3600) + except asyncio.CancelledError: + pass + + +class GithubStarState(rx.State): + @rx.var(cache=True, interval=60) + def stars(self) -> str: + return f"{REFLEX_STAR_COUNT}K" diff --git a/pcweb/pcweb.py b/pcweb/pcweb.py index 5faecd6890..5fea77826c 100644 --- a/pcweb/pcweb.py +++ b/pcweb/pcweb.py @@ -5,6 +5,7 @@ import reflex as rx from pcweb import styles +from pcweb.github import fetch_count from pcweb.pages import page404, routes from pcweb.pages.docs import outblocks, exec_blocks from pcweb.whitelist import _check_whitelisted_path @@ -121,8 +122,10 @@ ("/docs/tutorial/adding-state", "/docs/getting-started/chatapp-tutorial"), ("/docs/tutorial/final-app", "/docs/getting-started/chatapp-tutorial"), ("/docs/getting-started/configuration", "/docs/advanced-onboarding/configuration"), - ("/docs/getting-started/how-reflex-works", "/docs/advanced-onboarding/how-reflex-works"), - + ( + "/docs/getting-started/how-reflex-works", + "/docs/advanced-onboarding/how-reflex-works", + ), # Recipes ("/docs/recipes/auth", "/docs/recipes"), ("/docs/recipes/auth", "/docs/recipes"), @@ -135,6 +138,9 @@ ] for source, target in redirects: - app.add_page(lambda: rx.fragment(), route=source, on_load=rx.redirect(target)) + if _check_whitelisted_path(target): + app.add_page(lambda: rx.fragment(), route=source, on_load=rx.redirect(target)) app.add_custom_404_page(page404.component) + +app.register_lifespan_task(fetch_count)