2022-06-08 19:42:23 +00:00
|
|
|
import clsx from 'clsx'
|
2022-06-08 04:07:05 +00:00
|
|
|
import { PencilIcon } from '@heroicons/react/outline'
|
2022-06-15 21:34:34 +00:00
|
|
|
|
2022-06-08 03:24:18 +00:00
|
|
|
import { User } from 'common/user'
|
2022-09-02 02:38:09 +00:00
|
|
|
import { useState } from 'react'
|
2022-06-08 03:24:18 +00:00
|
|
|
import { useFollowers, useFollows } from 'web/hooks/use-follows'
|
2022-09-02 02:38:09 +00:00
|
|
|
import { usePrefetchUsers, useUser } from 'web/hooks/use-user'
|
2022-06-08 03:24:18 +00:00
|
|
|
import { FollowList } from './follow-list'
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Modal } from './layout/modal'
|
|
|
|
import { Tabs } from './layout/tabs'
|
2022-06-08 04:42:42 +00:00
|
|
|
import { useDiscoverUsers } from 'web/hooks/use-users'
|
2022-06-08 19:23:10 +00:00
|
|
|
import { TextButton } from './text-button'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-06-08 03:24:18 +00:00
|
|
|
|
|
|
|
export function FollowingButton(props: { user: User }) {
|
|
|
|
const { user } = props
|
2022-06-08 19:23:10 +00:00
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
2022-06-08 03:24:18 +00:00
|
|
|
const followingIds = useFollows(user.id)
|
|
|
|
const followerIds = useFollowers(user.id)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-08 19:23:10 +00:00
|
|
|
<TextButton onClick={() => setIsOpen(true)}>
|
2022-06-08 03:24:18 +00:00
|
|
|
<span className="font-semibold">{followingIds?.length ?? ''}</span>{' '}
|
|
|
|
Following
|
2022-06-08 19:23:10 +00:00
|
|
|
</TextButton>
|
2022-06-08 03:24:18 +00:00
|
|
|
|
2022-06-08 19:38:04 +00:00
|
|
|
<FollowsDialog
|
2022-06-08 03:24:18 +00:00
|
|
|
user={user}
|
|
|
|
defaultTab="following"
|
|
|
|
followingIds={followingIds ?? []}
|
|
|
|
followerIds={followerIds ?? []}
|
2022-06-08 19:23:10 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
setIsOpen={setIsOpen}
|
2022-06-08 03:24:18 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-08 04:07:05 +00:00
|
|
|
export function EditFollowingButton(props: { user: User; className?: string }) {
|
|
|
|
const { user, className } = props
|
2022-06-08 19:23:10 +00:00
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
2022-06-08 04:07:05 +00:00
|
|
|
const followingIds = useFollows(user.id)
|
|
|
|
const followerIds = useFollowers(user.id)
|
|
|
|
|
|
|
|
return (
|
2022-06-08 19:42:23 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
className,
|
|
|
|
'btn btn-sm btn-ghost cursor-pointer gap-2 whitespace-nowrap text-sm normal-case text-gray-700'
|
|
|
|
)}
|
2022-06-15 21:34:34 +00:00
|
|
|
onClick={() => {
|
|
|
|
setIsOpen(true)
|
|
|
|
track('edit following button')
|
|
|
|
}}
|
2022-06-08 19:42:23 +00:00
|
|
|
>
|
2022-06-08 04:07:05 +00:00
|
|
|
<PencilIcon className="inline h-4 w-4" />
|
2022-06-08 16:24:35 +00:00
|
|
|
Following
|
2022-06-08 19:38:04 +00:00
|
|
|
<FollowsDialog
|
2022-06-08 04:07:05 +00:00
|
|
|
user={user}
|
|
|
|
defaultTab="following"
|
|
|
|
followingIds={followingIds ?? []}
|
|
|
|
followerIds={followerIds ?? []}
|
2022-06-08 19:23:10 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
setIsOpen={setIsOpen}
|
2022-06-08 04:07:05 +00:00
|
|
|
/>
|
2022-06-08 19:42:23 +00:00
|
|
|
</div>
|
2022-06-08 04:07:05 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-08 03:24:18 +00:00
|
|
|
export function FollowersButton(props: { user: User }) {
|
|
|
|
const { user } = props
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
const followingIds = useFollows(user.id)
|
|
|
|
const followerIds = useFollowers(user.id)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-08 19:23:10 +00:00
|
|
|
<TextButton onClick={() => setIsOpen(true)}>
|
2022-06-08 03:24:18 +00:00
|
|
|
<span className="font-semibold">{followerIds?.length ?? ''}</span>{' '}
|
|
|
|
Followers
|
2022-06-08 19:23:10 +00:00
|
|
|
</TextButton>
|
2022-06-08 03:24:18 +00:00
|
|
|
|
2022-06-08 19:38:04 +00:00
|
|
|
<FollowsDialog
|
2022-06-08 03:24:18 +00:00
|
|
|
user={user}
|
|
|
|
defaultTab="followers"
|
|
|
|
followingIds={followingIds ?? []}
|
|
|
|
followerIds={followerIds ?? []}
|
|
|
|
isOpen={isOpen}
|
|
|
|
setIsOpen={setIsOpen}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-08 19:38:04 +00:00
|
|
|
function FollowsDialog(props: {
|
2022-06-08 03:24:18 +00:00
|
|
|
user: User
|
|
|
|
followingIds: string[]
|
|
|
|
followerIds: string[]
|
|
|
|
defaultTab: 'following' | 'followers'
|
|
|
|
isOpen: boolean
|
|
|
|
setIsOpen: (isOpen: boolean) => void
|
|
|
|
}) {
|
|
|
|
const { user, followingIds, followerIds, defaultTab, isOpen, setIsOpen } =
|
|
|
|
props
|
|
|
|
|
2022-06-08 04:42:42 +00:00
|
|
|
const currentUser = useUser()
|
2022-06-08 19:38:04 +00:00
|
|
|
const discoverUserIds = useDiscoverUsers(user?.id)
|
2022-09-02 02:38:09 +00:00
|
|
|
usePrefetchUsers([...followingIds, ...followerIds, ...discoverUserIds])
|
2022-06-08 04:42:42 +00:00
|
|
|
|
2022-06-08 03:24:18 +00:00
|
|
|
return (
|
|
|
|
<Modal open={isOpen} setOpen={setIsOpen}>
|
|
|
|
<Col className="rounded bg-white p-6">
|
|
|
|
<div className="p-2 pb-1 text-xl">{user.name}</div>
|
|
|
|
<div className="p-2 pt-0 text-sm text-gray-500">@{user.username}</div>
|
|
|
|
<Tabs
|
2022-09-22 16:12:53 +00:00
|
|
|
className="mb-4"
|
2022-06-08 03:24:18 +00:00
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
title: 'Following',
|
|
|
|
content: <FollowList userIds={followingIds} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Followers',
|
|
|
|
content: <FollowList userIds={followerIds} />,
|
|
|
|
},
|
2022-06-08 04:42:42 +00:00
|
|
|
...(currentUser
|
|
|
|
? [
|
|
|
|
{
|
2022-06-08 19:38:04 +00:00
|
|
|
title: 'Similar',
|
2022-06-08 04:42:42 +00:00
|
|
|
content: <FollowList userIds={discoverUserIds} />,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []),
|
2022-06-08 03:24:18 +00:00
|
|
|
]}
|
|
|
|
defaultIndex={defaultTab === 'following' ? 0 : 1}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|