-
Notifications
You must be signed in to change notification settings - Fork 626
Expand file tree
/
Copy pathMovesTable.tsx
More file actions
205 lines (199 loc) · 6.89 KB
/
Copy pathMovesTable.tsx
File metadata and controls
205 lines (199 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
isBoardFlippedAtom,
movesAtom,
takebackAlertAtom,
userSelectedMoveIndexAtom,
} from '@repo/store/chessBoard';
import { Move } from 'chess.js';
import { useEffect, useRef } from 'react';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import {
HandshakeIcon,
FlagIcon,
ChevronFirst,
ChevronLast,
ChevronLeft,
ChevronRight,
RefreshCw,
Undo,
Check,
X
} from 'lucide-react';
import { useSocket } from '../hooks/useSocket';
import { PROPOSE_A_TAKEBACK, TAKEBACK } from '../screens/Game';
const MovesTable = ({ gameId, userId }: { gameId: string; userId: string; }) => {
const socket = useSocket()
const [takebackAlert, settakebackAlert] = useRecoilState(takebackAlertAtom);
const [userSelectedMoveIndex, setUserSelectedMoveIndex] = useRecoilState(
userSelectedMoveIndexAtom,
);
const setIsFlipped = useSetRecoilState(isBoardFlippedAtom);
const moves = useRecoilValue(movesAtom);
const movesTableRef = useRef<HTMLInputElement>(null);
const movesArray = moves.reduce((result, _, index: number, array: Move[]) => {
if (index % 2 === 0) {
result.push(array.slice(index, index + 2));
}
return result;
}, [] as Move[][]);
useEffect(() => {
if (movesTableRef && movesTableRef.current) {
movesTableRef.current.scrollTo({
top: movesTableRef.current.scrollHeight,
behavior: 'smooth',
});
}
}, [moves]);
return (
<div className="text-[#C3C3C0] relative w-full ">
<div
className="text-sm h-[45vh] max-h-[45vh] overflow-y-auto"
ref={movesTableRef}
>
{movesArray.map((movePairs, index) => {
return (
<div
key={index}
className={`w-full py-px px-4 font-bold items-stretch ${index % 2 !== 0 ? 'bg-[#2B2927]' : ''}`}
>
<div className="grid grid-cols-6 gap-16 w-4/5">
<span className="text-[#C3C3C0] px-2 py-1.5">{`${index + 1}.`}</span>
{movePairs.map((move, movePairIndex) => {
const isLastIndex =
movePairIndex === movePairs.length - 1 &&
movesArray.length - 1 === index;
const isHighlighted =
userSelectedMoveIndex !== null
? userSelectedMoveIndex === index * 2 + movePairIndex
: isLastIndex;
const { san } = move;
return (
<div
key={movePairIndex}
className={`col-span-2 cursor-pointer flex items-center w-full pl-1 ${isHighlighted ? 'bg-[#484644] rounded border-b-[#5A5858] border-b-[3px]' : ''}`}
onClick={() => {
setUserSelectedMoveIndex(index * 2 + movePairIndex);
}}
>
<span className="text-[#C3C3C0]">{san}</span>
</div>
);
})}
</div>
</div>
);
})}
</div>
{moves.length ? (
<>
<div className="w-full p-2 bg-[#20211D] flex items-center justify-between">
<div className="flex gap-4">
<button className="flex items-center gap-2 hover:bg-[#32302E] rounded px-2.5 py-1" onClick={() => {
socket?.send(
JSON.stringify({
type: PROPOSE_A_TAKEBACK,
payload: {
gameId,
userId
}
}));
}}>
{<Undo size={16} />}
Takeback
</button>
<button className="flex items-center gap-2 hover:bg-[#32302E] rounded px-2.5 py-1">
{<HandshakeIcon size={16} />}
Draw
</button>
<button className="flex items-center gap-2 hover:bg-[#32302E] rounded px-2.5 py-1">
{<FlagIcon size={16} />}
Resign
</button>
</div>
<div className="flex gap-1">
<button
onClick={() => {
setUserSelectedMoveIndex(0);
}}
disabled={userSelectedMoveIndex === 0}
className="hover:text-white"
title="Go to first move"
>
<ChevronFirst />
</button>
<button
onClick={() => {
setUserSelectedMoveIndex((prev) =>
prev !== null ? prev - 1 : moves.length - 2,
);
}}
disabled={userSelectedMoveIndex === 0}
className="hover:text-white"
>
<ChevronLeft />
</button>
<button
onClick={() => {
setUserSelectedMoveIndex((prev) =>
prev !== null
? prev + 1 >= moves.length - 1
? moves.length - 1
: prev + 1
: null,
);
}}
disabled={userSelectedMoveIndex === null}
className="hover:text-white"
>
<ChevronRight />
</button>
<button
onClick={() => {
setUserSelectedMoveIndex(moves.length - 1);
}}
disabled={userSelectedMoveIndex === null}
className="hover:text-white"
title="Go to last move"
>
<ChevronLast />
</button>
<button
onClick={() => {
setIsFlipped((prev) => !prev);
}}
title="Flip the board"
>
<RefreshCw className="hover:text-white mx-2" size={18} />
</button>
</div>
</div>
{takebackAlert &&
<div className="flex items-center justify-center mt-4">
<div className="flex items-center justify-center bg-green-500 text-white w-8 h-8 rounded-full mr-2 cursor-pointer
" onClick={() => {
socket?.send(JSON.stringify({
type: TAKEBACK,
payload: {
gameId,
userId
}
}));
settakebackAlert(null)
}}>
<Check />
</div>
<div className="text-white mx-2">{takebackAlert}</div>
<div className="flex items-center justify-center bg-red-500 text-white w-8 h-8 rounded-full ml-2 cursor-pointer
" onClick={() => {
settakebackAlert(null)
}}>
<X />
</div>
</div>
}
</>
) : null}
</div>
);
};
export default MovesTable;