feat: character card
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import {
|
||||
offset,
|
||||
flip,
|
||||
shift,
|
||||
autoUpdate,
|
||||
useFloating,
|
||||
useInteractions,
|
||||
useHover,
|
||||
useFocus,
|
||||
useRole,
|
||||
useDismiss,
|
||||
} from "@floating-ui/react-dom-interactions"
|
||||
import { Avatar } from "~/components/ui/Avatar"
|
||||
import dayjs from "dayjs"
|
||||
import duration from "dayjs/plugin/duration"
|
||||
import relativeTime from "dayjs/plugin/relativeTime"
|
||||
import { FollowingButton } from "~/components/common/FollowingButton"
|
||||
import { FollowingCount } from "~/components/common/FollowingCount"
|
||||
import * as siteModel from "~/models/site.model"
|
||||
import type { Profile } from "~/lib/types"
|
||||
|
||||
dayjs.extend(duration)
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
export const CharacterCard: React.FC<{
|
||||
siteId?: string
|
||||
children: JSX.Element
|
||||
}> = ({ siteId, children }) => {
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
const { x, y, reference, floating, strategy, context } = useFloating({
|
||||
placement: "bottom-start",
|
||||
open,
|
||||
onOpenChange: setOpen,
|
||||
middleware: [offset(5), flip(), shift({ padding: 8 })],
|
||||
whileElementsMounted: autoUpdate,
|
||||
})
|
||||
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||
useHover(context, { delay: { close: 100 } }),
|
||||
useFocus(context),
|
||||
useRole(context, { role: "tooltip" }),
|
||||
useDismiss(context),
|
||||
])
|
||||
|
||||
const [buttonLoading, setButtonLoading] = useState(false)
|
||||
const [firstOpen, setFirstOpen] = useState(false)
|
||||
const [site, setSite] = useState<Profile>()
|
||||
|
||||
useEffect(() => {
|
||||
if (!firstOpen && open && siteId && !site) {
|
||||
setFirstOpen(true)
|
||||
siteModel.getSite(siteId).then((site) => setSite(site))
|
||||
}
|
||||
}, [open, firstOpen, siteId, site])
|
||||
|
||||
return (
|
||||
<div {...getReferenceProps({ ref: reference, ...children.props })}>
|
||||
{children}
|
||||
{(open || buttonLoading) && (
|
||||
<div
|
||||
{...getFloatingProps({
|
||||
ref: floating,
|
||||
className:
|
||||
"border-gray-100 rounded-lg p-4 bg-white z-10 space-y-2 text-sm w-80 shadow-xl",
|
||||
style: {
|
||||
position: strategy,
|
||||
top: y ?? "",
|
||||
left: x ?? "",
|
||||
},
|
||||
})}
|
||||
>
|
||||
{site ? (
|
||||
<>
|
||||
<div className="flex items-center justify-between">
|
||||
<Avatar
|
||||
images={site?.avatars || []}
|
||||
name={site?.name}
|
||||
size={45}
|
||||
/>
|
||||
<FollowingButton
|
||||
siteId={siteId}
|
||||
size="sm"
|
||||
loadingStatusChange={(status) => setButtonLoading(status)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-bold text-base">{site?.name}</span>
|
||||
<span className="ml-1 text-gray-600">@{siteId}</span>
|
||||
</div>
|
||||
{site?.description && (
|
||||
<div
|
||||
className="text-gray-600"
|
||||
dangerouslySetInnerHTML={{ __html: site?.description || "" }}
|
||||
></div>
|
||||
)}
|
||||
<div>
|
||||
<FollowingCount siteId={siteId} disableList={true} />
|
||||
</div>
|
||||
<div className="text-gray-500">
|
||||
Joined{" "}
|
||||
{dayjs
|
||||
.duration(
|
||||
dayjs(site?.date_created).diff(dayjs(), "minute"),
|
||||
"minute",
|
||||
)
|
||||
.humanize()}{" "}
|
||||
ago
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
"Loading..."
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { CSB_SCAN } from "~/lib/env"
|
||||
import { Avatar } from "../ui/Avatar"
|
||||
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
|
||||
import { getSiteLink } from "~/lib/helpers"
|
||||
import { CharacterCard } from "~/components/common/CharacterCard"
|
||||
|
||||
export const CharacterList: React.FC<{
|
||||
open: boolean
|
||||
@@ -41,15 +42,17 @@ export const CharacterList: React.FC<{
|
||||
})}
|
||||
className="flex items-center space-x-2 text-sm"
|
||||
>
|
||||
<Avatar
|
||||
className="align-middle border-2 border-white"
|
||||
images={sub.character?.metadata?.content?.avatars || []}
|
||||
name={
|
||||
sub.character?.metadata?.content?.name ||
|
||||
sub.character?.handle
|
||||
}
|
||||
size={40}
|
||||
/>
|
||||
<CharacterCard siteId={sub?.character?.handle}>
|
||||
<Avatar
|
||||
className="align-middle border-2 border-white"
|
||||
images={sub.character?.metadata?.content?.avatars || []}
|
||||
name={
|
||||
sub.character?.metadata?.content?.name ||
|
||||
sub.character?.handle
|
||||
}
|
||||
size={40}
|
||||
/>
|
||||
</CharacterCard>
|
||||
<span>{sub.character?.metadata?.content?.name}</span>
|
||||
<span className="text-zinc-400 truncate max-w-xs">
|
||||
@{sub.character?.handle}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Button } from "~/components/ui/Button"
|
||||
import { Reactions } from "~/components/common/Reactions"
|
||||
import { useState } from "react"
|
||||
import { CommentInput } from "~/components/common/CommentInput"
|
||||
import { CharacterCard } from "~/components/common/CharacterCard"
|
||||
|
||||
dayjs.extend(duration)
|
||||
dayjs.extend(relativeTime)
|
||||
@@ -31,21 +32,27 @@ export const CommentItem: React.FC<{
|
||||
className={depth > 0 ? "" : "border-b border-dashed pb-6"}
|
||||
>
|
||||
<div className="flex group">
|
||||
<UniLink
|
||||
href={
|
||||
comment?.character?.handle &&
|
||||
getSiteLink({
|
||||
subdomain: comment.character.handle,
|
||||
})
|
||||
}
|
||||
className="align-middle mr-3"
|
||||
>
|
||||
<Avatar
|
||||
images={comment?.character?.metadata?.content?.avatars || []}
|
||||
name={comment?.character?.metadata?.content?.name}
|
||||
size={45}
|
||||
/>
|
||||
</UniLink>
|
||||
<div>
|
||||
<CharacterCard siteId={comment?.character?.handle}>
|
||||
<div>
|
||||
<UniLink
|
||||
href={
|
||||
comment?.character?.handle &&
|
||||
getSiteLink({
|
||||
subdomain: comment.character.handle,
|
||||
})
|
||||
}
|
||||
className="block align-middle mr-3"
|
||||
>
|
||||
<Avatar
|
||||
images={comment?.character?.metadata?.content?.avatars || []}
|
||||
name={comment?.character?.metadata?.content?.name}
|
||||
size={45}
|
||||
/>
|
||||
</UniLink>
|
||||
</div>
|
||||
</CharacterCard>
|
||||
</div>
|
||||
<div className="flex-1 flex flex-col rounded-lg">
|
||||
<div className="mb-1 text-sm">
|
||||
<UniLink
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useRouter } from "next/router"
|
||||
import { SITE_URL } from "~/lib/env"
|
||||
import { Button } from "../ui/Button"
|
||||
import { Profile } from "~/lib/types"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import type { Variant } from "~/components/ui/Button"
|
||||
import { useAccount } from "wagmi"
|
||||
import { useEffect, useState } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
@@ -12,11 +12,15 @@ import {
|
||||
useGetUserSites,
|
||||
} from "~/queries/site"
|
||||
import { useConnectModal } from "@rainbow-me/rainbowkit"
|
||||
import type { Links } from "unidata.js"
|
||||
import clsx from "clsx"
|
||||
|
||||
export const FollowingButton: React.FC<{
|
||||
siteId?: string
|
||||
}> = ({ siteId }) => {
|
||||
variant?: Variant
|
||||
className?: string
|
||||
size?: "sm" | "xl"
|
||||
loadingStatusChange?: (status: boolean) => void
|
||||
}> = ({ siteId, variant, className, size, loadingStatusChange }) => {
|
||||
const { address } = useAccount()
|
||||
const subscribeToSite = useSubscribeToSite()
|
||||
const unsubscribeFromSite = useUnsubscribeFromSite()
|
||||
@@ -93,11 +97,23 @@ export const FollowingButton: React.FC<{
|
||||
}
|
||||
}, [subscribeToSite.isError, subscribeToSite.data?.code, subscribeToSite])
|
||||
|
||||
useEffect(() => {
|
||||
if (unsubscribeFromSite.isLoading || subscribeToSite.isLoading) {
|
||||
loadingStatusChange?.(true)
|
||||
} else {
|
||||
loadingStatusChange?.(false)
|
||||
}
|
||||
}, [
|
||||
unsubscribeFromSite.isLoading,
|
||||
subscribeToSite.isLoading,
|
||||
loadingStatusChange,
|
||||
])
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="text"
|
||||
variant={variant}
|
||||
onClick={handleClickSubscribe}
|
||||
className="space-x-1 group align-middle text-accent mx-5"
|
||||
className={clsx(className, "align-middle space-x-1")}
|
||||
isLoading={
|
||||
subscription.data
|
||||
? unsubscribeFromSite.isLoading || subscribeToSite.isLoading
|
||||
@@ -106,6 +122,7 @@ export const FollowingButton: React.FC<{
|
||||
subscribeToSite.isLoading ||
|
||||
subscription.isLoading
|
||||
}
|
||||
size={size}
|
||||
>
|
||||
<span className="i-bxs:bell"></span>
|
||||
{subscription.data ? (
|
||||
|
||||
@@ -8,7 +8,8 @@ import { useGetSiteSubscriptions } from "~/queries/site"
|
||||
|
||||
export const FollowingCount: React.FC<{
|
||||
siteId?: string
|
||||
}> = ({ siteId }) => {
|
||||
disableList?: boolean
|
||||
}> = ({ siteId, disableList }) => {
|
||||
let [isFollowListOpen, setIsFollowListOpen] = useState(false)
|
||||
|
||||
const subscriptions = useGetSiteSubscriptions({
|
||||
@@ -48,7 +49,10 @@ export const FollowingCount: React.FC<{
|
||||
<>
|
||||
{subscriptions ? (
|
||||
<span
|
||||
className="xlog-site-followers align-middle text-zinc-500 text-sm cursor-pointer"
|
||||
className={
|
||||
"xlog-site-followers align-middle text-zinc-500 text-sm" +
|
||||
(disableList ? "" : " cursor-pointer")
|
||||
}
|
||||
onClick={() => setIsFollowListOpen(true)}
|
||||
>
|
||||
<span className="font-bold text-zinc-700">
|
||||
@@ -59,14 +63,16 @@ export const FollowingCount: React.FC<{
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
<CharacterList
|
||||
open={isFollowListOpen}
|
||||
setOpen={setIsFollowListOpen}
|
||||
title="Follow List"
|
||||
loadMore={loadMoreSubscriptions}
|
||||
hasMore={!!cursor}
|
||||
list={siteSubscriptionList}
|
||||
></CharacterList>
|
||||
{!disableList && (
|
||||
<CharacterList
|
||||
open={isFollowListOpen}
|
||||
setOpen={setIsFollowListOpen}
|
||||
title="Follow List"
|
||||
loadMore={loadMoreSubscriptions}
|
||||
hasMore={!!cursor}
|
||||
list={siteSubscriptionList}
|
||||
></CharacterList>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import { ConnectButton } from "../common/ConnectButton"
|
||||
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
|
||||
import { RSS3Icon } from "~/components/icons/RSS3Icon"
|
||||
import { EllipsisHorizontalIcon, RssIcon } from "@heroicons/react/20/solid"
|
||||
import type { Links } from "unidata.js"
|
||||
import { Image } from "~/components/ui/Image"
|
||||
import { FollowingButton } from "~/components/common/FollowingButton"
|
||||
import { FollowingCount } from "~/components/common/FollowingCount"
|
||||
@@ -116,7 +115,11 @@ export const SiteHeader: React.FC<{
|
||||
)}
|
||||
<div className="mt-3 text-sm">
|
||||
<FollowingCount siteId={site?.username} />
|
||||
<FollowingButton siteId={site?.username} />
|
||||
<FollowingButton
|
||||
siteId={site?.username}
|
||||
className="text-accent mx-5"
|
||||
variant="text"
|
||||
/>
|
||||
<div className="relative inline-block align-middle h-7 group">
|
||||
<Button
|
||||
variant="text"
|
||||
|
||||
@@ -7,12 +7,20 @@ export const ButtonGroup: React.FC<{ children: React.ReactNode }> = ({
|
||||
return <div className="button-group">{children}</div>
|
||||
}
|
||||
|
||||
export type Variant =
|
||||
| "primary"
|
||||
| "secondary"
|
||||
| "text"
|
||||
| "like"
|
||||
| "collect"
|
||||
| "crossbell"
|
||||
|
||||
type ButtonProps = {
|
||||
isLoading?: boolean
|
||||
isBlock?: boolean
|
||||
isDisabled?: boolean
|
||||
isAutoWidth?: boolean
|
||||
variant?: "primary" | "secondary" | "text" | "like" | "collect" | "crossbell"
|
||||
variant?: Variant
|
||||
variantColor?: "green" | "red" | "gray"
|
||||
size?: "sm" | "xl"
|
||||
rounded?: "full" | "lg"
|
||||
|
||||
@@ -152,8 +152,8 @@ a {
|
||||
|
||||
.button.is-sm {
|
||||
@apply h-7;
|
||||
@apply px-2;
|
||||
@apply text-xs;
|
||||
@apply px-3;
|
||||
@apply text-sm;
|
||||
@apply min-w-[auto];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user