Commit 948abcbd authored by Chunchi Che's avatar Chunchi Che

fix chatHook

parent 842ce3a0
Pipeline #23625 passed with stages
in 15 minutes and 52 seconds
...@@ -16,7 +16,7 @@ interface ChatItem { ...@@ -16,7 +16,7 @@ interface ChatItem {
export const ChatBox: React.FC = () => { export const ChatBox: React.FC = () => {
const { open } = useSnapshot(store); const { open } = useSnapshot(store);
const { dialogs, input, setInput, ref, onSend } = useChat(); const { dialogs, input, setInput, ref, onSend } = useChat(true);
const onClose = () => (store.open = false); const onClose = () => (store.open = false);
......
...@@ -5,7 +5,7 @@ import { useEffect, useRef, useState } from "react"; ...@@ -5,7 +5,7 @@ import { useEffect, useRef, useState } from "react";
import { useSnapshot } from "valtio"; import { useSnapshot } from "valtio";
import { sendChat } from "@/api"; import { sendChat } from "@/api";
import { chatStore, roomStore } from "@/stores"; import { chatStore, isMe, roomStore } from "@/stores";
interface ChatItem { interface ChatItem {
name: string; name: string;
...@@ -13,7 +13,7 @@ interface ChatItem { ...@@ -13,7 +13,7 @@ interface ChatItem {
content: string; content: string;
} }
export const useChat = () => { export const useChat = (isDuel: boolean = false) => {
const [chatList, setChatList] = useState<ChatItem[]>([]); const [chatList, setChatList] = useState<ChatItem[]>([]);
const [input, setInput] = useState<string | undefined>(undefined); const [input, setInput] = useState<string | undefined>(undefined);
const chat = useSnapshot(chatStore); const chat = useSnapshot(chatStore);
...@@ -36,15 +36,28 @@ export const useChat = () => { ...@@ -36,15 +36,28 @@ export const useChat = () => {
} }
}; };
// 获取消息发送者的名字
const getSenderName = (sender: number) => {
if (sender < roomStore.players.length) {
if (isDuel) {
// 决斗内和决斗外场景sender是不一样的
if (isMe(sender)) {
return roomStore.getMePlayer()?.name;
} else {
return roomStore.getOpPlayer()?.name;
}
} else {
return roomStore.players[sender]?.name;
}
} else if (sender <= 8 || (sender >= 11 && sender <= 19)) {
return "System";
}
};
useEffect(() => { useEffect(() => {
if (chatStore.sender >= 0 && chatStore.message.length !== 0) { if (chatStore.sender >= 0 && chatStore.message.length !== 0) {
const { sender } = chatStore; const { sender } = chatStore;
const name = const name = getSenderName(sender) ?? "?";
sender < roomStore.players.length
? roomStore.players[sender]?.name ?? "?"
: (sender > 8 && sender < 11) || sender > 19
? "?"
: "System";
setChatList((prev) => [ setChatList((prev) => [
...prev, ...prev,
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment