-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 그리드 아이템 컴포넌트로 분리 * fix: type 변경 - isMine 추가 * feat: 그리드 아이템 컴포넌트 분리, 유저 입력 로직 추가 - '/' 키 입력 시 말풍선 등장 - 'enter' 키 입력 시 말풍선 제거 * feat: 퀴즈 대기 페이지 채팅 기능 추가 * feat: my position 이벤트 추가 - myPosition 상태를 통해 본인의 위치를 파악 * feat: 채팅 이벤트 추가 및 닉네임 이벤트 수정 * fix: 햄스터 캐릭터 png 수정 * style: 그리드 뷰 스타일 수정 * feat: 새로고침 시 connect 될 때 my position 이벤트 emit --------- Co-authored-by: byeong <[email protected]>
- Loading branch information
1 parent
27c86d0
commit 13577f7
Showing
5 changed files
with
183 additions
and
54 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
121 changes: 121 additions & 0 deletions
121
packages/client/src/pages/quiz-wait/ui/UserGridItem.tsx
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,121 @@ | ||
import { useRef, useEffect, useState } from 'react'; | ||
|
||
import { Guest } from '../index'; | ||
import { getQuizSocket } from '@/shared/utils/socket'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
interface UserGridItemProps { | ||
participant: Guest; | ||
isMine: boolean; | ||
otherMessage: string | undefined; | ||
} | ||
|
||
const characterNames = ['강아지', '고양이', '돼지', '토끼', '펭귄', '햄스터']; | ||
|
||
const randomColor = [ | ||
'bg-gradient-to-br from-blue-400/80 to-blue-500/80 text-white', | ||
'bg-gradient-to-br from-pink-300/80 to-pink-400/80', | ||
'bg-gradient-to-br from-yellow-300/80 to-yellow-400/80', | ||
]; | ||
|
||
export default function UserGridItem({ participant, isMine, otherMessage }: UserGridItemProps) { | ||
// 내 메시지, 상대방들 메시지 | ||
const [message, setMessage] = useState(''); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const socket = getQuizSocket(); | ||
const { pinCode } = useParams(); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleMessageChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setMessage(e.target.value); | ||
socket.emit('message', { pinCode, message: e.target.value, position: participant.position }); | ||
}; | ||
|
||
const handleEnterKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
//메세지 전송 | ||
setIsFocused(false); | ||
|
||
setTimeout(() => { | ||
socket.emit('message', { | ||
pinCode, | ||
message: '', | ||
position: participant.position, | ||
}); | ||
setMessage(''); | ||
}, 1500); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const handleKeyDown = (e: KeyboardEvent) => { | ||
if (e.key === '/') { | ||
setIsFocused(true); | ||
setTimeout(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}, 100); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyDown); | ||
return () => { | ||
window.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div key={participant.position} className="relative w-full h-24 flex flex-col items-center"> | ||
{isMine && isFocused && ( | ||
<> | ||
<input | ||
ref={inputRef} | ||
value={message} | ||
onChange={handleMessageChange} | ||
onKeyDown={handleEnterKeyDown} | ||
className="absolute -top-7 rounded-md shadow-sm outline-none px-2 w-40 h-6 text-sm" | ||
/> | ||
<div | ||
className="absolute inset-0 -top-1 left-12 w-0 h-0 | ||
border-l-[8px] border-l-transparent | ||
border-t-[10px] border-t-white | ||
border-r-[8px] border-r-transparent | ||
drop-shadow-[0_0.7px_0.7px_rgba(0,0,0,0.1)] | ||
" | ||
/> | ||
</> | ||
)} | ||
{!isFocused && message && ( | ||
<div | ||
className={`flex items-center absolute -top-7 left-1/2 transform -translate-x-1/2 ${randomColor[participant.position % 3]} px-2 h-6 rounded-lg shadow-sm text-sm font-bold whitespace-nowrap`} | ||
> | ||
{message} | ||
</div> | ||
)} | ||
<div | ||
className={`relative w-20 h-20 rounded-full aspect-square flex items-center justify-center shadow-sm hover:shadow transition-shadow ${ | ||
isMine ? 'bg-blue-500' : 'bg-white' | ||
}`} | ||
> | ||
<img | ||
src={`/src/shared/assets/characters/${characterNames[participant.character]}.png`} | ||
alt={`${characterNames[participant.character]}character`} | ||
className="w-20 h-20 rounded-full" | ||
/> | ||
{!isMine && otherMessage && ( | ||
<div | ||
className={`flex items-center absolute -top-7 left-1/2 transform -translate-x-1/2 ${randomColor[participant.position % 3]} px-2 h-6 rounded-lg shadow-sm text-sm font-semibold whitespace-nowrap`} | ||
> | ||
{otherMessage} | ||
</div> | ||
)} | ||
</div> | ||
<div className="flex justify-center items-center gap-2 w-full mt-2"> | ||
<div className="w-[10px] h-[10px] rounded-full bg-green-500 animate-blink" /> | ||
<div className="text-sm text-center truncate font-bold">{participant.nickname}</div> | ||
</div> | ||
</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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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