-
Notifications
You must be signed in to change notification settings - Fork 26
/
new-game.tsx
256 lines (241 loc) · 7.21 KB
/
new-game.tsx
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import Button from "../components/Button";
import { IGameOptions, defaultOptions, ILanguage, IGame } from "../lib/game";
import { useCallback, useEffect, useState } from "react";
import { useNewGame } from "../hooks/game";
import classnames from "classnames";
import { useTranslation } from "react-i18next";
import LanguageSelector from "../components/LanguageSelector";
import { useRouter } from "next/router";
import { i18n } from "../lib/i18n";
import { debounce } from "lodash";
const NewGame = () => {
const newGame = useNewGame();
const router = useRouter();
const [loading, setLoading] = useState(false);
const [options, setOptions] = useState<IGameOptions>({
...defaultOptions,
language: (router.locale as ILanguage) || "en",
});
useEffect(() => {
i18n.changeLanguage(router.locale as ILanguage);
}, []);
const { t } = useTranslation();
return (
<div className="flex flex-col items-center w-screen min-h-screen p-6 bg-gray-100">
<div className="absolute top-0 right-0 m-4">
<LanguageSelector />
</div>
<h1 className="mt-6 mb-4 font-mono h1">codegame.cards</h1>
<h2 className="mt-6 mb-4 text-xl font-bold">
{t("choose-options", "Choose your options")}
</h2>
<Form
className="w-full max-w-md mb-4"
options={options}
setOptions={setOptions}
/>
<Button
className="flex items-center justify-center p-6"
disabled={
loading ||
(options.onlyUseCustomWords && options.customWords.length < 25)
}
onClick={async () => {
setLoading(true);
try {
await newGame(options);
} catch (error) {
setLoading(false);
}
}}
>
{t("create-game", "Create game")}
</Button>
</div>
);
};
export default NewGame;
const Form = ({
className,
options,
setOptions,
}: {
className: string;
options: IGameOptions;
setOptions: (options: IGameOptions) => void;
}) => {
const { t } = useTranslation();
return (
<form className={className}>
<div className="w-auto mb-2">
<h2 className="mb-2 font-semibold">{t("dictionary", "Dictionary")}</h2>
<MultipleChoice
options={[
{
label: "🇬🇧 English",
value: "en",
},
{
label: "🇫🇷 Français",
value: "fr",
},
{
label: "🇩🇪 Deutsch",
value: "de",
},
{
label: "🇪🇸 Español",
value: "es",
},
{
label: "🇷🇺 Русский",
value: "ru",
},
{
label: "🇭🇺 Magyar",
value: "hu",
},
{
label: "🇧🇷 Português",
value: "ptbr",
},
{
label: "🔥 Emoji",
value: "emoji",
},
]}
onClick={(language) => setOptions({ ...options, language })}
selected={options.language}
/>
</div>
<div className="w-auto mb-2">
<h2 className="mb-2 font-semibold">{t("mode", "Mode")}</h2>
<MultipleChoice
options={[
{ value: "duet", label: `👥 ${t("duet", "Duet")}` },
{ value: "classic", label: `👨👩👧👦 ${t("classic", "Classic")}` },
]}
onClick={(mode) => setOptions({ ...options, mode })}
selected={options.mode}
/>
</div>
<div className="w-auto mb-2">
<h2 className="mb-2 font-semibold">
{t("public-game", "Public game")}
</h2>
<MultipleChoice
options={[
{ value: "private", label: `🔒 ${t("private", "Private")}` },
{ value: "public", label: `🖖 ${t("public", "Public")}` },
]}
onClick={(p) => setOptions({ ...options, private: p })}
selected={options.private}
/>
</div>
<CustomWordsInput options={options} setOptions={setOptions} />
</form>
);
};
interface IMultipleChoice {
options: Array<{ value: string; label: string }>;
onChange: (value: string) => void;
selected: string;
}
const MultipleChoice = ({ options, onClick, selected }) => {
return (
<div className="grid grid-cols-2 gap-2">
{options.map((opt) => (
<div
key={opt.value}
className={classnames(
"rounded px-2 py-1 cursor-pointer text-center font-bold text-white border-b-4 w-full",
{
"bg-green-600 border-green-800": selected === opt.value,
"bg-blue-500 border-blue-700 hover:bg-blue-400 hover:border-blue-600":
selected !== opt.value,
}
)}
onClick={() => onClick(opt.value)}
>
{opt.label}
</div>
))}
</div>
);
};
const CustomWordsInput = ({
options,
setOptions,
}: {
options: IGameOptions;
setOptions: (opts: IGameOptions) => void;
}) => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const { t } = useTranslation();
const debouncedSetWords = useCallback(
debounce((value: string) => {
setOptions({
...options,
customWords: value
.split(",")
.map((s) => s.trim())
.filter((s) => !!s),
});
}, 300),
[]
);
return (
<div className="w-auto py-2">
<button
className="mb-2 text-blue-800 underline cursor-pointer hover:text-blue-700"
type="button"
onClick={() => setIsOpen(!isOpen)}
>
{t("advanced", "advanced...")}
</button>
{isOpen && (
<div>
<h2 className="font-semibold">{t("custom-words", "Custom words")}</h2>
<p className="mb-2 text-sm text-gray-800">
{t(
"custom-words-notice",
"Enter a comma-separated list of custom words. Uncheck the box below to use those words exclusively."
)}
</p>
<label className="inline-block mb-2 cursor-pointer">
<input
type="checkbox"
className="-mt-1 rounded"
checked={!options.onlyUseCustomWords}
onChange={(e) => {
setOptions({
...options,
onlyUseCustomWords: !e.target.checked,
});
}}
></input>
<span className="ml-2 text-gray-800">
{t("custom-words-checkbox", "Merge with the dictionnary")}
</span>
{options.onlyUseCustomWords && options.customWords.length < 25 && (
<p className="text-sm text-red-600">
{t(
"custom-words-warning",
"You need at least 25 words to play with custom words only."
)}
</p>
)}
</label>
<textarea
onChange={(e) => debouncedSetWords(e.target.value)}
className="block w-full rounded"
placeholder={t(
"custom-words-placeholder",
"harder, better, faster, stronger"
)}
></textarea>
</div>
)}
</div>
);
};