-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (99 loc) · 3.63 KB
/
index.js
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
// @flow
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Game from './Game';
import GameHeader from './GameHeader';
import GameStatsModal from './GameStatsModal';
import HowToPlayModal from './HowToPlayModal';
import YesterdayModal from './YesterdayModal';
import {getCurrentWordPair, getTodaySpecialMessageData} from './wordPairs4.js';
import {getNumDaysSinceEpoch} from './utils.js';
import {getHasSolvedToday} from './userStatsUtils.js';
import {logEventWithWordsLabel} from './loggingUtils.js';
ReactDOM.render(
<React.StrictMode>
<CreateGame />
</React.StrictMode>,
document.getElementById('app-container')
);
function CreateGame() {
const [startWord, endWord] = getCurrentWordPair();
const currentEpochDay = getNumDaysSinceEpoch();
const hasSolvedToday = getHasSolvedToday();
const [isHowToPlayModalOpen, setIsHowToPlayModalOpen] = useState(false);
const [isYesterdayModalOpen, setIsYesterdayModalOpen] = useState(false);
const [isGameStatsModalOpen, setIsGameStatsModalOpen] = useState(hasSolvedToday);
return (
<div id='game-container'>
<GameHeader
onClickHowToPlay={() => {
logEventWithWordsLabel('click_how_to_play');
setIsHowToPlayModalOpen(true);
}}
onClickYesterday={() => {
logEventWithWordsLabel('click_yesterday');
setIsYesterdayModalOpen(true);
}}
onClickUserStats={() => {
logEventWithWordsLabel('click_user_stats');
setIsGameStatsModalOpen(true);
}}
/>
<Game
startWord={startWord}
endWord={endWord}
gameEpochDay={currentEpochDay}
onGameLoad={() => setSpecialMessageToast()}
openGameStatsModal={() => setIsGameStatsModalOpen(true)}
setErrorToast={(message) => setErrorToast(message)}
setSuccessToast={(message) => setSuccessToast(message)}
/>
<HowToPlayModal
isOpen={isHowToPlayModalOpen}
onClose={() => setIsHowToPlayModalOpen(false)}
/>
<YesterdayModal
isOpen={isYesterdayModalOpen}
onClose={() => setIsYesterdayModalOpen(false)}
/>
<GameStatsModal
isOpen={isGameStatsModalOpen}
onClose={() => setIsGameStatsModalOpen(false)}
onCopyToClipboard={() => setGenericToast('Copied to clipboard')}
/>
<div className='toast aboveKeyboardToast' id='error-toast' />
<div className='toast aboveKeyboardToast' id='success-toast' />
<div className='toast aboveKeyboardToast' id='generic-toast' />
<div className='toast belowHeaderToast' id='special-message-toast' />
</div>
);
}
function setErrorToast(message) {
setToast('error-toast', message, 2000);
}
function setSuccessToast(message) {
setToast('success-toast', message, 2000);
}
function setGenericToast(message) {
setToast('generic-toast', message, 2000);
}
function setSpecialMessageToast() {
const [todaySpecialMessage, todaySpecialMessageBackgroundColor] = getTodaySpecialMessageData();
if (todaySpecialMessage != null) {
if (todaySpecialMessageBackgroundColor != null) {
const specialMessageToastElement = document.getElementById('special-message-toast');
specialMessageToastElement.style.setProperty('background-color', todaySpecialMessageBackgroundColor);
}
setToast('special-message-toast', todaySpecialMessage, 3000);
}
}
function setToast(elementId, message, duration) {
const element = document.getElementById(elementId);
element.innerText = message;
element.style.display = 'block';
setTimeout(() => {
element.style.display = 'none';
element.innerText = null;
}, duration);
}