refactor: add UserGallery and integrate into SupportCreator (#1167)
* feat: add UserGallery component and user utilities * refactor: integrate UserGallery to display deduplicated supporters in SupportCreator * feat: integrate UserGallery for displaying boosting contributors * fix: merging tipUsers fields with array * chore: increase default user limit in UserGallery to 18
This commit is contained in:
@@ -5,6 +5,8 @@ import { replaceImgUrlIfNeed } from "~/lib/img-proxy"
|
||||
import { useFeedBoostersQuery } from "~/modules/boost/query"
|
||||
|
||||
import { usePresentUserProfileModal } from "../profile/hooks"
|
||||
import { UserGallery } from "../user/UserGallery"
|
||||
import type { User } from "../user/utils"
|
||||
|
||||
export const BoostingContributors = ({ feedId }: { feedId: string }) => {
|
||||
const { data: boosters, isLoading } = useFeedBoostersQuery(feedId)
|
||||
@@ -21,27 +23,34 @@ export const BoostingContributors = ({ feedId }: { feedId: string }) => {
|
||||
>
|
||||
<h2 className="mb-4 text-2xl font-bold">Boosting Contributors</h2>
|
||||
<ul className="space-y-4">
|
||||
{boosters
|
||||
.map((user) => (
|
||||
<li
|
||||
key={user.id}
|
||||
role="button"
|
||||
className="flex items-center space-x-4"
|
||||
onClick={() => presentUserProfile(user.id)}
|
||||
>
|
||||
<Avatar className="block aspect-square size-10 overflow-hidden rounded-full border border-border ring-1 ring-background">
|
||||
<AvatarImage src={replaceImgUrlIfNeed(user?.image || undefined)} />
|
||||
<AvatarFallback>{user.name?.slice(0, 2)}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div>
|
||||
<div className="font-semibold">{user.name || user.handle}</div>
|
||||
{user.handle && <div className="text-xs text-gray-500">@{user.handle}</div>}
|
||||
</div>
|
||||
</li>
|
||||
{boosters.length < 8 ? (
|
||||
boosters.map((user) => (
|
||||
<UserListItem key={user.id} user={user} onClick={presentUserProfile} />
|
||||
))
|
||||
// TODO use grid layout when there are many boosters
|
||||
.slice(0, 10)}
|
||||
) : (
|
||||
<UserGallery users={boosters} />
|
||||
)}
|
||||
</ul>
|
||||
</m.div>
|
||||
)
|
||||
}
|
||||
|
||||
const UserListItem = ({ user, onClick }: { user: User; onClick: (userId: string) => void }) => {
|
||||
return (
|
||||
<li
|
||||
key={user.id}
|
||||
role="button"
|
||||
className="flex items-center space-x-4"
|
||||
onClick={() => onClick(user.id)}
|
||||
>
|
||||
<Avatar className="block aspect-square size-10 overflow-hidden rounded-full border border-border ring-1 ring-background">
|
||||
<AvatarImage src={replaceImgUrlIfNeed(user?.image || undefined)} />
|
||||
<AvatarFallback>{user.name?.slice(0, 2)}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div>
|
||||
<div className="font-semibold">{user.name || user.handle}</div>
|
||||
{user.handle && <div className="text-xs text-gray-500">@{user.handle}</div>}
|
||||
</div>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Button } from "@follow/components/ui/button/index.js"
|
||||
import { Divider } from "@follow/components/ui/divider/index.js"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useWhoami } from "~/atoms/user"
|
||||
@@ -7,9 +8,11 @@ import { useBoostModal } from "~/modules/boost/hooks"
|
||||
import { useFeedBoostersQuery } from "~/modules/boost/query"
|
||||
import { FeedIcon } from "~/modules/feed/feed-icon"
|
||||
import { UserAvatar } from "~/modules/user/UserAvatar"
|
||||
import { deduplicateUsers } from "~/modules/user/utils"
|
||||
import { useEntry } from "~/store/entry"
|
||||
import { useFeedById } from "~/store/feed"
|
||||
|
||||
import { UserGallery } from "../../user/UserGallery"
|
||||
import { useTipModal } from "../../wallet/hooks"
|
||||
|
||||
export const SupportCreator = ({ entryId }: { entryId: string }) => {
|
||||
@@ -26,10 +29,17 @@ export const SupportCreator = ({ entryId }: { entryId: string }) => {
|
||||
const openBoostModal = useBoostModal()
|
||||
|
||||
const isMyOwnedFeed = feed?.ownerUserId === useWhoami()?.id
|
||||
if (!feed || feed.type !== "feed") return null
|
||||
const allSupporters = useMemo(
|
||||
() =>
|
||||
deduplicateUsers([
|
||||
...(feed && "tipUsers" in feed && feed.tipUsers ? feed.tipUsers : []),
|
||||
...(feedBoosters ?? []),
|
||||
]),
|
||||
[feed, feedBoosters],
|
||||
)
|
||||
const supportAmount = allSupporters.length
|
||||
|
||||
const supportAmount =
|
||||
(feed && feed.tipUsers ? feed.tipUsers.length : 0) + (feedBoosters ? feedBoosters.length : 0)
|
||||
if (!feed || feed.type !== "feed") return null
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -69,32 +79,7 @@ export const SupportCreator = ({ entryId }: { entryId: string }) => {
|
||||
<div className="text-sm text-zinc-500">
|
||||
{t("entry_content.support_amount", { amount: supportAmount })}
|
||||
</div>
|
||||
<div className="flex w-fit max-w-80 flex-wrap gap-4">
|
||||
{feed.tipUsers?.map((user) => (
|
||||
<div key={user.id} className="size-8">
|
||||
<UserAvatar
|
||||
className="h-auto p-0"
|
||||
avatarClassName="size-8"
|
||||
userId={user?.id}
|
||||
enableModal={true}
|
||||
hideName={true}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex w-fit max-w-80 flex-wrap gap-4">
|
||||
{feedBoosters?.map((user) => (
|
||||
<div key={user.id} className="size-8">
|
||||
<UserAvatar
|
||||
className="h-auto p-0"
|
||||
avatarClassName="size-8"
|
||||
userId={user?.id}
|
||||
enableModal={true}
|
||||
hideName={true}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<UserGallery users={allSupporters} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { UserAvatar } from "~/modules/user/UserAvatar"
|
||||
|
||||
import type { User } from "./utils"
|
||||
|
||||
interface UserGalleryProps {
|
||||
users: User[]
|
||||
dedupe?: boolean
|
||||
limit?: number
|
||||
}
|
||||
|
||||
export const UserGallery = ({ users, limit = 18 }: UserGalleryProps) => {
|
||||
const displayedUsers = users.slice(0, limit)
|
||||
|
||||
return (
|
||||
<div className="flex w-fit max-w-80 flex-wrap gap-4">
|
||||
{displayedUsers.map((user) => (
|
||||
<div key={user.id} className="size-8">
|
||||
<UserAvatar
|
||||
className="h-auto p-0"
|
||||
avatarClassName="size-8"
|
||||
userId={user?.id}
|
||||
enableModal={true}
|
||||
hideName={true}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
export interface User {
|
||||
name: string | null
|
||||
id: string
|
||||
emailVerified: string | null
|
||||
image: string | null
|
||||
handle: string | null
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export const deduplicateUsers = (users: User[]): User[] => {
|
||||
const userMap = new Map<string, User>()
|
||||
users.forEach((user) => {
|
||||
userMap.set(user.id, user)
|
||||
})
|
||||
return Array.from(userMap.values())
|
||||
}
|
||||
@@ -54,12 +54,13 @@ class FeedActions {
|
||||
}
|
||||
|
||||
// Not all API return these fields, so merging is needed here.
|
||||
const optionalFields = ["owner", "tipUsers"] as const
|
||||
optionalFields.forEach((field) => {
|
||||
if (state.feeds[feed.id!]?.[field] && !(field in feed)) {
|
||||
;(feed as any)[field] = { ...state.feeds[feed.id!]?.[field] }
|
||||
}
|
||||
})
|
||||
const targetFeed = state.feeds[feed.id]
|
||||
if (targetFeed?.owner) {
|
||||
feed.owner = { ...targetFeed.owner }
|
||||
}
|
||||
if (targetFeed && "tipUsers" in targetFeed && targetFeed.tipUsers) {
|
||||
feed.tipUsers = [...targetFeed.tipUsers]
|
||||
}
|
||||
|
||||
state.feeds[feed.id] = omit(feed, "feeds") as FeedOrListModel
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user