]> Untitled Git - lemmy-ui.git/blob - src/shared/components/app/app.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / app / app.tsx
1 import { Component, createRef, linkEvent, RefObject } from "inferno";
2 import { Provider } from "inferno-i18next-dess";
3 import { Route, Switch } from "inferno-router";
4 import { i18n } from "../../i18next";
5 import { IsoDataOptionalSite } from "../../interfaces";
6 import { routes } from "../../routes";
7 import { isAuthPath, setIsoData } from "../../utils";
8 import AuthGuard from "../common/auth-guard";
9 import ErrorGuard from "../common/error-guard";
10 import { ErrorPage } from "./error-page";
11 import { Footer } from "./footer";
12 import { Navbar } from "./navbar";
13 import "./styles.scss";
14 import { Theme } from "./theme";
15
16 export class App extends Component<any, any> {
17   private isoData: IsoDataOptionalSite = setIsoData(this.context);
18   private readonly mainContentRef: RefObject<HTMLElement>;
19   constructor(props: any, context: any) {
20     super(props, context);
21     this.mainContentRef = createRef();
22   }
23
24   handleJumpToContent(event) {
25     event.preventDefault();
26     this.mainContentRef.current?.focus();
27   }
28   render() {
29     const siteRes = this.isoData.site_res;
30     const siteView = siteRes?.site_view;
31
32     return (
33       <>
34         <Provider i18next={i18n}>
35           <div id="app" className="lemmy-site">
36             <a
37               className="skip-link bg-light text-dark p-2 text-decoration-none position-absolute start-0 z-3"
38               onClick={linkEvent(this, this.handleJumpToContent)}
39             >
40               ${i18n.t("jump_to_content", "Jump to content")}
41             </a>
42             {siteView && (
43               <Theme defaultTheme={siteView.local_site.default_theme} />
44             )}
45             <Navbar siteRes={siteRes} />
46             <div className="mt-4 p-0 fl-1">
47               <Switch>
48                 {routes.map(({ path, component: RouteComponent }) => (
49                   <Route
50                     key={path}
51                     path={path}
52                     exact
53                     component={routeProps => (
54                       <ErrorGuard>
55                         <main tabIndex={-1} ref={this.mainContentRef}>
56                           {RouteComponent &&
57                             (isAuthPath(path ?? "") ? (
58                               <AuthGuard>
59                                 <RouteComponent {...routeProps} />
60                               </AuthGuard>
61                             ) : (
62                               <RouteComponent {...routeProps} />
63                             ))}
64                         </main>
65                       </ErrorGuard>
66                     )}
67                   />
68                 ))}
69                 <Route component={ErrorPage} />
70               </Switch>
71             </div>
72             <Footer site={siteRes} />
73           </div>
74         </Provider>
75       </>
76     );
77   }
78 }