diff --git a/public/locales/zh/dashboard.json b/public/locales/zh/dashboard.json index a17673c5..bd4040b5 100644 --- a/public/locales/zh/dashboard.json +++ b/public/locales/zh/dashboard.json @@ -167,5 +167,7 @@ "DNS Checking": "DNS 检查中", "DNS check passed.": "DNS 检查通过。", "DNS check failed.": "DNS 检查失败。", - "Recheck": "重新检查" + "Recheck": "重新检查", + "Latest": "最新", + "Following": "关注" } \ No newline at end of file diff --git a/public/locales/zh/index.json b/public/locales/zh/index.json index 4990112c..92a733fb 100644 --- a/public/locales/zh/index.json +++ b/public/locales/zh/index.json @@ -29,6 +29,8 @@ "Features": "特色", "Showcase": "展示柜", "Integration": "集成", + "Home": "首页", + "Activities": "动态", "Source Code": "源码", "Visit": "访问", "Easy to get started": "简单上手", diff --git a/src/components/main/MainFeed.tsx b/src/components/main/MainFeed.tsx new file mode 100644 index 00000000..a1d53c9e --- /dev/null +++ b/src/components/main/MainFeed.tsx @@ -0,0 +1,165 @@ +import Link from "next/link" +import { useDate } from "~/hooks/useDate" +import { EmptyState } from "../ui/EmptyState" +import { useRouter } from "next/router" +import { Image } from "~/components/ui/Image" +import { Button } from "~/components/ui/Button" +import { useTranslation } from "next-i18next" +import { useEffect, useState } from "react" +import { useGetFeed } from "~/queries/home" +import { CharacterFloatCard } from "~/components/common/CharacterFloatCard" +import { useAccountState } from "@crossbell/connect-kit" +import InfiniteScroll from "react-infinite-scroller" + +export const MainFeed: React.FC<{ + type?: "latest" | "recommend" | "following" +}> = ({ type }) => { + const currentCharacterId = useAccountState( + (s) => s.computed.account?.characterId, + ) + + const feed = useGetFeed({ + type: type, + characterId: currentCharacterId, + }) + + const router = useRouter() + const { t } = useTranslation(["common", "site"]) + const date = useDate() + + const [isMounted, setIsMounted] = useState(false) + + useEffect(() => { + setIsMounted(true) + }, []) + + let currentLength = 0 + + return ( + <> + + Loading ... + + } + > + {feed.isLoading ? ( +
Loading...
+ ) : !feed.data?.pages[0]?.count ? ( + + ) : ( +
+ {feed.data?.pages.map((posts) => + posts?.list.map((post) => { + currentLength++ + return ( +
+
+ +
+ + {post.character?.handle + + + {post.character?.metadata?.content?.name || + post.character?.handle} + +
+
+ · + +
+ +
+

+ {post.metadata?.content?.title} +

+
+ {!!post.metadata?.content?.tags?.filter( + (tag) => tag !== "post" && tag !== "page", + ).length && ( + + {post.metadata?.content?.tags + ?.filter( + (tag) => tag !== "post" && tag !== "page", + ) + .map((tag) => ( + { + e.preventDefault() + router.push(`/tag/${tag}`) + }} + > + #{tag} + + ))} + + )} +
+
+ {post.metadata?.content?.summary} + {post.metadata?.content?.summary && "..."} +
+
+ {post.metadata?.content.cover && ( +
+ cover +
+ )} + +
+ ) + }), + )} +
+ )} +
+ + ) +} diff --git a/src/components/main/MainLayout.tsx b/src/components/main/MainLayout.tsx index 959a4541..6aca5e44 100644 --- a/src/components/main/MainLayout.tsx +++ b/src/components/main/MainLayout.tsx @@ -9,20 +9,35 @@ import { SEOHead } from "../common/SEOHead" import { UniLink } from "../ui/UniLink" import { ConnectButton } from "../common/ConnectButton" import { getSiteLink } from "~/lib/helpers" -import { Link } from "react-scroll" import { useTranslation } from "next-i18next" import { Logo } from "~/components/common/Logo" +import { useRouter } from "next/router" +import { cn } from "~/lib/utils" + +const tabs = [ + { + name: "Home", + link: "/", + }, + { + name: "Activities", + link: "/activities", + }, + { + name: "Source Code", + link: GITHUB_LINK, + }, +] export function MainLayout({ children, title, - tabs, }: { children?: React.ReactNode title?: string - tabs?: string[] }) { const { t } = useTranslation("index") + const router = useRouter() return ( <> @@ -33,32 +48,30 @@ export function MainLayout({ />
-
+
xLog -
+
{tabs?.map((tab, index) => ( - - {t(tab)} - + {t(tab.name)} + ))} - - {t("Source Code")} -
diff --git a/src/lib/types.ts b/src/lib/types.ts index 37ca195d..a44b1367 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,5 +1,6 @@ import type { Note as UniNote, Profile as UniProfile } from "unidata.js" import { ReactElement } from "react" +import { NoteEntity } from "crossbell.js" export type Site = { id: string @@ -88,6 +89,18 @@ export type Note = UniNote & { views?: number } +export type ExpandedNote = NoteEntity & { + metadata: { + content: { + summary?: string + cover?: string + frontMatter?: Record + slug?: string + views?: number + } + } +} + export type Notes = { total: number list: Note[] diff --git a/src/models/home.model.ts b/src/models/home.model.ts new file mode 100644 index 00000000..c75f71d1 --- /dev/null +++ b/src/models/home.model.ts @@ -0,0 +1,104 @@ +import { ExpandedNote } from "~/lib/types" +import { indexer } from "@crossbell/indexer" + +const expandPage = async (page: ExpandedNote, useStat?: boolean) => { + if (page.metadata?.content) { + if (page.metadata?.content?.content) { + const { renderPageContent } = await import("~/markdown") + const rendered = renderPageContent(page.metadata.content.content, true) + if (!page.metadata.content.summary) { + page.metadata.content.summary = rendered.excerpt + } + page.metadata.content.cover = rendered.cover + if (page.metadata) { + page.metadata.content.frontMatter = rendered.frontMatter + } + } + page.metadata.content.slug = encodeURIComponent( + page.metadata.content.attributes?.find( + (a) => a.trait_type === "xlog_slug", + )?.value || "", + ) + + if (useStat) { + const stat = await ( + await fetch( + `https://indexer.crossbell.io/v1/stat/notes/${page.characterId}/${page.noteId}`, + ) + ).json() + page.metadata.content.views = stat.viewDetailCount + } + } + + return page +} + +export async function getFeed({ + type, + cursor, + limit = 10, + characterId, +}: { + type?: "latest" | "recommend" | "following" + cursor?: string + limit?: number + characterId?: number +}) { + switch (type) { + case "latest": + const result = await indexer.getNotes({ + sources: "xlog", + tags: ["post"], + limit, + cursor, + includeCharacter: true, + }) + + const list = await Promise.all( + result.list.map(async (page: any) => { + return expandPage(page, true) + }), + ) + + return { + list, + cursor: result.cursor, + count: result.count, + } + case "following": + if (!characterId) { + return { + list: [], + cursor: "", + count: 0, + } + } else { + const result = await indexer.getFollowingFeedsOfCharacter(characterId, { + // sources: "xlog", + // tags: ["post"], + limit: limit * 2, + cursor, + type: ["POST_NOTE"], + }) + + const list = await Promise.all( + result.list + .filter((page) => { + return ( + page.note?.metadata?.content?.sources?.includes("xlog") && + page.note?.metadata?.content?.tags?.includes("post") + ) + }) + .map(async (page: any) => { + return expandPage(page.note, true) + }), + ) + + return { + list, + cursor: result.cursor, + count: result.count, + } + } + } +} diff --git a/src/pages/activities.tsx b/src/pages/activities.tsx new file mode 100644 index 00000000..98f2e99b --- /dev/null +++ b/src/pages/activities.tsx @@ -0,0 +1,168 @@ +import { GetServerSideProps } from "next" +import { ReactElement, useState } from "react" +import { MainLayout } from "~/components/main/MainLayout" +import { UniLink } from "~/components/ui/UniLink" +import { Button } from "~/components/ui/Button" +import { useConnectedAction } from "@crossbell/connect-kit" +import { useRefCallback } from "@crossbell/util-hooks" +import { getSiteLink } from "~/lib/helpers" +import { Image } from "~/components/ui/Image" +import { dehydrate, QueryClient } from "@tanstack/react-query" +import { prefetchGetSites } from "~/queries/site.server" +import { useGetSites } from "~/queries/site" +import showcase from "../../showcase.json" +import { CharacterFloatCard } from "~/components/common/CharacterFloatCard" +import { useAccountSites, useSubscribeToSites } from "~/queries/site" +import { useTranslation } from "next-i18next" +import { serverSideTranslations } from "next-i18next/serverSideTranslations" +import { languageDetector } from "~/lib/language-detector" +import { MainFeed } from "~/components/main/MainFeed" +import { Tabs } from "~/components/ui/Tabs" + +export const getServerSideProps: GetServerSideProps = async (ctx) => { + const queryClient = new QueryClient() + await prefetchGetSites(showcase, queryClient) + + return { + props: { + ...(await serverSideTranslations(languageDetector(ctx), [ + "common", + "index", + "dashboard", + ])), + dehydratedState: dehydrate(queryClient), + }, + } +} + +function Activities() { + const showcaseSites = useGetSites(showcase) + const { t } = useTranslation("index") + + const userSite = useAccountSites() + const subscribeToSites = useSubscribeToSites() + + const doSubscribeToSites = useRefCallback(() => { + subscribeToSites.mutate({ + characterIds: showcaseSites.data + ?.map((s: { characterId?: string }) => s.characterId) + .filter(Boolean) + .map(Number), + siteIds: showcaseSites.data?.map((s: { handle: string }) => s.handle), + } as any) + }) + + const followAll = useConnectedAction(() => { + doSubscribeToSites() + }) + + const [showcaseMore, setShowcaseMore] = useState(false) + + const [feedType, setFeedType] = useState<"latest" | "following">( + userSite.data?.length ? "following" : "latest", + ) + const tabs = [ + { + text: "Latest", + onClick: () => setFeedType("latest"), + active: feedType === "latest", + }, + { + text: "Following", + onClick: () => setFeedType("following"), + active: feedType === "following", + }, + ] + + return ( +
+
+
+ + +
+
+
+
+

+ {t( + "Discover these awesome teams and creators on xLog (sorted by update time)", + )} +

+ +
    +
    setShowcaseMore(true)} + > + {t("Show more")} +
    + {showcaseSites.data?.map((site: any) => ( +
  • + + + + {site.handle} + + + + + {site.metadata.content?.name} + + {site.metadata.content?.bio && ( + + {site.metadata.content?.bio} + + )} + + +
  • + ))} +
+
+
+
+
+
+ ) +} + +Activities.getLayout = (page: ReactElement) => { + return {page} +} + +export default Activities diff --git a/src/pages/index.tsx b/src/pages/index.tsx index e3094f3f..791e950a 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,5 +1,5 @@ import { GetServerSideProps } from "next" -import { useState } from "react" +import { ReactElement, useState } from "react" import { MainLayout } from "~/components/main/MainLayout" import { UniLink } from "~/components/ui/UniLink" import { Button } from "~/components/ui/Button" @@ -45,7 +45,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { } } -export default function Home() { +function Home() { const isConnected = useAccountState((s) => !!s.computed.account) const router = useRouter() const showcaseSites = useGetSites(showcase) @@ -249,122 +249,164 @@ export default function Home() { const [showcaseMore, setShowcaseMore] = useState(false) return ( - -
-
-
-

- {features.map((feature) => ( - - {t(feature.title)} - {t(".")} - - ))} -

-

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

-
- - -
-
- {t("Explore the xLog way")} +
+
+

+ {features.map((feature) => ( + + {t(feature.title)} + {t(".")} + + ))} +

+

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

+
+ +
-
-
- - {features.map((feature, index) => ( -
-
-
-
- {index + 1} -
-

- {t(feature.title)} -

-

- {t(`features.${feature.title}.subtitle`)} -

-

+ onClick={() => { + setShowcaseMore(true) + }} + > + {t("Look at others'")} + + + +

+
+ {t("Explore the xLog way")} + +
+
+
+ + {features.map((feature, index) => ( +
+
+
+
+ {index + 1} +
+

+ {t(feature.title)} +

+

+ {t(`features.${feature.title}.subtitle`)} +

+

+ + . + + ), + }} + ns="index" + /> +

+
+
    + {feature.subfeatures.map((item, index) => ( +
  • + {item.screenshot?.src && ( +
    + {item.title} +
    + )} +

    + {item.icon} + + {t(item.title)} + +

    +

    , + axfm: ( . @@ -373,238 +415,188 @@ export default function Home() { ns="index" />

    +
  • + ))} +
+ {feature.extra && ( +
+
+ 🤫{" "} + + {t(`features.${feature.title}.extra.title`)} +
-
    - {feature.subfeatures.map((item, index) => ( -
  • - {item.screenshot?.src && ( -
    - {item.title} -
    - )} -

    - {item.icon} - - {t(item.title)} - -

    -

    - , - axfm: ( - - . - - ), - }} - ns="index" - /> -

    -
  • - ))} -
- {feature.extra && ( -
-
- 🤫{" "} - - {t(`features.${feature.title}.extra.title`)} - -
-
- {t(`features.${feature.title}.extra.description`)} -
- -
- )} -
- ))} - - -
- {t("Showcase")} -
-
-

- {t( - "Discover these awesome teams and creators on xLog (sorted by update time)", - )} -

- -
    -
    setShowcaseMore(true)} +
    {t(`features.${feature.title}.extra.description`)}
    +
    - {showcaseSites.data?.map((site: any) => ( -
  • - - - - {site.handle} - - - - - {site.metadata.content?.name} - - {site.metadata.content?.bio && ( - - {site.metadata.content?.bio} - - )} - - -
  • - ))} -
  • - - {t("Submit yours")} - -
  • -
-
-
- -
- {t("Integration")} -
-
-

- {t( - "xLog's open design allows it to integrate with many other open protocols and applications without friction.", - )} -

-
    - {integrations.map((item, index) => ( -
  • - {item.url ? ( - -
    - {item.icon} -
    -
    - {item.name} -
    -
    - ) : ( - -
    -
    - {item.icon} -
    -
    - {item.name} -
    -
    -
    - )} -
  • - ))} -
  • - -
  • -
-
-
-
-
-
- + {t(`features.${feature.title}.extra.button`)} + +
+ )}
- {isConnected ? ( - - - - ) : ( - - )} + ))} +
+ +
+ {t("Showcase")}
+
+

+ {t( + "Discover these awesome teams and creators on xLog (sorted by update time)", + )} +

+ +
    +
    setShowcaseMore(true)} + > + {t("Show more")} +
    + {showcaseSites.data?.map((site: any) => ( +
  • + + + + {site.handle} + + + + + {site.metadata.content?.name} + + {site.metadata.content?.bio && ( + + {site.metadata.content?.bio} + + )} + + +
  • + ))} +
+
+
+ +
+ {t("Integration")} +
+
+

+ {t( + "xLog's open design allows it to integrate with many other open protocols and applications without friction.", + )} +

+
    + {integrations.map((item, index) => ( +
  • + {item.url ? ( + +
    + {item.icon} +
    +
    + {item.name} +
    +
    + ) : ( + +
    +
    + {item.icon} +
    +
    + {item.name} +
    +
    +
    + )} +
  • + ))} +
  • + +
  • +
+
+
+
+
+
+
-
-
+ {isConnected ? ( + + + + ) : ( + + )} + + ) } + +Home.getLayout = (page: ReactElement) => { + return {page} +} + +export default Home diff --git a/src/queries/home.ts b/src/queries/home.ts new file mode 100644 index 00000000..fa3242b3 --- /dev/null +++ b/src/queries/home.ts @@ -0,0 +1,20 @@ +import { useInfiniteQuery } from "@tanstack/react-query" + +import * as homeModel from "~/models/home.model" + +export const useGetFeed = (data?: { + type?: "latest" | "recommend" | "following" + characterId?: number + limit?: number +}) => { + return useInfiniteQuery({ + queryKey: ["getFeed", data], + queryFn: async ({ pageParam }) => { + return homeModel.getFeed({ + ...data, + cursor: pageParam, + }) + }, + getNextPageParam: (lastPage) => lastPage?.cursor || undefined, + }) +}