From 862b393d5beb729022891ee69c15736bcce8859a Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Fri, 22 Dec 2023 23:20:22 +0800 Subject: [PATCH] refactor: unify loading component (#1184) --- .../[subdomain]/comments/page-component.tsx | 15 ++---------- .../[subdomain]/editor/portfolio-editor.tsx | 5 ++-- .../[subdomain]/editor/post-editor.tsx | 5 ++-- .../[subdomain]/editor/short-editor.tsx | 5 ++-- .../[subdomain]/layout-component.tsx | 5 ++-- src/app/[locale]/dashboard/page.tsx | 6 ++--- src/components/common/CharacterList.tsx | 12 ++-------- src/components/common/Comment.tsx | 22 ++++-------------- src/components/common/Loading.tsx | 23 +++++++++++++++++++ src/components/common/PatronModal.tsx | 3 ++- src/components/dashboard/DualColumnEditor.tsx | 14 ++++------- src/components/dashboard/PagesManager.tsx | 3 ++- src/components/home/HomeFeed.tsx | 15 ++---------- src/components/home/Showcase.tsx | 4 +++- src/components/site/SiteHome.tsx | 4 +++- src/components/site/SiteSearch.tsx | 4 +++- src/components/ui/CodeMirror.tsx | 10 ++------ src/components/ui/XLogShorts.tsx | 3 ++- 18 files changed, 65 insertions(+), 93 deletions(-) create mode 100644 src/components/common/Loading.tsx diff --git a/src/app/[locale]/dashboard/[subdomain]/comments/page-component.tsx b/src/app/[locale]/dashboard/[subdomain]/comments/page-component.tsx index d636e391..1ca72b46 100644 --- a/src/app/[locale]/dashboard/[subdomain]/comments/page-component.tsx +++ b/src/app/[locale]/dashboard/[subdomain]/comments/page-component.tsx @@ -6,6 +6,7 @@ import { useEffect } from "react" import { Virtuoso } from "react-virtuoso" import { CommentItem } from "~/components/common/CommentItem" +import { Loading } from "~/components/common/Loading" import { DashboardMain } from "~/components/dashboard/DashboardMain" import { UniLink } from "~/components/ui/UniLink" import { getSiteLink } from "~/lib/helpers" @@ -65,7 +66,7 @@ export default function CommentsPage() { comments.hasNextPage && comments.fetchNextPage() } components={{ - Footer: comments.isLoading ? Loader : undefined, + Footer: comments.isLoading ? Loading : undefined, }} itemContent={(_, p) => p?.list?.map((comment, idx) => { @@ -125,15 +126,3 @@ export default function CommentsPage() { ) } - -const Loader = () => { - const t = useTranslations() - return ( -
- {t("Loading")}... -
- ) -} diff --git a/src/app/[locale]/dashboard/[subdomain]/editor/portfolio-editor.tsx b/src/app/[locale]/dashboard/[subdomain]/editor/portfolio-editor.tsx index 456c7bc2..c66b21dc 100644 --- a/src/app/[locale]/dashboard/[subdomain]/editor/portfolio-editor.tsx +++ b/src/app/[locale]/dashboard/[subdomain]/editor/portfolio-editor.tsx @@ -8,6 +8,7 @@ import toast from "react-hot-toast" import { useQueryClient } from "@tanstack/react-query" +import { Loading } from "~/components/common/Loading" import { DashboardMain } from "~/components/dashboard/DashboardMain" import EditorAutofill from "~/components/dashboard/editor-properties/EditorAutofill" import EditorCover from "~/components/dashboard/editor-properties/EditorCover" @@ -247,9 +248,7 @@ export default function PortfolioEditor() { <> {page.isLoading ? ( -
- {t("Loading")}... -
+ ) : ( <> diff --git a/src/app/[locale]/dashboard/[subdomain]/editor/post-editor.tsx b/src/app/[locale]/dashboard/[subdomain]/editor/post-editor.tsx index fe1c0d65..b054fb05 100644 --- a/src/app/[locale]/dashboard/[subdomain]/editor/post-editor.tsx +++ b/src/app/[locale]/dashboard/[subdomain]/editor/post-editor.tsx @@ -9,6 +9,7 @@ import toast from "react-hot-toast" import type { EditorView } from "@codemirror/view" import { useQueryClient } from "@tanstack/react-query" +import { Loading } from "~/components/common/Loading" import { DashboardMain } from "~/components/dashboard/DashboardMain" import DualColumnEditor from "~/components/dashboard/DualColumnEditor" import EditorCover from "~/components/dashboard/editor-properties/EditorCover" @@ -385,9 +386,7 @@ export default function PostEditor() { <> {page.isLoading ? ( -
- {t("Loading")}... -
+ ) : ( <>
{page.isLoading ? ( -
- {t("Loading")}... -
+ ) : ( <> diff --git a/src/app/[locale]/dashboard/[subdomain]/layout-component.tsx b/src/app/[locale]/dashboard/[subdomain]/layout-component.tsx index 7df66a7a..e0288636 100644 --- a/src/app/[locale]/dashboard/[subdomain]/layout-component.tsx +++ b/src/app/[locale]/dashboard/[subdomain]/layout-component.tsx @@ -16,6 +16,7 @@ import { } from "@crossbell/notification" import { ConnectButton } from "~/components/common/ConnectButton" +import { Loading } from "~/components/common/Loading" import { Logo } from "~/components/common/Logo" import { DashboardTopbar } from "~/components/dashboard/DashboardTopbar" import { Avatar } from "~/components/ui/Avatar" @@ -444,8 +445,6 @@ export default function DashboardLayout({ ) ) : ( -
- Loading... -
+ ) } diff --git a/src/app/[locale]/dashboard/page.tsx b/src/app/[locale]/dashboard/page.tsx index 68d105b3..3638ab01 100644 --- a/src/app/[locale]/dashboard/page.tsx +++ b/src/app/[locale]/dashboard/page.tsx @@ -1,12 +1,12 @@ +import { Loading } from "~/components/common/Loading" + import PageComponent from "./page-component" export default function Dashboard() { return ( <> -
- Loading... -
+ ) } diff --git a/src/components/common/CharacterList.tsx b/src/components/common/CharacterList.tsx index 615abfca..4f25852a 100644 --- a/src/components/common/CharacterList.tsx +++ b/src/components/common/CharacterList.tsx @@ -7,6 +7,7 @@ import { ExpandedCharacter } from "~/lib/types" import { Button } from "../ui/Button" import CharacterListItem from "./CharacterListItem" +import { Loading } from "./Loading" import { PortalProvider } from "./Portal" export const CharacterList = ({ @@ -57,7 +58,7 @@ export const CharacterList = ({ className="max-h-screen" endReached={() => hasMore && loadMore()} components={{ - Footer: hasMore ? Loader : undefined, + Footer: hasMore ? Loading : undefined, }} data={flattenList} itemContent={(index, sub) => { @@ -89,12 +90,3 @@ export const CharacterList = ({ ) } - -const Loader = () => { - const t = useTranslations() - return ( -
- {t("Loading")} ... -
- ) -} diff --git a/src/components/common/Comment.tsx b/src/components/common/Comment.tsx index 9f61b128..92c7d18a 100644 --- a/src/components/common/Comment.tsx +++ b/src/components/common/Comment.tsx @@ -11,6 +11,8 @@ import { ExpandedNote } from "~/lib/types" import { cn } from "~/lib/utils" import { useGetComments } from "~/queries/page" +import { Loading } from "./Loading" + const Comment = ({ page, className, @@ -64,7 +66,7 @@ const Comment = ({ data={data} endReached={() => comments.hasNextPage && comments.fetchNextPage()} components={{ - Footer: comments.isLoading ? Loader : undefined, + Footer: comments.isLoading ? Loading : undefined, }} totalListHeightChanged={(height) => { if (fixHeight && height > 0) { @@ -89,23 +91,7 @@ const Comment = ({ )) } > - {comments.isLoading && ( -
- {t("Loading")}... -
- )} - - ) -} - -const Loader = () => { - const t = useTranslations() - return ( -
- {t("Loading")}... + {comments.isLoading && }
) } diff --git a/src/components/common/Loading.tsx b/src/components/common/Loading.tsx new file mode 100644 index 00000000..83c7bf30 --- /dev/null +++ b/src/components/common/Loading.tsx @@ -0,0 +1,23 @@ +import { useTranslations } from "next-intl" + +import { cn } from "~/lib/utils" + +export const Loading = ({ + className, +}: { + className?: string + context?: unknown +}) => { + const t = useTranslations() + return ( +
+
+ {t("Loading")}... +
+ ) +} diff --git a/src/components/common/PatronModal.tsx b/src/components/common/PatronModal.tsx index 4e211daf..efd68fb9 100644 --- a/src/components/common/PatronModal.tsx +++ b/src/components/common/PatronModal.tsx @@ -17,6 +17,7 @@ import { ModalContentProps, useModalStack } from "../ui/ModalStack" import { Tabs } from "../ui/Tabs" import { UniLink } from "../ui/UniLink" import { CharacterFloatCard } from "./CharacterFloatCard" +import { Loading } from "./Loading" export const usePatronModal = () => { const { present } = useModalStack() @@ -179,7 +180,7 @@ const PatronModal = ({
{tips.isLoading ? ( - "Loading..." + ) : tips.data?.pages?.[0]?.list?.length ? ( <>
    diff --git a/src/components/dashboard/DualColumnEditor.tsx b/src/components/dashboard/DualColumnEditor.tsx index 11532b60..12178e13 100644 --- a/src/components/dashboard/DualColumnEditor.tsx +++ b/src/components/dashboard/DualColumnEditor.tsx @@ -13,15 +13,13 @@ import { useUploadFile } from "~/hooks/useUploadFile" import { cn } from "~/lib/utils" import { Rendered, renderPageContent } from "~/markdown" +import { Loading } from "../common/Loading" + const DynamicCodeMirrorEditor = dynamic( () => import("~/components/ui/CodeMirror"), { ssr: false, - loading: () => ( -
    - Loading... -
    - ), + loading: () => , }, ) @@ -29,11 +27,7 @@ const DynamicPageContent = dynamic( () => import("~/components/common/PageContent"), { ssr: false, - loading: () => ( -
    - Loading... -
    - ), + loading: () => , }, ) diff --git a/src/components/dashboard/PagesManager.tsx b/src/components/dashboard/PagesManager.tsx index 83bc6363..fe259da9 100644 --- a/src/components/dashboard/PagesManager.tsx +++ b/src/components/dashboard/PagesManager.tsx @@ -25,6 +25,7 @@ import { cn } from "~/lib/utils" import { useGetPagesBySite, usePinnedPage } from "~/queries/page" import { useGetSite } from "~/queries/site" +import { Loading } from "../common/Loading" import { PlatformsSyncMap } from "../site/Platform" import { Button } from "../ui/Button" import { EmptyState } from "../ui/EmptyState" @@ -317,7 +318,7 @@ export const PagesManager = ({ type }: { type: NoteType }) => { )}
    - {pages.isLoading &&

    {t("Loading")}...

    } + {pages.isLoading && } {!pages.isLoading && !pages.data?.pages?.[0].count && ( )} diff --git a/src/components/home/HomeFeed.tsx b/src/components/home/HomeFeed.tsx index 6b504b1b..50c11bbd 100644 --- a/src/components/home/HomeFeed.tsx +++ b/src/components/home/HomeFeed.tsx @@ -8,6 +8,7 @@ import { VirtuosoGrid } from "react-virtuoso" import { useAccountState, useConnectModal } from "@crossbell/connect-kit" import { Switch } from "@headlessui/react" +import { Loading } from "~/components/common/Loading" import PostCard from "~/components/common/PostCard" import { EmptyState } from "~/components/ui/EmptyState" import { Skeleton } from "~/components/ui/Skeleton" @@ -245,7 +246,7 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => { }} > - {feed.isFetching && feed.hasNextPage && isMounted && } + {feed.isFetching && feed.hasNextPage && isMounted && }
    )}
@@ -253,18 +254,6 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => { ) } -const Loader = () => { - const t = useTranslations() - return ( -
- {t("Loading")} ... -
- ) -} - const FeedSkeleton = () => { return ( s.handle)} /> - {showcaseSites.isLoading &&

{t("Loading")}...

} + {showcaseSites.isLoading && }
    - {!posts.data?.pages?.length && posts.isLoading && <>Loading...} + {!posts.data?.pages?.length && posts.isLoading && } { const t = useTranslations() @@ -24,7 +26,7 @@ export const SiteSearch = () => {

    {posts.data?.pages?.[0].count || "0"} {t("results")}

    - {posts.isLoading && <>{t("Loading")}...} + {posts.isLoading && } ) diff --git a/src/components/ui/CodeMirror.tsx b/src/components/ui/CodeMirror.tsx index 0f428f99..3ee5081d 100644 --- a/src/components/ui/CodeMirror.tsx +++ b/src/components/ui/CodeMirror.tsx @@ -50,13 +50,7 @@ import { useIsDark } from "~/hooks/useDarkMode" import { useGetState } from "~/hooks/useGetState" import { useIsUnmounted } from "~/hooks/useLifecycle" -const LoadingHolder = () => { - return ( -
    - Loading... -
    - ) -} +import { Loading } from "../common/Loading" interface XLogCodeMirrorEditorProps { value?: string @@ -229,7 +223,7 @@ const CodeMirrorEditor = forwardRef< (props.LoadingComponent ? ( createElement(props.LoadingComponent) ) : ( - + ))} ) diff --git a/src/components/ui/XLogShorts.tsx b/src/components/ui/XLogShorts.tsx index 6b1957d5..280e6c6d 100644 --- a/src/components/ui/XLogShorts.tsx +++ b/src/components/ui/XLogShorts.tsx @@ -8,6 +8,7 @@ import { cn, isMobileDevice } from "~/lib/utils" import { useGetPage } from "~/queries/page" import { useGetSite } from "~/queries/site" +import { Loading } from "../common/Loading" import { Time } from "../common/Time" import PostCover from "../home/PostCover" import { Avatar } from "./Avatar" @@ -53,7 +54,7 @@ const XLogShorts: FC = ({ slug, handle, url }) => { window.open(url, "_blank") }, [url]) - if (page.isLoading) return
    Loading...
    + if (page.isLoading) return if (!isShort) return {url}