Skip to content

Commit 68b835c

Browse files
committed
fix an issue where the chat ghostbar is in the wrong position after game end
1 parent 0f337c5 commit 68b835c

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/main/client/src/component/Chat.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import {
1010
} from "tailwind-merge"
1111
import {
1212
useAuthStore,
13+
useChatStore,
1314
} from "src/store.js"
1415
import {
1516
StompContext,
1617
tfetch,
1718
doTry,
1819
getRemInPixel,
20+
DELTA,
1921
} from "src/util.js"
2022

2123
export const Chat = ({chatId, className}) => {
@@ -139,6 +141,15 @@ function SplitPane({className, messageRef, topElement, bottomElement}) {
139141
let splitPosRef = useRef()
140142
splitPosRef.current = splitPos
141143
let containerRef = useRef()
144+
let chatState = useChatStore(state => state.chatState)
145+
146+
// force re-render: sidebar layout may have changed
147+
useEffect(() => {
148+
let splitPos = splitPosRef.current
149+
if (containerRef.current) {
150+
setTimeout(() => setSplitPos(getSplitPos(chatState ? splitPos + DELTA : splitPos - DELTA), containerRef.current), 0)
151+
}
152+
}, [chatState])
142153

143154
useEffect(() => {
144155
let onMouseMove = (e) => {
@@ -162,7 +173,7 @@ function SplitPane({className, messageRef, topElement, bottomElement}) {
162173
let topElementHeight = Math.trunc(splitPos)
163174
if (containerRef.current) {
164175
let rect = containerRef.current.getBoundingClientRect()
165-
topElementHeight = Math.trunc(splitPos - rect.top)
176+
topElementHeight = Math.trunc(getSplitPos(splitPos, containerRef.current) - rect.top)
166177
}
167178
return (
168179
<div
@@ -208,7 +219,7 @@ function SplitBar({splitPos, dragOffset, onMouseDown, container}) {
208219
<div
209220
onMouseDown={Number.isNaN(dragOffset) ? onMouseDown : undefined}
210221
style={{
211-
top: Math.trunc(splitPos) - 1,
222+
top: Math.trunc(getSplitPos(splitPos, container)) - 1,
212223
height: Math.trunc(getRemInPixel() * 0.5) + 2,
213224
width: rect.width,
214225
left: rect.left - parentRect.left,

src/main/client/src/feature/game/Game.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import {
3535
useAuthStore,
3636
useMuteStore,
37+
useChatStore,
3738
} from "src/store.js"
3839
import {
3940
useLayoutStore,
@@ -65,6 +66,7 @@ export const Game = () => {
6566
let [gameState, setGameState] = useState(initialState())
6667
let zoom = useViewStateStore(state => state.zoom)
6768
let {gameId} = useParams()
69+
let toggleChatState = useChatStore(state => state.toggleChatState)
6870
let navigate = useNavigate()
6971
let stompClient = useContext(StompContext)
7072
let auth = useAuthStore(state => state.auth)
@@ -215,6 +217,7 @@ export const Game = () => {
215217
}
216218
if (!isSelfPlay(gameState)) { // myColor is 0 in self play
217219
setGameState(addMove(gameState, {...move, color: myColor}))
220+
toggleChatState()
218221
}
219222
playClickSound()
220223
stompClient.publish({
@@ -281,13 +284,15 @@ export const Game = () => {
281284
},
282285
})
283286
setGameState(createGameState(game, auth))
287+
toggleChatState()
284288
}, () => navigate(base + "/lobby"))
285289
}, [queueStatus, auth, id, gameId, navigate])
286290

287291
useEffect(() => {
288292
let sub = stompClient.subscribe("/topic/move/" + gameId, (message) => {
289293
let move = JSON.parse(message.body)
290294
setGameState(addMove(gameState, move))
295+
toggleChatState()
291296
})
292297
return sub.unsubscribe
293298
}, [gameState, stompClient, gameId])

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,5 @@ function getScore(board) {
195195
}
196196
}
197197
}
198-
return { w, b }
198+
return {w, b}
199199
}

src/main/client/src/store.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ export const useMuteStore = create(
4242
muted: mute
4343
}))
4444
}),
45-
{ name: "mute-storage" }
45+
{ name: "mute-storage" },
4646
)
4747
)
48+
49+
// trick to re-render the chat
50+
export const useChatStore = create((set, get) => ({
51+
chatState: false,
52+
toggleChatState: () =>
53+
set(() => ({
54+
chatState: !get().chatState
55+
}))
56+
}),
57+
)

0 commit comments

Comments
 (0)