All files / src/components/app MyApp.tsx

0% Statements 0/16
0% Branches 0/10
0% Functions 0/7
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                                                 
import { PageContext } from "@/context/PageContext";
import { PageConfig } from "@/routes/App";
import { ThemeProvider } from "chainr-frontend-shared";
import React, { Suspense, useState } from "react";
import { Route, Routes } from "react-router-dom";
import { DynamicNavbarWrapper } from "../nav/NavBar/DynamicNavbarWrapper";
import AppShell from "./AppShell";
import { appPages } from "./Pages";
 
export interface Page {
  route: string;
  element: React.ReactNode;
}
 
export interface MyAppSettingsInterface {
  nav: boolean;
}
 
export interface MyAppInterface {
  homeName: string;
  config: Record<string, PageConfig>;
  prefix: string;
  settings?: MyAppSettingsInterface;
  returnAddr?: string;
  navbar?: (
    desktopOpen: boolean,
    onDesktopToggle: () => void,
  ) => React.ReactNode;
  /** Shown above every page of the app, e.g. to mark a section as a preview. */
  notice?: React.ReactNode;
  /** Floats over every page of the app — the chat dock (`ChatDock`), in the
   *  signed-in apps only. Outside the pages' Suspense boundary, so it stays
   *  mounted, and keeps its state, from one page to the next and while a
   *  page's chunk loads. */
  dock?: React.ReactNode;
}
 
const default_settings: MyAppSettingsInterface = {
  nav: true,
};
 
/**
 * Shown while a page's chunk downloads.
 *
 * Deliberately quiet: a page chunk is tens of kilobytes on a warm connection,
 * and a spinner that flashes for 80ms reads as jank rather than as progress.
 */
const PageLoading: React.FC = () => (
  <div className="animate-pulse space-y-4 pt-2" aria-busy="true">
    <div className="h-6 w-48 rounded bg-gray-200 dark:bg-gray-700" />
    <div className="h-px w-full bg-gray-200 dark:bg-gray-700" />
    <div className="h-24 w-full rounded-xl bg-gray-100 dark:bg-gray-800" />
  </div>
);
 
const MyApp: React.FC<MyAppInterface> = (props) => {
  const settings = props.settings ?? default_settings;
  const pages: Page[] = appPages(props.config);
  const [title, setTitle] = useState<React.ReactNode>(null);
  const [backButton, setBackButton] = useState(false);
  const [desktopSidebarOpen, setDesktopSidebarOpen] = useState(true);
 
  const routes = pages.map((page: Page) => (
    <Route
      path={page.route.replace(props.prefix, "")}
      element={page.element}
      key={page.route}
    />
  ));
 
  return (
    <ThemeProvider>
      <PageContext.Provider
        value={{ title, setTitle, backButton, setBackButton }}
      >
        {props.navbar
          ? props.navbar(desktopSidebarOpen, () =>
              setDesktopSidebarOpen((o) => !o),
            )
          : settings.nav && (
              <DynamicNavbarWrapper
                homeName={props.homeName}
                app={props.config}
                returnAddr={props.returnAddr}
                desktopOpen={desktopSidebarOpen}
                onDesktopToggle={() => setDesktopSidebarOpen((o) => !o)}
              />
            )}
        <div
          className={`
            transition-[margin] duration-200 ease-in-out
            px-10 pt-16 md:pt-5 pb-15
            ${settings.nav && desktopSidebarOpen ? "md:ml-56" : "md:ml-6"}
          `}
        >
          {props.notice}
          {/* Every routed page is loaded on demand, so one boundary here
              serves all of them. Without it the app ships as a single 3.6 MB
              chunk: visiting any page downloads all fifty-two. */}
          <Suspense fallback={<PageLoading />}>
            <Routes>
              <Route element={<AppShell />}>{routes}</Route>
            </Routes>
          </Suspense>
        </div>
        {props.dock}
      </PageContext.Provider>
    </ThemeProvider>
  );
};
 
export default MyApp;