Whalebird-desktop-client-ma.../renderer/components/detail/Profile.tsx

165 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-12-02 03:50:42 +01:00
import emojify from '@/utils/emojify'
import { Entity, MegalodonInterface } from 'megalodon'
2023-12-02 16:52:51 +01:00
import { MouseEventHandler, useEffect, useState } from 'react'
2023-12-02 03:50:42 +01:00
import { FaEllipsisVertical } from 'react-icons/fa6'
import { FormattedMessage } from 'react-intl'
2023-12-02 05:30:01 +01:00
import Timeline from './profile/Timeline'
2023-12-02 05:53:43 +01:00
import Followings from './profile/Followings'
2023-12-02 07:06:47 +01:00
import Followers from './profile/Followers'
2023-12-02 16:52:51 +01:00
import { findLink } from '@/utils/statusParser'
import { Account } from '@/db'
import {
Avatar,
Button,
IconButton,
List,
ListItem,
Popover,
PopoverContent,
PopoverHandler,
Tab,
TabPanel,
Tabs,
TabsBody,
TabsHeader
} from '@material-tailwind/react'
2023-12-02 03:50:42 +01:00
type Props = {
client: MegalodonInterface
account: Account
2023-12-02 03:50:42 +01:00
user_id: string
2023-12-02 11:26:45 +01:00
openMedia: (media: Entity.Attachment) => void
2023-12-02 03:50:42 +01:00
}
export default function Profile(props: Props) {
const [user, setUser] = useState<Entity.Account | null>(null)
const [relationship, setRelationship] = useState<Entity.Relationship | null>(null)
const [popoverDetail, setPopoverDetail] = useState(false)
2023-12-02 03:50:42 +01:00
useEffect(() => {
const f = async () => {
if (props.user_id) {
const res = await props.client.getAccount(props.user_id)
setUser(res.data)
const rel = await props.client.getRelationship(props.user_id)
setRelationship(rel.data)
}
}
f()
}, [props.user_id, props.client])
const follow = async (id: string) => {
const rel = await props.client.followAccount(id)
setRelationship(rel.data)
}
const unfollow = async (id: string) => {
const rel = await props.client.unfollowAccount(id)
setRelationship(rel.data)
}
const openOriginal = async (url: string) => {
global.ipc.invoke('open-browser', url)
}
2023-12-02 16:52:51 +01:00
const profileClicked: MouseEventHandler<HTMLDivElement> = async e => {
const url = findLink(e.target as HTMLElement, 'profile')
if (url) {
global.ipc.invoke('open-browser', url)
e.preventDefault()
e.stopPropagation()
}
}
2023-12-02 03:50:42 +01:00
return (
2023-12-02 07:13:28 +01:00
<div style={{ height: 'calc(100% - 50px)' }} className="overflow-y-auto timeline-scrollable">
{user && relationship && (
<>
<div className="header-image w-full bg-gray-100">
<img src={user.header} alt="header image" className="w-full object-cover h-40" />
</div>
<div className="p-5">
<div className="flex items-end justify-between" style={{ marginTop: '-50px' }}>
<Avatar src={user.avatar} size="xl" variant="rounded" />
<div className="flex gap-2">
{relationship.following ? (
<Button color="red" onClick={() => unfollow(user.id)}>
<FormattedMessage id="profile.unfollow" />
</Button>
) : (
<Button color="blue" onClick={() => follow(user.id)}>
<FormattedMessage id="profile.follow" />
</Button>
)}
<Popover open={popoverDetail} handler={setPopoverDetail}>
<PopoverHandler>
<IconButton variant="outlined">
<FaEllipsisVertical />
</IconButton>
</PopoverHandler>
<PopoverContent>
<List>
<ListItem
onClick={() => {
openOriginal(user.url)
setPopoverDetail(false)
}}
>
<FormattedMessage id="profile.open_original" />
</ListItem>
</List>
</PopoverContent>
</Popover>
</div>
2023-12-02 05:30:01 +01:00
</div>
<div className="pt-4">
<div className="font-bold" dangerouslySetInnerHTML={{ __html: emojify(user.display_name, user.emojis) }} />
<div className="text-gray-500">@{user.acct}</div>
<div className="mt-4 raw-html profile" onClick={profileClicked}>
<span
dangerouslySetInnerHTML={{ __html: emojify(user.note, user.emojis) }}
className="overflow-hidden break-all text-gray-800"
/>
2023-12-02 03:50:42 +01:00
</div>
<div className="bg-gray-100 overflow-hidden break-all raw-html mt-2 profile" onClick={profileClicked}>
{user.fields.map((data, index) => (
<dl key={index} className="px-4 py-2 border-gray-200 border-b">
<dt className="text-gray-500">{data.name}</dt>
<dd className="text-gray-700" dangerouslySetInnerHTML={{ __html: emojify(data.value, user.emojis) }} />
</dl>
))}
2023-12-02 03:50:42 +01:00
</div>
</div>
</div>
<div>
<Tabs value="timeline">
<TabsHeader>
<Tab value="timeline">
<FormattedMessage id="profile.timeline" />
</Tab>
<Tab value="followings">
<FormattedMessage id="profile.followings" />
</Tab>
<Tab value="followers">
<FormattedMessage id="profile.followers" />
</Tab>
</TabsHeader>
<TabsBody>
<TabPanel value="timeline">
<Timeline client={props.client} account={props.account} user_id={props.user_id} openMedia={props.openMedia} />
</TabPanel>
<TabPanel value="followings">
2023-12-02 05:53:43 +01:00
<Followings client={props.client} user_id={props.user_id} />
</TabPanel>
<TabPanel value="followers">
2023-12-02 07:06:47 +01:00
<Followers client={props.client} user_id={props.user_id} />
</TabPanel>
</TabsBody>
</Tabs>
</div>
</>
)}
2023-12-02 03:50:42 +01:00
</div>
)
}