Merge pull request #2579 from MartinFournier/fix/duplicate-previous-pages

Fix navigation issue (duplicate previous pages in history)
This commit is contained in:
hydroflame 2022-01-12 11:15:25 -05:00 committed by GitHub
commit 48c1386e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { cloneDeep } from "lodash"; import { cloneDeep, isEqual } from "lodash";
import { IPlayer } from "../PersonObjects/IPlayer"; import { IPlayer } from "../PersonObjects/IPlayer";
import { IEngine } from "../IEngine"; import { IEngine } from "../IEngine";
import { ITerminal } from "../Terminal/ITerminal"; import { ITerminal } from "../Terminal/ITerminal";
@ -251,11 +251,16 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
} }
function setCurrentPage(page: Page, ...args: any): void { function setCurrentPage(page: Page, ...args: any): void {
const newPage = { page, args: cloneDeep(args) };
const previousPage = pageHistory[0];
const isDifferentThenPrevious = page !== previousPage?.page || !isEqual([...args], previousPage?.args);
if (isDifferentThenPrevious) {
const history = [ const history = [
{ page, args: cloneDeep(args) }, newPage,
...pageHistory ...pageHistory
].slice(0, 20); ].slice(0, 20);
setPageHistory(history) setPageHistory(history)
}
setPage(page) setPage(page)
} }