From 7cf6b4bcfec5476f7f756dbaa6ff2c2b4cf83048 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 8 Nov 2020 13:18:15 -0800 Subject: [PATCH] Only load GA when valid key is provided; closes #2678 --- resources/scripts/api/server/getServer.ts | 2 +- resources/scripts/components/App.tsx | 18 +++++-- .../scripts/routers/AuthenticationRouter.tsx | 37 ++++++-------- resources/scripts/routers/DashboardRouter.tsx | 51 ++++++++----------- resources/scripts/routers/ServerRouter.tsx | 13 ++--- 5 files changed, 57 insertions(+), 64 deletions(-) diff --git a/resources/scripts/api/server/getServer.ts b/resources/scripts/api/server/getServer.ts index 95fda0ddc..3521ed0da 100644 --- a/resources/scripts/api/server/getServer.ts +++ b/resources/scripts/api/server/getServer.ts @@ -13,7 +13,7 @@ export interface Allocation { export interface Server { id: string; - internalId: number; + internalId: number | string; uuid: string; name: string; node: string; diff --git a/resources/scripts/components/App.tsx b/resources/scripts/components/App.tsx index 350387fac..f61e73e0b 100644 --- a/resources/scripts/components/App.tsx +++ b/resources/scripts/components/App.tsx @@ -1,7 +1,7 @@ import React, { useEffect } from 'react'; import ReactGA from 'react-ga'; import { hot } from 'react-hot-loader/root'; -import { BrowserRouter, Route, Switch } from 'react-router-dom'; +import { BrowserRouter, Route, Switch, useLocation } from 'react-router-dom'; import { StoreProvider } from 'easy-peasy'; import { store } from '@/state'; import DashboardRouter from '@/routers/DashboardRouter'; @@ -30,6 +30,16 @@ interface ExtendedWindow extends Window { }; } +const Pageview = () => { + const { pathname } = useLocation(); + + useEffect(() => { + ReactGA.pageview(pathname); + }, [ pathname ]); + + return null; +}; + const App = () => { const { PterodactylUser, SiteConfiguration } = (window as ExtendedWindow); if (PterodactylUser && !store.getState().user.data) { @@ -50,8 +60,9 @@ const App = () => { } useEffect(() => { - ReactGA.initialize(SiteConfiguration!.analytics); - ReactGA.pageview(location.pathname); + if (SiteConfiguration?.analytics) { + ReactGA.initialize(SiteConfiguration!.analytics); + } }, []); return ( @@ -62,6 +73,7 @@ const App = () => {
+ {SiteConfiguration?.analytics && } diff --git a/resources/scripts/routers/AuthenticationRouter.tsx b/resources/scripts/routers/AuthenticationRouter.tsx index 57d1422ca..a7c687eef 100644 --- a/resources/scripts/routers/AuthenticationRouter.tsx +++ b/resources/scripts/routers/AuthenticationRouter.tsx @@ -1,5 +1,4 @@ -import React, { useEffect } from 'react'; -import ReactGA from 'react-ga'; +import React from 'react'; import { Route, RouteComponentProps, Switch } from 'react-router-dom'; import LoginContainer from '@/components/auth/LoginContainer'; import ForgotPasswordContainer from '@/components/auth/ForgotPasswordContainer'; @@ -7,23 +6,17 @@ import ResetPasswordContainer from '@/components/auth/ResetPasswordContainer'; import LoginCheckpointContainer from '@/components/auth/LoginCheckpointContainer'; import NotFound from '@/components/screens/NotFound'; -export default ({ location, history, match }: RouteComponentProps) => { - useEffect(() => { - ReactGA.pageview(location.pathname); - }, [ location.pathname ]); - - return ( -
- - - - - - - - history.push('/auth/login')} /> - - -
- ); -}; +export default ({ location, history, match }: RouteComponentProps) => ( +
+ + + + + + + + history.push('/auth/login')}/> + + +
+); diff --git a/resources/scripts/routers/DashboardRouter.tsx b/resources/scripts/routers/DashboardRouter.tsx index 7a895a7e4..1e5963996 100644 --- a/resources/scripts/routers/DashboardRouter.tsx +++ b/resources/scripts/routers/DashboardRouter.tsx @@ -1,5 +1,4 @@ -import React, { useEffect } from 'react'; -import ReactGA from 'react-ga'; +import React from 'react'; import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom'; import AccountOverviewContainer from '@/components/dashboard/AccountOverviewContainer'; import NavigationBar from '@/components/NavigationBar'; @@ -9,30 +8,24 @@ import NotFound from '@/components/screens/NotFound'; import TransitionRouter from '@/TransitionRouter'; import SubNavigation from '@/components/elements/SubNavigation'; -export default ({ location }: RouteComponentProps) => { - useEffect(() => { - ReactGA.pageview(location.pathname); - }, [ location.pathname ]); - - return ( - <> - - {location.pathname.startsWith('/account') && - -
- Settings - API Credentials -
-
- } - - - - - - - - - - ); -}; +export default ({ location }: RouteComponentProps) => ( + <> + + {location.pathname.startsWith('/account') && + +
+ Settings + API Credentials +
+
+ } + + + + + + + + + +); diff --git a/resources/scripts/routers/ServerRouter.tsx b/resources/scripts/routers/ServerRouter.tsx index 01c5ac07e..0a09774aa 100644 --- a/resources/scripts/routers/ServerRouter.tsx +++ b/resources/scripts/routers/ServerRouter.tsx @@ -1,5 +1,4 @@ import React, { useEffect, useState } from 'react'; -import ReactGA from 'react-ga'; import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom'; import NavigationBar from '@/components/NavigationBar'; import ServerConsole from '@/components/server/ServerConsole'; @@ -40,9 +39,9 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) const id = ServerContext.useStoreState(state => state.server.data?.id); const uuid = ServerContext.useStoreState(state => state.server.data?.uuid); const isInstalling = ServerContext.useStoreState(state => state.server.data?.isInstalling); + const serverId = ServerContext.useStoreState(state => state.server.data?.internalId); const getServer = ServerContext.useStoreActions(actions => actions.server.getServer); const clearServerState = ServerContext.useStoreActions(actions => actions.clearServerState); - const serverId = ServerContext.useStoreState(state => state.server.data?.internalId); useEffect(() => () => { clearServerState(); @@ -70,10 +69,6 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) }; }, [ match.params.id ]); - useEffect(() => { - ReactGA.pageview(location.pathname); - }, [ location.pathname ]); - return ( @@ -113,9 +108,9 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) Settings {rootAdmin && - - - + + + }