feat: add skeleton loading for home feed (#574)
This commit is contained in:
@@ -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")}...</>
|
||||
<Skeleton.Container className={cn(simple ? "space-y-1" : "space-y-2")}>
|
||||
<div className="flex justify-between items-center">
|
||||
<Skeleton.Circle size={40} />
|
||||
<Skeleton.Rectangle className="h-7 w-24" />
|
||||
</div>
|
||||
<Skeleton.Rectangle className="w-2/3" />
|
||||
<Skeleton.Rectangle className="my-4 w-full h-16" />
|
||||
<Skeleton.Rectangle className="w-1/2" />
|
||||
</Skeleton.Container>
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
|
||||
@@ -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 ? (
|
||||
<div className="text-center text-zinc-600">{t("Loading")}...</div>
|
||||
<FeedSkeleton />
|
||||
) : !feed.data?.pages[0]?.count ? (
|
||||
<EmptyState />
|
||||
) : (
|
||||
@@ -390,3 +391,19 @@ const Loader = () => {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const FeedSkeleton = () => {
|
||||
return (
|
||||
<Skeleton.Container count={5} className="space-y-8">
|
||||
<div>
|
||||
<div className="flex space-x-2 items-center">
|
||||
<Skeleton.Circle size={40} />
|
||||
<Skeleton.Rectangle className="w-1/3" />
|
||||
</div>
|
||||
<div className="py-4 pr-4 ml-12">
|
||||
<Skeleton.Rectangle className="w-full h-24" />
|
||||
</div>
|
||||
</div>
|
||||
</Skeleton.Container>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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 (
|
||||
<div
|
||||
className={cn(skeletonColor, sizeObj.className, className)}
|
||||
style={sizeObj.style}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function Container({
|
||||
count = 1,
|
||||
className,
|
||||
children,
|
||||
}: PropsWithChildren<{ count?: number; className?: string }>) {
|
||||
const { t } = useTranslation("common")
|
||||
const childrenArray = React.Children.toArray(children)
|
||||
|
||||
return (
|
||||
<div role="status" className={cn("animate-pulse", className)}>
|
||||
{Array.from(new Array(count)).map((_, index) => (
|
||||
<React.Fragment key={index}>
|
||||
{childrenArray.map((child) =>
|
||||
React.cloneElement(child as React.ReactElement<any>, {
|
||||
key: index,
|
||||
}),
|
||||
)}
|
||||
</React.Fragment>
|
||||
))}
|
||||
<span className="sr-only">{t("Loading")}...</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Circle({
|
||||
size = 40,
|
||||
className,
|
||||
}: {
|
||||
size?: number
|
||||
className?: string
|
||||
}) {
|
||||
return (
|
||||
<Skeleton
|
||||
className={cn("rounded-full", className)}
|
||||
width={size}
|
||||
height={size}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function Rectangle({ className }: { className?: string }) {
|
||||
return <Skeleton className={cn("rounded", className)} />
|
||||
}
|
||||
|
||||
Skeleton.Container = Container
|
||||
Skeleton.Circle = Circle
|
||||
Skeleton.Rectangle = Rectangle
|
||||
Reference in New Issue
Block a user