refactor: 重寫消息顯示相關邏輯
This commit is contained in:
@@ -1,15 +1,25 @@
|
|||||||
|
import Client from "../../api/Client.ts"
|
||||||
|
import DataCaches from "../../api/DataCaches.ts"
|
||||||
import Avatar from "../Avatar.tsx"
|
import Avatar from "../Avatar.tsx"
|
||||||
|
import useAsyncEffect from "../useAsyncEffect.ts"
|
||||||
|
import React from "react"
|
||||||
|
|
||||||
|
interface Args extends React.HTMLAttributes<HTMLElement> {
|
||||||
|
userId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Message({ userId, children, ...props }: Args) {
|
||||||
|
const isAtRight = Client.myUserProfile?.id == userId
|
||||||
|
|
||||||
|
const [ nickName, setNickName ] = React.useState("")
|
||||||
|
const [ avatarUrl, setAvatarUrl ] = React.useState<string | undefined>("")
|
||||||
|
|
||||||
|
useAsyncEffect(async () => {
|
||||||
|
const user = await DataCaches.getUserProfile(userId)
|
||||||
|
setNickName(user.nickname)
|
||||||
|
setAvatarUrl(user?.avatar)
|
||||||
|
}, [userId])
|
||||||
|
|
||||||
/**
|
|
||||||
* 一条消息
|
|
||||||
* @param { Object } param
|
|
||||||
* @param { "left" | "right" } [param.direction="left"] 消息方向
|
|
||||||
* @param { String } [param.avatar] 头像链接
|
|
||||||
* @param { String } [param.nickName] 昵称
|
|
||||||
* @returns { React.JSX.Element }
|
|
||||||
*/
|
|
||||||
export default function Message({ direction = 'left', avatar, nickName, children, ...props } = {}) {
|
|
||||||
let isAtRight = direction == 'right'
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
slot="trigger"
|
slot="trigger"
|
||||||
@@ -39,7 +49,7 @@ export default function Message({ direction = 'left', avatar, nickName, children
|
|||||||
// 发送者头像
|
// 发送者头像
|
||||||
}
|
}
|
||||||
<Avatar
|
<Avatar
|
||||||
src={avatar}
|
src={avatarUrl}
|
||||||
text={nickName}
|
text={nickName}
|
||||||
style={{
|
style={{
|
||||||
width: "43px",
|
width: "43px",
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
/**
|
interface Args extends React.HTMLAttributes<HTMLElement> {}
|
||||||
* 消息容器
|
|
||||||
* @returns { React.JSX.Element }
|
export default function MessageContainer({ children, style, ...props }: Args) {
|
||||||
*/
|
|
||||||
export default function MessageContainer({ children, style, ...props } = {}) {
|
|
||||||
return (
|
return (
|
||||||
<div style={{
|
<div style={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
@@ -10,7 +8,6 @@ export default function MessageContainer({ children, style, ...props } = {}) {
|
|||||||
justifyContent: 'flex-end',
|
justifyContent: 'flex-end',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
marginBottom: '20px',
|
marginBottom: '20px',
|
||||||
height: "100%",
|
|
||||||
...style,
|
...style,
|
||||||
}}
|
}}
|
||||||
{...props}>
|
{...props}>
|
||||||
Reference in New Issue
Block a user