-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGameMenu.tsx
More file actions
208 lines (195 loc) · 6.23 KB
/
Copy pathGameMenu.tsx
File metadata and controls
208 lines (195 loc) · 6.23 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
206
207
208
import { Button, Menu } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import { notifications } from '@mantine/notifications';
import {
IconChevronDown,
IconCircleFilled,
IconDeviceFloppy,
IconDeviceGamepad,
IconDownload,
IconList,
IconPencil,
IconPlus,
IconSettings,
} from '@tabler/icons-react';
import cx from 'clsx';
import { useCallback, useEffect, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { v4 } from 'uuid';
import { useSession } from '@/auth/authSelectors';
import { useShallowStore, useStore } from '@/core/zustand';
import { loadRemoteGame } from '@/games/save/loadRemoteGame';
import { loadRemoteGamesList } from '@/games/save/loadRemoteGamesList';
import { saveRemoteGame } from '@/games/save/saveRemoteGame';
import { openGameSettingsModal } from '@/games/settings/GameSettingsModal';
import { GameDetailModal } from './GameDetailModal';
import classes from './GameMenu.module.css';
export interface IGameMenuProps {}
function useGameOptions() {
const gamesIds = useShallowStore(state =>
Object.values(state.games.games).map(game => game.id),
);
const gameNames = useShallowStore(state =>
Object.values(state.games.games).map(game => game.name),
);
return useMemo(() => {
return gamesIds.map((id, index) => {
return {
label: gameNames[index],
value: id,
};
});
}, [gamesIds, gameNames]);
}
export function GameMenu(props: IGameMenuProps) {
const gameName = useStore(
state => state.games.games[state.games.selected ?? '']?.name,
);
const session = useSession();
const selectedId = useStore(state => state.games.selected);
const isSelectedSavedOnRemote = useStore(
state => !!state.games.games[selectedId ?? '']?.savedId,
);
const isSaving = useStore(state => state.gameSave.isSaving);
const navigate = useNavigate();
const [opened, { toggle, open, close }] = useDisclosure();
const gameOptions = useGameOptions();
// biome-ignore lint/correctness/useExhaustiveDependencies: intentional
useEffect(() => {
loadRemoteGamesList().catch(console.error);
}, [session?.user.id]);
const handleSaveGame = useCallback(
async (gameId: string | null) => {
if (!gameId) {
return;
}
if (!session) {
notifications.show({
color: 'red',
title: 'Login to save games',
message: 'You need to login to save games',
icon: <IconDeviceFloppy size={16} />,
});
return;
}
await saveRemoteGame(selectedId);
},
[selectedId, session],
);
const handleLoadGame = useCallback(async (gameId: string) => {
await loadRemoteGame(gameId, { override: true });
}, []);
return (
<>
<Button.Group data-tutorial-id="games-menu">
<Menu>
<Menu.Target>
<Button
data-tutorial-id="games-menu-trigger"
variant="light"
color="gray"
leftSection={<IconDeviceGamepad size={16} />}
rightSection={<IconChevronDown size={12} stroke={1.5} />}
>
{gameName ?? 'Select game'}
</Button>
</Menu.Target>
<Menu.Dropdown data-tutorial-id="games-menu-dropdown">
<Menu.Label>Change game</Menu.Label>
{gameOptions.map(option => (
<Menu.Item
key={option.value}
leftSection={<IconDeviceGamepad size={16} />}
onClick={() => {
useStore.getState().selectGame(option.value);
navigate(`/factories`);
}}
rightSection={
selectedId === option.value && (
<IconCircleFilled
size={8}
color="var(--mantine-color-green-7)"
/>
)
}
>
{option.label}
</Menu.Item>
))}
<Menu.Item
onClick={() => {
useStore.getState().createGame(v4(), {
name:
'New Game ' +
(Object.keys(useStore.getState().games.games).length + 1),
});
}}
leftSection={<IconPlus color="orange" size={16} />}
>
New game
</Menu.Item>
<Menu.Divider />
<Menu.Label>Game actions</Menu.Label>
<Menu.Item
leftSection={
<IconPencil color="var(--mantine-color-blue-3)" size={16} />
}
onClick={() => {
open();
}}
>
Rename game
</Menu.Item>
<Menu.Item
leftSection={
<IconSettings color="var(--mantine-color-gray-5)" size={16} />
}
onClick={openGameSettingsModal}
>
Game settings
</Menu.Item>
<Menu.Item
leftSection={<IconDeviceFloppy size={16} />}
onClick={() => handleSaveGame(selectedId)}
>
Save game
</Menu.Item>
{selectedId && isSelectedSavedOnRemote && (
<Menu.Item
leftSection={<IconDownload size={16} />}
onClick={() => handleLoadGame(selectedId)}
>
Load last save
</Menu.Item>
)}
<Menu.Divider />
<Menu.Item
data-tutorial-id="games-menu-list"
leftSection={<IconList size={16} />}
onClick={() => {
navigate(`/games`);
}}
>
Games list
</Menu.Item>
</Menu.Dropdown>
</Menu>
<Button
data-tutorial-id="game-save-button"
className={cx(classes.gameMenuSecondaryButton)}
variant="light"
color="gray"
loading={isSaving}
onClick={() => {
handleSaveGame(selectedId);
}}
>
<IconDeviceFloppy size={16} />
</Button>
</Button.Group>
{selectedId && (
<GameDetailModal opened={opened} close={close} gameId={selectedId} />
)}
</>
);
}