Skip to content

Commit

Permalink
perf: lazy load charts page
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Sep 19, 2023
1 parent 9274e61 commit d7c006d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
12 changes: 9 additions & 3 deletions src/components/Budget/BudgetPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Papa from "papaparse";
import { useEffect, useRef, useState } from "react";
import { Suspense, lazy, useEffect, useRef, useState } from "react";
import { Col, Container, Row, ToastContainer } from "react-bootstrap";
import { Option } from "react-bootstrap-typeahead/types/types";
import { useHotkeys } from "react-hotkeys-hook";
Expand All @@ -19,9 +19,9 @@ import {
roundBig,
userLang,
} from "../../utils";
import ChartsPage from "../ChartsPage/ChartsPage";
import ErrorModal, { CsvError, JsonError } from "../ErrorModal/ErrorModal";
import LandingPage from "../LandingPage/LandingPage";
import Loading from "../Loading/Loading";
import NavBar from "../NavBar/NavBar";
import Notification from "../Notification/Notification";
import { Stat } from "../StatCard/Stat";
Expand All @@ -32,6 +32,8 @@ import TableCard from "../TableCard/TableCard";
import { Budget } from "./Budget";
// import { useWhatChanged } from "@simbathesailor/use-what-changed";

const ChartsPage = lazy(() => import("../ChartsPage/ChartsPage"));

function BudgetPage() {
const inputRef = useRef<HTMLInputElement>(null);

Expand Down Expand Up @@ -606,7 +608,11 @@ function BudgetPage() {
}}
/>

{showGraphs && <ChartsPage onShowGraphs={() => setShowGraphs(false)} />}
{showGraphs && (
<Suspense fallback={<Loading />}>
<ChartsPage onShowGraphs={() => setShowGraphs(false)} />
</Suspense>
)}

{!loading && !showGraphs && budget?.id && (
<Container key={budget.id}>
Expand Down
14 changes: 3 additions & 11 deletions src/components/LandingPage/LandingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RefObject } from "react";
import { Button, Container, Form, Row, Spinner, Stack } from "react-bootstrap";
import { Button, Container, Form, Row, Stack } from "react-bootstrap";
import { useBudget } from "../../context/BudgetContext";
import Loading from "../Loading/Loading";

interface LandingPageProps {
loading: boolean;
Expand All @@ -22,16 +23,7 @@ function LandingPage({ loading, inputRef, onNew, onImport }: LandingPageProps) {

return (
<>
{loading && (
<Container
fluid
className="position-absolute top-50 start-50 translate-middle"
>
<Row className="justify-content-center">
<Spinner animation="border" role="status" />
</Row>
</Container>
)}
{loading && <Loading />}

{!loading && !budget && budgetList && budgetList.length < 1 && (
<Container className="position-absolute top-50 start-50 translate-middle">
Expand Down
18 changes: 18 additions & 0 deletions src/components/Loading/Loading.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { render, screen } from "@testing-library/react";
import Loading from "./Loading";

describe("Loading", () => {
const comp = <Loading />;

beforeEach(() => {
render(comp);
});

it("matches snapshot", () => {
expect(comp).toMatchSnapshot();
});

it("renders initial state", () => {
expect(screen.getByRole("status")).toBeInTheDocument();
});
});
16 changes: 16 additions & 0 deletions src/components/Loading/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Container, Row, Spinner } from "react-bootstrap";

function Loading() {
return (
<Container
fluid
className="position-absolute top-50 start-50 translate-middle"
>
<Row className="justify-content-center">
<Spinner animation="border" role="status" />
</Row>
</Container>
);
}

export default Loading;
3 changes: 3 additions & 0 deletions src/components/Loading/__snapshots__/Loading.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Loading > matches snapshot 1`] = `<Loading />`;

0 comments on commit d7c006d

Please sign in to comment.