From 091c11847ba3a4e684059326a1e49e71d0db2657 Mon Sep 17 00:00:00 2001 From: enpitsulin Date: Sun, 7 May 2023 00:47:00 +0800 Subject: [PATCH] fix: i18n in app router (#496) --- src/app/(home)/components/EntranceButton.tsx | 19 +- src/app/(home)/components/Header.tsx | 17 +- src/app/(home)/components/Integrations.tsx | 142 +++++++++ src/app/(home)/components/Showcase.tsx | 24 ++ .../(home)/{ => components}/ShowcaseList.tsx | 0 src/app/(home)/layout.tsx | 1 + src/app/(home)/page.tsx | 277 ++++-------------- src/app/layout.tsx | 15 +- src/components/common/FollowAllButton.tsx | 2 + src/components/ui/Tooltip.tsx | 2 + src/hooks/useAcceptLang.ts | 19 ++ src/pages/{_app.tsx => _app_b.tsx} | 0 12 files changed, 274 insertions(+), 244 deletions(-) create mode 100644 src/app/(home)/components/Integrations.tsx create mode 100644 src/app/(home)/components/Showcase.tsx rename src/app/(home)/{ => components}/ShowcaseList.tsx (100%) create mode 100644 src/hooks/useAcceptLang.ts rename src/pages/{_app.tsx => _app_b.tsx} (100%) diff --git a/src/app/(home)/components/EntranceButton.tsx b/src/app/(home)/components/EntranceButton.tsx index 57aa745d..b606e0cb 100644 --- a/src/app/(home)/components/EntranceButton.tsx +++ b/src/app/(home)/components/EntranceButton.tsx @@ -1,12 +1,18 @@ "use client" import { useRouter } from "next/navigation" +import React from "react" import { useAccountState } from "@crossbell/connect-kit" import { Button } from "~/components/ui/Button" -export default function EntranceButton() { +interface Props { + connectedContent: React.ReactNode + unconnectedContent: React.ReactNode +} + +export default function EntranceButton(props: Props) { const router = useRouter() const isConnected = useAccountState((s) => !!s.computed.account) @@ -17,16 +23,7 @@ export default function EntranceButton() { size="2xl" variantColor="black" > - {isConnected ? ( - <> - - {/* {t("Dashboard")} */} - Dashboard - - ) : ( - // t("Get my xLog in 5 minutes") - <>Get my xLog in 5 minutes - )} + {isConnected ? props.connectedContent : props.unconnectedContent} ) } diff --git a/src/app/(home)/components/Header.tsx b/src/app/(home)/components/Header.tsx index 62687764..9b7fb2d5 100644 --- a/src/app/(home)/components/Header.tsx +++ b/src/app/(home)/components/Header.tsx @@ -1,13 +1,10 @@ -"use client" - -import { usePathname } from "next/navigation" -import { useTranslation } from "react-i18next" - import { ConnectButton } from "~/components/common/ConnectButton" import { Logo } from "~/components/common/Logo" import { Image } from "~/components/ui/Image" import { UniLink } from "~/components/ui/UniLink" +import { useAcceptLang } from "~/hooks/useAcceptLang" import { GITHUB_LINK } from "~/lib/env" +import { useTranslation } from "~/lib/i18n" import { cn } from "~/lib/utils" const tabs = [ @@ -31,9 +28,13 @@ const tabs = [ }, ] -export function Header() { - const { t } = useTranslation("index") - const pathname = usePathname() +export async function Header() { + const lang = useAcceptLang() + const { t } = await useTranslation(lang, "index") + // const pathname = usePathname() + // TODO how to set active state for Link in RSC, + // make be client component and add activeClass props or some way else + const pathname = "/" return (
diff --git a/src/app/(home)/components/Integrations.tsx b/src/app/(home)/components/Integrations.tsx new file mode 100644 index 00000000..f332a568 --- /dev/null +++ b/src/app/(home)/components/Integrations.tsx @@ -0,0 +1,142 @@ +"use client" + +import { + CrossbellChainLogo, + XCharLogo, + XFeedLogo, + XShopLogo, + XSyncLogo, +} from "@crossbell/ui" +import { RssIcon } from "@heroicons/react/24/outline" + +import { Image } from "~/components/ui/Image" +import { Tooltip } from "~/components/ui/Tooltip" +import { UniLink } from "~/components/ui/UniLink" +import { getSiteLink } from "~/lib/helpers" + +export function Integration() { + const integrations = [ + { + name: "RSS", + icon: , + url: + getSiteLink({ + subdomain: "xlog", + }) + "/feed?format=xml", + }, + { + name: "JSON Feed", + icon: JSON Feed, + url: + getSiteLink({ + subdomain: "xlog", + }) + "/feed", + }, + { + name: "xChar", + icon: , + url: "https://xchar.app/", + }, + { + name: "xFeed", + icon: , + url: "https://crossbell.io/feed", + }, + { + name: "xSync", + icon: , + url: "https://xsync.app/", + }, + { + name: "xShop", + icon: , + text: "Coming soon", + }, + { + name: "Crossbell Scan", + icon: , + url: "https://scan.crossbell.io/", + }, + { + name: "Crossbell Faucet", + icon: , + url: "https://faucet.crossbell.io/", + }, + { + name: "Crossbell Export", + icon: , + url: "https://export.crossbell.io/", + }, + { + name: "Crossbell SDK", + icon: , + url: "https://crossbell-box.github.io/crossbell.js/", + }, + { + name: "RSS3", + icon: ( + RSS3 + ), + url: "https://rss3.io/", + }, + { + name: "Hoot It", + icon: ( + Hoot It + ), + url: "https://hoot.it/search/xLog", + }, + { + name: "Raycast", + icon: Raycast, + url: "https://www.raycast.com/Songkeys/crossbell", + }, + { + name: "Obsidian", + icon: ( + Obsidian + ), + text: "Coming soon", + }, + ] + return ( + <> + {integrations.map((item, index) => ( +
  • + {item.url ? ( + +
    + {item.icon} +
    +
    + {item.name} +
    +
    + ) : ( + +
    +
    + {item.icon} +
    +
    + {item.name} +
    +
    +
    + )} +
  • + ))} + + ) +} diff --git a/src/app/(home)/components/Showcase.tsx b/src/app/(home)/components/Showcase.tsx new file mode 100644 index 00000000..7f105da6 --- /dev/null +++ b/src/app/(home)/components/Showcase.tsx @@ -0,0 +1,24 @@ +"use client" + +import { FollowAllButton } from "~/components/common/FollowAllButton" +import { useGetShowcase } from "~/queries/home" + +import ShowcaseList from "./ShowcaseList" + +export function ShowCase() { + const showcaseSites = useGetShowcase() + return ( + <> + s.characterId) + .filter(Boolean) + .map(Number)} + siteIds={showcaseSites.data?.map((s: { handle: string }) => s.handle)} + /> + + + ) +} diff --git a/src/app/(home)/ShowcaseList.tsx b/src/app/(home)/components/ShowcaseList.tsx similarity index 100% rename from src/app/(home)/ShowcaseList.tsx rename to src/app/(home)/components/ShowcaseList.tsx diff --git a/src/app/(home)/layout.tsx b/src/app/(home)/layout.tsx index e47e26af..daba730b 100644 --- a/src/app/(home)/layout.tsx +++ b/src/app/(home)/layout.tsx @@ -12,6 +12,7 @@ export default async function HomeLayout({ }) { return ( <> + {/* @ts-expect-error RSC */}
    {children}
    diff --git a/src/app/(home)/page.tsx b/src/app/(home)/page.tsx index 3758ef1e..3b6971b5 100644 --- a/src/app/(home)/page.tsx +++ b/src/app/(home)/page.tsx @@ -1,39 +1,36 @@ -"use client" +import Link from "next/link" +import { memo } from "react" +import { Trans } from "react-i18next/TransWithoutContext" -import { Trans, useTranslation } from "next-i18next" -import { useRouter } from "next/navigation" -import { Element, Link } from "react-scroll" - -import { - CrossbellChainLogo, - XCharLogo, - XFeedLogo, - XShopLogo, - XSyncLogo, -} from "@crossbell/ui" -import { RssIcon } from "@heroicons/react/24/outline" - -import { FollowAllButton } from "~/components/common/FollowAllButton" import { Logo } from "~/components/common/Logo" import { Button } from "~/components/ui/Button" import { Image } from "~/components/ui/Image" -import { Tooltip } from "~/components/ui/Tooltip" -import { UniLink } from "~/components/ui/UniLink" +import { useAcceptLang } from "~/hooks/useAcceptLang" import { CSB_SCAN, GITHUB_LINK } from "~/lib/env" import { getSiteLink } from "~/lib/helpers" -import { useGetShowcase } from "~/queries/home" +import { useTranslation } from "~/lib/i18n" -import ShowcaseList from "./ShowcaseList" -import EntranceButton from "./components/EntranceButton" +import { default as OriginEntranceButton } from "./components/EntranceButton" +import { Integration } from "./components/Integrations" +import { ShowCase } from "./components/Showcase" -function Home() { - const router = useRouter() - const showcaseSites = useGetShowcase() - const { t } = useTranslation("index") +async function Home() { + const lang = useAcceptLang() + const { t, i18n } = await useTranslation(lang, "index") - const tryNow = () => { - router.push("/dashboard") - } + const EntranceButton = memo(function Button() { + return ( + + + {t("Dashboard")} + + } + unconnectedContent={t("Get my xLog in 5 minutes")} + /> + ) + }) const features: { title: string @@ -114,96 +111,6 @@ function Home() { }, ] - const integrations = [ - { - name: "RSS", - icon: , - url: - getSiteLink({ - subdomain: "xlog", - }) + "/feed?format=xml", - }, - { - name: "JSON Feed", - icon: JSON Feed, - url: - getSiteLink({ - subdomain: "xlog", - }) + "/feed", - }, - { - name: "xChar", - icon: , - url: "https://xchar.app/", - }, - { - name: "xFeed", - icon: , - url: "https://crossbell.io/feed", - }, - { - name: "xSync", - icon: , - url: "https://xsync.app/", - }, - { - name: "xShop", - icon: , - text: "Coming soon", - }, - { - name: "Crossbell Scan", - icon: , - url: "https://scan.crossbell.io/", - }, - { - name: "Crossbell Faucet", - icon: , - url: "https://faucet.crossbell.io/", - }, - { - name: "Crossbell Export", - icon: , - url: "https://export.crossbell.io/", - }, - { - name: "Crossbell SDK", - icon: , - url: "https://crossbell-box.github.io/crossbell.js/", - }, - { - name: "RSS3", - icon: ( - RSS3 - ), - url: "https://rss3.io/", - }, - { - name: "Hoot It", - icon: ( - Hoot It - ), - url: "https://hoot.it/search/xLog", - }, - { - name: "Raycast", - icon: Raycast, - url: "https://www.raycast.com/Songkeys/crossbell", - }, - { - name: "Obsidian", - icon: ( - Obsidian - ), - text: "Coming soon", - }, - ] - return (
    @@ -219,21 +126,25 @@ function Home() { ))}

    - + xLog is the best{" "} - on-chain - {" "} + {" "} and{" "} - open-source - {" "} + {" "} blogging community for everyone.

    @@ -246,25 +157,20 @@ function Home() { variantColor="gradient" > - {t("Look at others'")} + {t("Look at others'")}
    {t("Explore the xLog way")} - {/* @ts-expect-error: TODO fix or replace this lib */} - + >
    - {/* @ts-expect-error: TODO fix or replace this lib */} - +
    {features.map((feature, index) => (
    @@ -291,15 +197,18 @@ function Home() {

    . - + ), }} ns="index" @@ -332,28 +241,33 @@ function Home() {

    , axfm: ( - . - + ), aincentive: ( - . - + ), }} ns="index" @@ -373,25 +287,20 @@ function Home() {

    {t(`features.${feature.title}.extra.description`)}
    - +
    )}
    ))} -
    - {/* @ts-expect-error: TODO fix or replace this lib */} - +
    +
    {t("Showcase")}
    @@ -401,22 +310,10 @@ function Home() { "Discover these awesome teams and creators on xLog (sorted by update time)", )}

    - s.characterId) - .filter(Boolean) - .map(Number)} - siteIds={showcaseSites.data?.map( - (s: { handle: string }) => s.handle, - )} - /> - +
    - - {/* @ts-expect-error: TODO fix or replace this lib */} - +
    +
    {t("Integration")}
    @@ -427,62 +324,18 @@ function Home() { )}

      - {integrations.map((item, index) => ( -
    • - {item.url ? ( - -
      - {item.icon} -
      -
      - {item.name} -
      -
      - ) : ( - -
      -
      - {item.icon} -
      -
      - {item.name} -
      -
      -
      - )} -
    • - ))} +
    - +
    - {/* {isConnected ? ( - - - - ) : ( - - )} */}
    diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 37e53b1d..29cf2802 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,10 +1,9 @@ import { dir } from "i18next" import { Metadata } from "next" -import { headers } from "next/headers" import "~/css/main.css" +import { useAcceptLang } from "~/hooks/useAcceptLang" import { APP_DESCRIPTION, APP_NAME, SITE_URL } from "~/lib/env" -import { fallbackLng } from "~/lib/i18n/settings" import Providers from "./providers" @@ -28,17 +27,7 @@ export default function RootLayout({ }: { children: React.ReactNode }) { - let lng = fallbackLng - - let acceptLang = headers().get("accept-language")?.split(",")[0] - if (acceptLang === "zh-HK" || acceptLang === "zh-TW") { - acceptLang = "zh-TW" - } else { - acceptLang = acceptLang?.split("-")[0] - } - if (acceptLang) { - lng = acceptLang - } + const lng = useAcceptLang() return ( diff --git a/src/components/common/FollowAllButton.tsx b/src/components/common/FollowAllButton.tsx index 779c9fa4..1eac7ea2 100644 --- a/src/components/common/FollowAllButton.tsx +++ b/src/components/common/FollowAllButton.tsx @@ -1,3 +1,5 @@ +"use client" + import { useTranslation } from "next-i18next" import { useState } from "react" diff --git a/src/components/ui/Tooltip.tsx b/src/components/ui/Tooltip.tsx index a3523446..084a6318 100644 --- a/src/components/ui/Tooltip.tsx +++ b/src/components/ui/Tooltip.tsx @@ -1,3 +1,5 @@ +"use client" + import { useState } from "react" import { diff --git a/src/hooks/useAcceptLang.ts b/src/hooks/useAcceptLang.ts new file mode 100644 index 00000000..eb3dc951 --- /dev/null +++ b/src/hooks/useAcceptLang.ts @@ -0,0 +1,19 @@ +import { headers } from "next/headers" + +import { fallbackLng } from "~/lib/i18n/settings" + +export function useAcceptLang() { + let lng = fallbackLng + + let acceptLang = headers().get("accept-language")?.split(",")[0] + if (acceptLang === "zh-HK" || acceptLang === "zh-TW") { + acceptLang = "zh-TW" + } else { + acceptLang = acceptLang?.split("-")[0] + } + if (acceptLang) { + lng = acceptLang + } + + return lng +} diff --git a/src/pages/_app.tsx b/src/pages/_app_b.tsx similarity index 100% rename from src/pages/_app.tsx rename to src/pages/_app_b.tsx