-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useChatMessageStore } from '@/pages/playRoom/store/chatMessage.js'; | ||
import { customScrollBar } from '@/styles/scrollbar.js'; | ||
import { emitSocket } from '@/utils/api.js'; | ||
import { infoContext } from '@/utils/infoContext.js'; | ||
import { SendOutlined } from '@ant-design/icons'; | ||
import { css } from '@emotion/react'; | ||
import { Button, Input, Tooltip } from 'antd'; | ||
import React, { useContext, useEffect, useRef, useState } from 'react'; | ||
import PlayerAvatar from '../playerAvatar/index.js'; | ||
|
||
const chatContainerStyle = css` | ||
width: 50vw; | ||
margin: 0 auto; | ||
${customScrollBar()} | ||
`; | ||
|
||
const scrollMessageBox = css` | ||
width: 100%; | ||
max-height: 30vh; | ||
min-height: 200px; | ||
overflow-y: auto; | ||
overflow-x: hidden; | ||
margin: 0 auto; | ||
background-color: rgb(246,248,250); | ||
${customScrollBar()} | ||
`; | ||
|
||
const chatBubbleBoxStyle = (isUser = false) => css` | ||
width: 100%; | ||
color: #fff; | ||
padding: 4px 8px; | ||
border-radius: 10px; | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
justify-content: ${isUser ? 'flex-end;' : 'flex-start;'} | ||
`; | ||
|
||
const chatBubbleStyle = (isUser = false) => css` | ||
background-color: ${isUser ? '#69c0ff' : '#87e8de'}; | ||
color: #fff; | ||
padding: 4px 8px; | ||
border-radius: 10px; | ||
margin: 5px; | ||
max-width: 70%; | ||
`; | ||
|
||
export default function ChatRoom (): React.JSX.Element { | ||
const listRef = useRef<HTMLDivElement>(null); | ||
const [message, setMessage] = useState(''); | ||
const { chatMessage: { info: messages } } = useChatMessageStore(); | ||
const { player } = useContext(infoContext); | ||
|
||
useEffect(() => { | ||
listRef.current?.scrollTo({ top: listRef.current.scrollHeight }); | ||
}, [messages]); | ||
|
||
const handleSendMessage = () => { | ||
if (message.trim() === '') return; | ||
emitSocket('sendMessage', { player, msg: message }); | ||
setMessage(''); | ||
}; | ||
|
||
return ( | ||
<div css={chatContainerStyle}> | ||
<div | ||
ref={listRef} | ||
css={scrollMessageBox} | ||
> | ||
{ | ||
messages.map(({ player: curPlayer, msg, key }) => { | ||
const isMe = curPlayer.name === player?.name; | ||
|
||
return <div css={chatBubbleBoxStyle(isMe)} key={key}> | ||
{!isMe && <Tooltip title={curPlayer.name}><PlayerAvatar player={curPlayer}/></Tooltip>} | ||
<span css={chatBubbleStyle(isMe)}> | ||
{msg} | ||
</span> | ||
{isMe && <Tooltip title={curPlayer.name}><PlayerAvatar player={curPlayer}/></Tooltip>} | ||
</div>; | ||
}) | ||
} | ||
</div> | ||
<Input | ||
css={css` | ||
width: 100% | ||
`} | ||
value={message} | ||
onChange={(e) => setMessage(e.target.value)} | ||
onPressEnter={handleSendMessage} | ||
suffix={ | ||
<Button type="primary" shape='circle' onClick={handleSendMessage} icon={<SendOutlined />} /> | ||
} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { PlayerInfoType } from '@/pages/playRoom/type.js'; | ||
import { css } from '@emotion/react'; | ||
import { Avatar, AvatarProps } from 'antd'; | ||
|
||
const ColorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae']; | ||
|
||
export default function PlayerAvatar ({ player, importCss, ...restProps } : { player: PlayerInfoType; importCss?: string } & AvatarProps) { | ||
return <> | ||
<Avatar | ||
css={css` | ||
background-color: ${ColorList[(player.position % 4)]}; | ||
text-align: center; | ||
${importCss} | ||
`} | ||
{ | ||
...restProps | ||
}> | ||
{player.name[0].toLocaleUpperCase()} | ||
</Avatar> | ||
</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Badge, Button, Popconfirm } from 'antd'; | ||
import { CommentOutlined } from '@ant-design/icons'; | ||
import 'intro.js/introjs.css'; | ||
import { getFloatCss } from '../../styles/playRoom.js'; | ||
import ChatRoom from '@/component/chatRoom/index.js'; | ||
import { useChatMessageStore } from '../../store/chatMessage.js'; | ||
import { useState } from 'react'; | ||
|
||
export default function TalkRommButton () { | ||
const { chatMessage: { unReadNum }, readMsg } = useChatMessageStore(); | ||
const [open, setOpen] = useState(false); | ||
|
||
return <> | ||
<Popconfirm | ||
title="聊天室" | ||
description={<ChatRoom/>} | ||
okText="关闭" | ||
showCancel={false} | ||
placement='bottomLeft' | ||
icon={<></>} | ||
open={open} | ||
onOpenChange={() => { | ||
setOpen(pre => !pre); | ||
readMsg(); | ||
}} | ||
> | ||
<Badge css={getFloatCss(true, 1)} count={open ? 0 : unReadNum}> | ||
<Button | ||
icon={<CommentOutlined />} | ||
type='primary' | ||
shape='circle' | ||
data-intro="此处为聊天室" | ||
/> | ||
</Badge> | ||
</Popconfirm> | ||
</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useEffect } from 'react'; | ||
import { Socket } from 'socket.io-client'; | ||
import { useChatMessageStore } from '../store/chatMessage.js'; | ||
import { ChatMessageType } from '../type.js'; | ||
|
||
/** set room infomation */ | ||
export default function useChatMessage (socket: Socket | void) { | ||
const { addMessage } = useChatMessageStore(); | ||
|
||
useEffect(() => { | ||
if (socket) { | ||
socket.on('receiveMessage', (msg: Omit<ChatMessageType, 'key'>) => { | ||
console.log(msg); | ||
addMessage(msg); | ||
}); | ||
} | ||
}, [socket]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { create } from 'zustand'; | ||
import { ChatMessageType } from '../type.js'; | ||
|
||
interface ChatMessageStore { | ||
chatMessage: { | ||
info: ChatMessageType[]; | ||
unReadNum: number; | ||
}, | ||
addMessage: (msg: Omit<ChatMessageType, 'key'>) => void; | ||
readMsg: () => void; | ||
} | ||
|
||
export const useChatMessageStore = create<ChatMessageStore>((set) => ({ | ||
chatMessage: { | ||
info: [], | ||
unReadNum: 0, | ||
}, | ||
addMessage: (msg) => set(({ chatMessage }) => { | ||
return ({ | ||
chatMessage: { | ||
info: [...chatMessage.info, { ...msg, key: chatMessage.info.length }], | ||
unReadNum: chatMessage.unReadNum + 1, | ||
}, | ||
}); | ||
}), | ||
readMsg:() => set(({ chatMessage }) => { | ||
return ({ | ||
chatMessage: { | ||
...chatMessage, | ||
unReadNum: 0, | ||
}, | ||
}); | ||
}), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters