From 218dcfe7a02184fe059592aec99ad5e382f80aae Mon Sep 17 00:00:00 2001
From: zkldi <20380519+zkldi@users.noreply.github.com>
Date: Wed, 24 Nov 2021 02:53:55 +0000
Subject: [PATCH] Try out history safe-go-back
---
client/src/app/pages/ErrorPage.tsx | 8 ++------
client/src/app/pages/ForgotPasswordPage.tsx | 3 ++-
client/src/app/pages/LoginPage.tsx | 3 ++-
client/src/app/pages/RegisterPage.tsx | 3 ++-
client/src/app/routes/AppRoutes.tsx | 3 ++-
client/src/util/misc.ts | 9 +++++++++
6 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/client/src/app/pages/ErrorPage.tsx b/client/src/app/pages/ErrorPage.tsx
index 6a8d2a100..24afa4e65 100644
--- a/client/src/app/pages/ErrorPage.tsx
+++ b/client/src/app/pages/ErrorPage.tsx
@@ -2,6 +2,7 @@ import useSetSubheader from "components/layout/header/useSetSubheader";
import React from "react";
import { useHistory } from "react-router-dom";
import { ToCDNURL } from "util/api";
+import { HistorySafeGoBack } from "util/misc";
export function ErrorPage({
statusCode,
@@ -72,12 +73,7 @@ export function ErrorPage({
{statusCode || "Completely Screwed."}
{message}
-
- history.length === 1 ? history.replace("/") : history.goBack()
- }
- >
+ HistorySafeGoBack(history)}>
Go Back.
diff --git a/client/src/app/pages/ForgotPasswordPage.tsx b/client/src/app/pages/ForgotPasswordPage.tsx
index 55892b92a..555376d92 100644
--- a/client/src/app/pages/ForgotPasswordPage.tsx
+++ b/client/src/app/pages/ForgotPasswordPage.tsx
@@ -5,6 +5,7 @@ import React, { useState } from "react";
import { Link, useHistory } from "react-router-dom";
import { APIFetchV1 } from "util/api";
import MainPageTitleContainer from "components/util/MainPageTitleContainer";
+import { HistorySafeGoBack } from "util/misc";
export default function ForgotPasswordPage() {
const [email, setEmail] = useState("");
@@ -59,7 +60,7 @@ export default function ForgotPasswordPage() {
history.goBack()}
+ onClick={() => HistorySafeGoBack(history)}
tabIndex={4}
className="mr-auto btn btn-outline-danger"
>
diff --git a/client/src/app/pages/LoginPage.tsx b/client/src/app/pages/LoginPage.tsx
index 375a20b34..12a9f12fc 100644
--- a/client/src/app/pages/LoginPage.tsx
+++ b/client/src/app/pages/LoginPage.tsx
@@ -10,6 +10,7 @@ import { Link, useHistory } from "react-router-dom";
import { PublicUserDocument } from "tachi-common";
import { APIFetchV1 } from "util/api";
import { HumaniseError } from "util/humanise-error";
+import { HistorySafeGoBack } from "util/misc";
export default function LoginPage() {
useSetSubheader("Login");
@@ -59,7 +60,7 @@ export default function LoginPage() {
setUser(userRJ.body);
localStorage.setItem("isLoggedIn", "true");
- history.goBack();
+ HistorySafeGoBack(history);
}, 500);
},
});
diff --git a/client/src/app/pages/RegisterPage.tsx b/client/src/app/pages/RegisterPage.tsx
index 1326acc6d..99c8167c7 100644
--- a/client/src/app/pages/RegisterPage.tsx
+++ b/client/src/app/pages/RegisterPage.tsx
@@ -11,6 +11,7 @@ import { PublicUserDocument } from "tachi-common";
import { UseFormik } from "types/react";
import { APIFetchV1, ToCDNURL } from "util/api";
import { HumaniseError } from "util/humanise-error";
+import { HistorySafeGoBack } from "util/misc";
// seconds it takes for a user to actually read the rules.
const RULES_READ_TIME = Number(process.env.REACT_APP_RULES_READ_TIME) || 30;
@@ -78,7 +79,7 @@ export default function RegisterPage() {
setUser(rj.body);
localStorage.setItem("isLoggedIn", "true");
- history.goBack();
+ HistorySafeGoBack(history);
}, 500);
},
});
diff --git a/client/src/app/routes/AppRoutes.tsx b/client/src/app/routes/AppRoutes.tsx
index 7d02a070c..2c420b633 100644
--- a/client/src/app/routes/AppRoutes.tsx
+++ b/client/src/app/routes/AppRoutes.tsx
@@ -11,6 +11,7 @@ import { UserContext } from "context/UserContext";
import { ClientConfig } from "lib/config";
import React, { useContext } from "react";
import { Redirect, Route, Switch, useHistory } from "react-router-dom";
+import { HistorySafeGoBack } from "util/misc";
import ClientFileFlowRoutes from "./ClientFileFlowRoutes";
import DashboardRoutes from "./DashboardRoutes";
import OAuth2CallbackRoutes from "./OAuth2CallbackRoutes";
@@ -76,7 +77,7 @@ export function Routes() {
desc="If you signed up with a fake email, you're now locked out of this account. Nice one."
/>
history.goBack()}
+ onClick={() => HistorySafeGoBack(history)}
tabIndex={4}
className="btn btn-outline-danger"
>
diff --git a/client/src/util/misc.ts b/client/src/util/misc.ts
index 7dda9202b..5e5f91253 100644
--- a/client/src/util/misc.ts
+++ b/client/src/util/misc.ts
@@ -1,4 +1,5 @@
import toast from "react-hot-toast";
+import { useHistory } from "react-router-dom";
import {
APIPermissions,
ChartDocument,
@@ -188,3 +189,11 @@ export function WrapError(fn: () => T, errMsg: string) {
export function Sleep(ms: number) {
return new Promise(resolve => setTimeout(() => resolve(), ms));
}
+
+export function HistorySafeGoBack(history: ReturnType) {
+ if (history.length === 1) {
+ history.replace("/");
+ } else {
+ history.goBack();
+ }
+}