pashto-grammar/src/App.tsx

87 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-03-06 10:40:31 +00:00
/**
* Copyright (c) 2021 lingdocs.com
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import React, { useState } from "react";
// eslint-disable-next-line
2021-09-18 04:43:00 +00:00
import { BrowserRouter as Router, Route, withRouter, Switch, RouteComponentProps } from "react-router-dom";
2021-03-06 10:40:31 +00:00
import "./App.css";
import Page404 from "./pages/404";
import Chapter from "./components/Chapter";
import { content } from "./content/index";
import Sidebar from "./components/Sidebar";
import Header from "./components/Header";
2021-04-14 12:31:13 +00:00
import TableOfContentsPage from "./pages/TableOfContentsPage";
2021-03-06 10:40:31 +00:00
import { useEffect } from "react";
2021-09-18 04:43:00 +00:00
import { useUser } from "./user-context";
import { AT } from "@lingdocs/lingdocs-main";
2021-06-25 13:56:57 +00:00
import ReactGA from "react-ga";
2021-03-06 10:40:31 +00:00
const chapters = content.reduce((chapters, item) => (
item.content
? [...chapters, item]
: [...chapters, ...item.chapters]
), []);
2021-06-25 13:56:57 +00:00
const prod = document.location.hostname === "grammar.lingdocs.com";
if (prod) {
ReactGA.initialize("UA-196576671-2");
ReactGA.set({ anonymizeIp: true });
}
2021-09-18 04:43:00 +00:00
function App(props: RouteComponentProps) {
2021-03-06 10:40:31 +00:00
const [navOpen, setNavOpen] = useState(false);
2021-09-18 04:43:00 +00:00
const { setUser } = useUser();
2021-08-04 09:58:50 +00:00
useEffect(() => {
ReactGA.pageview(window.location.pathname);
2021-09-18 04:43:00 +00:00
fetch("https://account.lingdocs.com/api/user").then((res) => res.json()).then((res) => {
console.log("fetched user info");
if (res.user) {
const user = res.user as AT.LingdocsUser
setUser(user);
} else {
setUser(undefined);
}
}).catch(console.error);
// eslint-disable-next-line
2021-08-04 09:58:50 +00:00
}, []);
2021-03-06 10:40:31 +00:00
useEffect(() => {
window.scroll(0, 0);
2021-08-04 09:58:50 +00:00
if (prod) {
ReactGA.pageview(window.location.pathname);
}
2021-03-06 10:40:31 +00:00
}, [props.location.pathname]);
return (
<>
<Header setNavOpen={setNavOpen} />
<div className="container-fluid">
<div className="main-row row">
<Sidebar
content={content}
navOpen={navOpen}
setNavOpen={setNavOpen}
pathname={props.location.pathname}
/>
<Switch>
<Route path="/" exact>
2021-04-14 12:31:13 +00:00
<TableOfContentsPage />
2021-03-06 10:40:31 +00:00
</Route>
2021-09-18 04:43:00 +00:00
{chapters.map((chapter: any) => (
2021-03-06 10:40:31 +00:00
<Route key={chapter.path} path={chapter.path}>
<Chapter>{chapter}</Chapter>
</Route>
))}
<Route component={Page404} />
</Switch>
</div>
</div>
</>
);
}
export default withRouter(App);