Skip to content

Commit 42a2f65

Browse files
authored
Publisher pages lazy import, pt5 (#5476)
* fix: use Link components for SPA navigation * feature: lazy import models, model details and policies pages * chore: refactor model page layout to fix ugly rerender * fix: use default loader * fix: bad useQuery key (fix #5452) * fix: test that now fails
1 parent 96d5f94 commit 42a2f65

12 files changed

Lines changed: 37 additions & 28 deletions

File tree

static/js/publisher/components/BrandStoreRoute/BrandStoreRoute.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Spinner } from "@canonical/react-components";
21
import { useSetAtom } from "jotai";
32
import { useEffect } from "react";
43
import { Outlet, useLocation, useNavigate, useParams } from "react-router-dom";
@@ -7,6 +6,7 @@ import { useBrandStores } from "../../hooks";
76
import StoreNotFound from "../../pages/StoreNotFound";
87
import { brandStoresState } from "../../state/brandStoreState";
98
import { Store } from "../../types/shared";
9+
import Loader from "../Loader";
1010

1111
function BrandStoreRoute() {
1212
const location = useLocation();
@@ -47,7 +47,7 @@ function BrandStoreRoute() {
4747
<StoreNotFound />
4848
)
4949
) : (
50-
<Spinner text="Loading..." />
50+
<Loader />
5151
);
5252
}
5353

static/js/publisher/hooks/usePolicies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function usePolicies(
77
modelId: string | undefined,
88
): UsePoliciesResponse {
99
return useQuery<Policy[], ApiError>({
10-
queryKey: ["policies", storeId],
10+
queryKey: ["policies", storeId, modelId],
1111
queryFn: async () => {
1212
const response = await fetch(
1313
`/api/store/${storeId}/models/${modelId}/policies`,

static/js/publisher/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import { importComponent } from "./utils/importComponent";
88
import BrandStoreRoute from "./components/BrandStoreRoute/BrandStoreRoute";
99
import PublisherLayout from "./layouts/PublisherLayout";
1010
import SnapsManagementLayout from "./layouts/SnapsManagementLayout";
11+
import ModelDetailsPageLayout from "./layouts/ModelDetailsPageLayout/ModelPageLayout";
1112

1213
import BrandStoreSettings from "./pages/BrandStoreSettings";
1314
import Members from "./pages/Members";
14-
import Model from "./pages/Model";
15-
import Policies from "./pages/Model/Policies";
16-
import Models from "./pages/Models";
1715
import SigningKeys from "./pages/SigningKeys";
1816
import Snaps from "./pages/Snaps";
1917
import ValidationSet from "./pages/ValidationSet";
@@ -38,6 +36,9 @@ const RegisterSnap = importComponent(() => import("./pages/RegisterSnap"));
3836
const RequestReservedName = importComponent(
3937
() => import("./pages/RequestReservedName"),
4038
);
39+
const Models = importComponent(() => import("./pages/Models"));
40+
const Model = importComponent(() => import("./pages/Model"));
41+
const Policies = importComponent(() => import("./pages/Model/Policies"));
4142

4243
Sentry.init({
4344
dsn: window.SENTRY_DSN,
@@ -117,7 +118,7 @@ root.render(
117118
<Route path="models">
118119
<Route index element={<Models />} />
119120
<Route path="create" element={<Models />} />
120-
<Route path=":model_id">
121+
<Route path=":model_id" element={<ModelDetailsPageLayout />}>
121122
<Route index element={<Model />} />
122123
<Route path="policies" element={<Policies />} />
123124
<Route path="policies/create" element={<Policies />} />

static/js/publisher/pages/Model/ModelBreadcrumb.tsx renamed to static/js/publisher/layouts/ModelDetailsPageLayout/ModelBreadcrumb.tsx

File renamed without changes.

static/js/publisher/pages/Model/ModelNav.tsx renamed to static/js/publisher/layouts/ModelDetailsPageLayout/ModelNav.tsx

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Outlet, useLocation } from "react-router-dom";
2+
import ModelBreadcrumb from "./ModelBreadcrumb";
3+
import ModelNav from "./ModelNav";
4+
5+
function ModelDetailsPageLayout() {
6+
const { pathname } = useLocation();
7+
const isPolicies = pathname.endsWith("policies");
8+
9+
return (
10+
<>
11+
<ModelBreadcrumb />
12+
<ModelNav sectionName={isPolicies ? "policies" : "overview"} />
13+
<Outlet />
14+
</>
15+
);
16+
}
17+
18+
export default ModelDetailsPageLayout;

static/js/publisher/pages/Model/__tests__/ModelNav.test.tsx renamed to static/js/publisher/layouts/ModelDetailsPageLayout/__tests__/ModelNav.test.tsx

File renamed without changes.

static/js/publisher/pages/Model/Model.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414

1515
import { modelsListState, currentModelState } from "../../state/modelsState";
1616
import { brandIdState, brandStoreState } from "../../state/brandStoreState";
17-
import ModelNav from "./ModelNav";
18-
import ModelBreadcrumb from "./ModelBreadcrumb";
1917
import { useModels } from "../../hooks";
2018
import { setPageTitle } from "../../utils";
2119
import type { Model as ModelType } from "../../types/shared";
@@ -96,12 +94,6 @@ function Model() {
9694

9795
return (
9896
<>
99-
<div className="u-fixed-width">
100-
<ModelBreadcrumb />
101-
</div>
102-
<div className="u-fixed-width">
103-
<ModelNav sectionName="overview" />
104-
</div>
10597
<div className="u-fixed-width u-align--right">
10698
<Button
10799
type="button"

static/js/publisher/pages/Model/Policies.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
import { useAtomValue, useSetAtom } from "jotai";
1010
import { Row, Col, Notification, Icon } from "@canonical/react-components";
1111

12-
import ModelNav from "./ModelNav";
13-
import ModelBreadcrumb from "./ModelBreadcrumb";
1412
import Filter from "../../components/Filter";
1513
import PoliciesTable from "./PoliciesTable";
1614
import CreatePolicyForm from "./CreatePolicyForm";
@@ -78,13 +76,6 @@ function Policies(): React.JSX.Element {
7876

7977
return (
8078
<>
81-
<div className="u-fixed-width">
82-
<ModelBreadcrumb />
83-
<h1 className="u-off-screen">{model_id}</h1>
84-
</div>
85-
<div className="u-fixed-width">
86-
<ModelNav sectionName="policies" />
87-
</div>
8879
<Row>
8980
<Col size={6}>
9081
<Filter

static/js/publisher/pages/Publisher/Publisher.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { Link } from "react-router-dom";
2+
13
function Publisher(): React.JSX.Element {
24
return (
35
<div className="u-fixed-width">
46
<h1>Publisher</h1>
57
<p>
68
As a publisher you can{" "}
7-
<a href="/snaps">register a snap name on the Snap store</a> and{" "}
9+
<Link to="/snaps">register a snap name on the Snap store</Link> and{" "}
810
<a href={`${window.API_URL}stores/snaps/`}>
911
manage your snaps on the dashboard
1012
</a>

0 commit comments

Comments
 (0)