Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Neos
Commits
c84dcd0b
Commit
c84dcd0b
authored
Sep 18, 2023
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimize code
parent
e04e960c
Pipeline
#23503
passed with stages
in 13 minutes and 13 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
86 additions
and
105 deletions
+86
-105
src/hook/chat.ts
src/hook/chat.ts
+75
-0
src/hook/index.ts
src/hook/index.ts
+1
-0
src/ui/Duel/PlayMat/ChatBox/index.tsx
src/ui/Duel/PlayMat/ChatBox/index.tsx
+5
-46
src/ui/WaitRoom/Chat.tsx
src/ui/WaitRoom/Chat.tsx
+5
-59
No files found.
src/hook/chat.ts
0 → 100644
View file @
c84dcd0b
// 聊天框的通用Hook
import
{
OverlayScrollbarsComponentRef
}
from
"
overlayscrollbars-react
"
;
import
{
useEffect
,
useRef
,
useState
}
from
"
react
"
;
import
{
useSnapshot
}
from
"
valtio
"
;
import
{
sendChat
}
from
"
@/api
"
;
import
{
chatStore
,
roomStore
}
from
"
@/stores
"
;
interface
ChatItem
{
name
:
string
;
time
:
string
;
content
:
string
;
}
export
const
useChat
=
()
=>
{
const
[
chatList
,
setChatList
]
=
useState
<
ChatItem
[]
>
([]);
const
[
input
,
setInput
]
=
useState
<
string
|
undefined
>
(
undefined
);
const
chat
=
useSnapshot
(
chatStore
);
/** 滚动条的ref */
const
ref
=
useRef
<
OverlayScrollbarsComponentRef
<
"
div
"
>>
(
null
);
/** dialogs 滚动到最底下 */
const
scrollToBottom
=
()
=>
{
const
viewport
=
ref
.
current
?.
osInstance
()?.
elements
().
viewport
;
if
(
viewport
)
{
viewport
.
scrollTop
=
viewport
.
scrollHeight
;
}
};
/** 发信息 */
const
onSend
=
()
=>
{
if
(
input
!==
undefined
)
{
sendChat
(
input
);
setInput
(
""
);
}
};
useEffect
(()
=>
{
if
(
chatStore
.
sender
>=
0
&&
chatStore
.
message
.
length
!==
0
)
{
const
{
sender
}
=
chatStore
;
const
name
=
sender
<
roomStore
.
players
.
length
?
roomStore
.
players
[
sender
]?.
name
??
"
?
"
:
(
sender
>
8
&&
sender
<
11
)
||
sender
>
19
?
"
?
"
:
"
System
"
;
setChatList
((
prev
)
=>
[
...
prev
,
{
name
:
name
,
time
:
formatTimeToHHMMSS
(),
content
:
chatStore
.
message
,
},
]);
scrollToBottom
();
}
},
[
chat
]);
return
{
dialogs
:
chatList
,
input
,
setInput
,
ref
,
onSend
,
};
};
function
formatTimeToHHMMSS
()
{
const
now
=
new
Date
();
const
hours
=
String
(
now
.
getHours
()).
padStart
(
2
,
"
0
"
);
const
minutes
=
String
(
now
.
getMinutes
()).
padStart
(
2
,
"
0
"
);
const
seconds
=
String
(
now
.
getSeconds
()).
padStart
(
2
,
"
0
"
);
return
`
${
hours
}
:
${
minutes
}
:
${
seconds
}
`
;
}
src/hook/index.ts
View file @
c84dcd0b
export
*
from
"
./chat
"
;
export
*
from
"
./useEnv
"
;
src/ui/Duel/PlayMat/ChatBox/index.tsx
View file @
c84dcd0b
import
{
DownOutlined
}
from
"
@ant-design/icons
"
;
import
{
Button
,
Drawer
,
Input
}
from
"
antd
"
;
import
{
type
OverlayScrollbarsComponentRef
}
from
"
overlayscrollbars-react
"
;
import
React
,
{
useEffect
,
useRef
,
useState
}
from
"
react
"
;
import
React
from
"
react
"
;
import
{
proxy
,
useSnapshot
}
from
"
valtio
"
;
import
{
sendChat
}
from
"
@/api
"
;
import
{
chatStore
,
roomStore
}
from
"
@/stores
"
;
import
{
useChat
}
from
"
@/hook
"
;
import
{
IconFont
,
ScrollableArea
}
from
"
@/ui/Shared
"
;
import
styles
from
"
./index.module.scss
"
;
...
...
@@ -19,49 +17,10 @@ interface ChatItem {
export
const
ChatBox
:
React
.
FC
=
()
=>
{
const
{
open
}
=
useSnapshot
(
store
);
const
[
chatList
,
setChatList
]
=
useState
<
ChatItem
[]
>
([]);
const
[
input
,
setInput
]
=
useState
<
string
|
undefined
>
(
undefined
);
const
chat
=
useSnapshot
(
chatStore
);
useEffect
(()
=>
{
if
(
chatStore
.
sender
>=
0
&&
chatStore
.
message
.
length
!==
0
)
{
const
{
sender
}
=
chatStore
;
const
name
=
sender
<
roomStore
.
players
.
length
?
roomStore
.
players
[
sender
]?.
name
??
"
?
"
:
(
sender
>
8
&&
sender
<
11
)
||
sender
>
19
?
"
?
"
:
"
System
"
;
setChatList
((
prev
)
=>
[
...
prev
,
{
name
:
name
,
content
:
chatStore
.
message
,
},
]);
scrollToBottom
();
}
},
[
chat
]);
/** 滚动条的ref */
const
ref
=
useRef
<
OverlayScrollbarsComponentRef
<
"
div
"
>>
(
null
);
/** dialogs 滚动到最底下 */
const
scrollToBottom
=
()
=>
{
const
viewport
=
ref
.
current
?.
osInstance
()?.
elements
().
viewport
;
if
(
viewport
)
{
viewport
.
scrollTop
=
viewport
.
scrollHeight
;
}
};
const
{
dialogs
,
input
,
setInput
,
ref
,
onSend
}
=
useChat
();
const
onClose
=
()
=>
(
store
.
open
=
false
);
/** 发信息 */
const
onSend
=
()
=>
{
if
(
input
!==
undefined
)
{
sendChat
(
input
);
setInput
(
""
);
}
};
return
(
<
Drawer
open=
{
open
}
...
...
@@ -74,7 +33,7 @@ export const ChatBox: React.FC = () => {
>
<
div
className=
{
styles
.
container
}
>
<
ScrollableArea
className=
{
styles
.
dialogs
}
ref=
{
ref
}
>
{
chatList
.
map
((
item
,
idx
)
=>
(
{
dialogs
.
map
((
item
,
idx
)
=>
(
<
DialogItem
key=
{
idx
}
{
...
item
}
/>
))
}
</
ScrollableArea
>
...
...
src/ui/WaitRoom/Chat.tsx
View file @
c84dcd0b
import
{
Button
,
Input
}
from
"
antd
"
;
import
{
type
OverlayScrollbarsComponentRef
}
from
"
overlayscrollbars-react
"
;
import
{
useEffect
,
useRef
,
useState
}
from
"
react
"
;
import
{
useSnapshot
}
from
"
valtio
"
;
import
{
sendChat
}
from
"
@/api
"
;
import
{
chatStore
,
roomStore
}
from
"
@/stores
"
;
import
{
useChat
}
from
"
@/hook
"
;
import
{
IconFont
,
ScrollableArea
}
from
"
@/ui/Shared
"
;
import
styles
from
"
./Chat.module.scss
"
;
...
...
@@ -16,54 +12,12 @@ interface ChatItem {
}
export
const
Chat
:
React
.
FC
=
()
=>
{
const
[
chatlist
,
setChatlist
]
=
useState
<
ChatItem
[]
>
([]);
const
[
input
,
setInput
]
=
useState
<
string
|
undefined
>
(
undefined
);
const
chat
=
useSnapshot
(
chatStore
);
useEffect
(()
=>
{
if
(
chatStore
.
sender
>=
0
&&
chatStore
.
message
.
length
!==
0
)
{
const
{
sender
}
=
chatStore
;
const
name
=
sender
<
roomStore
.
players
.
length
?
roomStore
.
players
[
sender
]?.
name
??
"
?
"
:
(
sender
>
8
&&
sender
<
11
)
||
sender
>
19
?
"
?
"
:
"
System
"
;
setChatlist
((
prev
)
=>
[
...
prev
,
{
name
:
name
,
time
:
formatTimeToHHMMSS
(),
content
:
chatStore
.
message
,
},
]);
scrollToBottom
();
}
},
[
chat
]);
/** dialogs 滚动到最底下 */
const
scrollToBottom
=
()
=>
{
const
viewport
=
ref
.
current
?.
osInstance
()?.
elements
().
viewport
;
if
(
viewport
)
{
viewport
.
scrollTop
=
viewport
.
scrollHeight
;
}
};
/** 发信息 */
const
send
=
()
=>
{
if
(
input
!==
undefined
)
{
sendChat
(
input
);
setInput
(
""
);
}
};
/** 滚动条的ref */
const
ref
=
useRef
<
OverlayScrollbarsComponentRef
<
"
div
"
>>
(
null
);
const
{
dialogs
,
input
,
setInput
,
ref
,
onSend
}
=
useChat
();
return
(
<
div
className=
{
styles
.
chat
}
>
<
ScrollableArea
className=
{
styles
.
dialogs
}
ref=
{
ref
}
>
{
chatlist
.
map
((
item
,
idx
)
=>
(
{
dialogs
.
map
((
item
,
idx
)
=>
(
<
DialogItem
key=
{
idx
}
{
...
item
}
/>
))
}
</
ScrollableArea
>
...
...
@@ -76,13 +30,13 @@ export const Chat: React.FC = () => {
placeholder=
"请输入聊天内容"
onPressEnter=
{
(
e
)
=>
{
e
.
preventDefault
();
s
end
();
onS
end
();
}
}
/>
<
Button
type=
"text"
icon=
{
<
IconFont
type=
"icon-send"
size=
{
16
}
/>
}
onClick=
{
s
end
}
onClick=
{
onS
end
}
/>
</
div
>
</
div
>
...
...
@@ -100,11 +54,3 @@ const DialogItem: React.FC<ChatItem> = ({ name, time, content }) => {
</
div
>
);
};
function
formatTimeToHHMMSS
()
{
const
now
=
new
Date
();
const
hours
=
String
(
now
.
getHours
()).
padStart
(
2
,
"
0
"
);
const
minutes
=
String
(
now
.
getMinutes
()).
padStart
(
2
,
"
0
"
);
const
seconds
=
String
(
now
.
getSeconds
()).
padStart
(
2
,
"
0
"
);
return
`
${
hours
}
:
${
minutes
}
:
${
seconds
}
`
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment