From 3b18b29e6a842a250368258dbc86f73bdcb55f56 Mon Sep 17 00:00:00 2001 From: Birdgg Date: Tue, 23 May 2023 02:26:06 +0800 Subject: [PATCH] feat: add skeleton loading for home feed (#574) --- src/components/common/CharacterCard.tsx | 11 +++- src/components/home/HomeFeed.tsx | 19 +++++- src/components/ui/Skeleton.tsx | 87 +++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 src/components/ui/Skeleton.tsx diff --git a/src/components/common/CharacterCard.tsx b/src/components/common/CharacterCard.tsx index db108847..4d0be64b 100644 --- a/src/components/common/CharacterCard.tsx +++ b/src/components/common/CharacterCard.tsx @@ -2,6 +2,7 @@ import { FollowingButton } from "~/components/common/FollowingButton" import { FollowingCount } from "~/components/common/FollowingCount" import { Titles } from "~/components/common/Titles" import { Avatar } from "~/components/ui/Avatar" +import { Skeleton } from "~/components/ui/Skeleton" import { useDate } from "~/hooks/useDate" import { useTranslation } from "~/lib/i18n/client" import { cn } from "~/lib/utils" @@ -97,7 +98,15 @@ export const CharacterCard: React.FC<{ )} ) : ( - <>{t("Loading")}... + +
+ + +
+ + + +
)} ) diff --git a/src/components/home/HomeFeed.tsx b/src/components/home/HomeFeed.tsx index eb874d7b..d1bb79eb 100644 --- a/src/components/home/HomeFeed.tsx +++ b/src/components/home/HomeFeed.tsx @@ -24,6 +24,7 @@ import type { FeedType, SearchType } from "~/models/home.model" import { useGetFeed } from "~/queries/home" import { EmptyState } from "../ui/EmptyState" +import { Skeleton } from "../ui/Skeleton" const PostCard = ({ post, @@ -337,7 +338,7 @@ export const HomeFeed: React.FC<{ )} {feed.isLoading ? ( -
{t("Loading")}...
+ ) : !feed.data?.pages[0]?.count ? ( ) : ( @@ -390,3 +391,19 @@ const Loader = () => { ) } + +const FeedSkeleton = () => { + return ( + +
+
+ + +
+
+ +
+
+
+ ) +} diff --git a/src/components/ui/Skeleton.tsx b/src/components/ui/Skeleton.tsx new file mode 100644 index 00000000..acb79ba7 --- /dev/null +++ b/src/components/ui/Skeleton.tsx @@ -0,0 +1,87 @@ +"use client" + +import React, { PropsWithChildren, useMemo } from "react" + +import { useTranslation } from "~/lib/i18n/client" +import { cn } from "~/lib/utils" + +interface Props { + className?: string + width?: number | string + height?: number | string +} + +const skeletonColor = "bg-gray-200 dark:bg-gray-700" + +function unit(unit: string | number | undefined) { + if (!unit) return + return typeof unit === "number" ? `${unit}px` : unit +} + +export function Skeleton({ className, width, height }: Props) { + const sizeObj = useMemo(() => { + const _size: any = {} + let _className = [] + width ? (_size.width = unit(width)) : _className.push("w-full") + height ? (_size.height = unit(height)) : _className.push("h-4") + return { + style: _size, + className: _className.join(" "), + } + }, [width, height]) + + return ( +
+ ) +} + +function Container({ + count = 1, + className, + children, +}: PropsWithChildren<{ count?: number; className?: string }>) { + const { t } = useTranslation("common") + const childrenArray = React.Children.toArray(children) + + return ( +
+ {Array.from(new Array(count)).map((_, index) => ( + + {childrenArray.map((child) => + React.cloneElement(child as React.ReactElement, { + key: index, + }), + )} + + ))} + {t("Loading")}... +
+ ) +} + +function Circle({ + size = 40, + className, +}: { + size?: number + className?: string +}) { + return ( + + ) +} + +function Rectangle({ className }: { className?: string }) { + return +} + +Skeleton.Container = Container +Skeleton.Circle = Circle +Skeleton.Rectangle = Rectangle