Skip to content

Commit 2d4e76f

Browse files
committed
feat: Enhance game history pagination and state management
- Updated GameHistory component to support external pagination state. - Introduced internal pagination state management for better control. - Modified ModDashboard to lift game history pagination state, preserving it when viewing a game. - Implemented scroll position restoration when navigating back from game view.
1 parent d0053e9 commit 2d4e76f

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

components/gameHistory.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import styles from '../styles/gameHistory.module.css';
55
import Link from 'next/link';
66
import CountryFlag from './utils/countryFlag';
77

8-
export default function GameHistory({ session, onGameClick, targetUserSecret = null, targetUserData = null }) {
8+
export default function GameHistory({ session, onGameClick, targetUserSecret = null, targetUserData = null, page = null, setPage = null }) {
99
const { t: text } = useTranslation("common");
1010
const [games, setGames] = useState([]);
1111
const [loading, setLoading] = useState(true);
12+
13+
// Use external page state if provided, otherwise use internal state
14+
const [internalPage, setInternalPage] = useState(1);
15+
const currentPage = page !== null ? page : internalPage;
16+
const setCurrentPage = setPage !== null ? setPage : setInternalPage;
17+
1218
const [pagination, setPagination] = useState({
1319
currentPage: 1,
1420
totalPages: 1,
@@ -51,9 +57,9 @@ export default function GameHistory({ session, onGameClick, targetUserSecret = n
5157

5258
useEffect(() => {
5359
if (typeof window !== 'undefined' && session?.token?.secret && window.cConfig?.apiUrl) {
54-
fetchGames(1);
60+
fetchGames(currentPage);
5561
}
56-
}, [session?.token?.secret, targetUserSecret]);
62+
}, [session?.token?.secret, targetUserSecret, currentPage]);
5763

5864
const getGameTypeDisplay = (gameType) => {
5965
const types = {
@@ -281,7 +287,7 @@ export default function GameHistory({ session, onGameClick, targetUserSecret = n
281287
<button
282288
className={styles.paginationBtn}
283289
disabled={!pagination.hasPrevPage}
284-
onClick={() => fetchGames(pagination.currentPage - 1)}
290+
onClick={() => setCurrentPage(currentPage - 1)}
285291
>
286292
{text('previous')}
287293
</button>
@@ -296,7 +302,7 @@ export default function GameHistory({ session, onGameClick, targetUserSecret = n
296302
<button
297303
className={styles.paginationBtn}
298304
disabled={!pagination.hasNextPage}
299-
onClick={() => fetchGames(pagination.currentPage + 1)}
305+
onClick={() => setCurrentPage(currentPage + 1)}
300306
>
301307
{text('next')}
302308
</button>

components/modDashboard.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default function ModDashboard({ session }) {
3333
const [userReportsPage, setUserReportsPage] = useState(1);
3434
const USER_REPORTS_PER_PAGE = 10;
3535

36+
// Game history pagination (lifted from GameHistory to preserve state when viewing a game)
37+
const [gameHistoryPage, setGameHistoryPage] = useState(1);
38+
const [savedScrollPosition, setSavedScrollPosition] = useState(0);
39+
3640
// Action modal state
3741
const [actionModal, setActionModal] = useState(null);
3842
const [actionLoading, setActionLoading] = useState(false);
@@ -176,6 +180,7 @@ export default function ModDashboard({ session }) {
176180
setTargetUser(data.targetUser);
177181
setUserHistory(data.history);
178182
setUserReportsPage(1); // Reset reports pagination
183+
setGameHistoryPage(1); // Reset game history pagination
179184

180185
// Show notice if found by past name
181186
if (data.foundByPastName) {
@@ -195,6 +200,8 @@ export default function ModDashboard({ session }) {
195200
};
196201

197202
const handleGameClick = (game) => {
203+
// Save scroll position before viewing game
204+
setSavedScrollPosition(window.scrollY);
198205
setSelectedGame(game);
199206
};
200207

@@ -269,6 +276,7 @@ export default function ModDashboard({ session }) {
269276
setTargetUser(data.targetUser);
270277
setUserHistory(data.history);
271278
setUserReportsPage(1); // Reset reports pagination
279+
setGameHistoryPage(1); // Reset game history pagination
272280
// Update input if user was found by past name
273281
if (data.foundByPastName) {
274282
setUsernameInput(data.targetUser.username);
@@ -313,6 +321,7 @@ export default function ModDashboard({ session }) {
313321
setUserHistory(data.history);
314322
setUsernameInput(data.targetUser.username);
315323
setUserReportsPage(1); // Reset reports pagination
324+
setGameHistoryPage(1); // Reset game history pagination
316325
} else {
317326
const errorData = await response.json();
318327
setError(errorData.message || 'User not found');
@@ -1123,6 +1132,8 @@ export default function ModDashboard({ session }) {
11231132
onBack={() => {
11241133
setSelectedGame(null);
11251134
setReportedUserId(null);
1135+
// Restore scroll position after component re-renders
1136+
setTimeout(() => window.scrollTo(0, savedScrollPosition), 0);
11261137
}}
11271138
onUsernameLookup={handleUsernameLookup}
11281139
options={{
@@ -1355,9 +1366,8 @@ export default function ModDashboard({ session }) {
13551366
</div>
13561367
)}
13571368

1358-
{/* Main Dashboard */}
1359-
{!selectedGame && (
1360-
<div className={styles.modDashboard}>
1369+
{/* Main Dashboard - hidden instead of unmounted when viewing a game to preserve state */}
1370+
<div className={styles.modDashboard} style={{ display: selectedGame ? 'none' : undefined }}>
13611371
<div className={styles.header}>
13621372
<div className={styles.worldGuessrLogo}>
13631373
<div className={styles.logoIcon}>🌍</div>
@@ -1647,6 +1657,8 @@ export default function ModDashboard({ session }) {
16471657
targetUserSecret={targetUser.secret}
16481658
targetUserData={targetUser}
16491659
onGameClick={handleGameClick}
1660+
page={gameHistoryPage}
1661+
setPage={setGameHistoryPage}
16501662
/>
16511663
</div>
16521664
) : (
@@ -2229,7 +2241,6 @@ export default function ModDashboard({ session }) {
22292241
</div>
22302242
)}
22312243
</div>
2232-
)}
22332244
</>
22342245
);
22352246
}

0 commit comments

Comments
 (0)