@canonical/summon-application 0.43.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +57 -11
  2. package/dist/esm/application/react/index.js +175 -21
  3. package/dist/esm/application/react/index.js.map +1 -1
  4. package/dist/esm/application/react/templates/README.md.ejs +33 -1
  5. package/dist/esm/application/react/templates/package.json.ejs +25 -18
  6. package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +10 -16
  7. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +5 -5
  8. package/dist/esm/application/react/templates/src/domains/dashboard/DashboardBreadcrumbs.tsx.ejs +35 -0
  9. package/dist/esm/application/react/templates/src/domains/dashboard/DashboardLayout.tsx.ejs +39 -0
  10. package/dist/esm/application/react/templates/src/domains/dashboard/InstancesPage.test.tsx.ejs +144 -0
  11. package/dist/esm/application/react/templates/src/domains/dashboard/InstancesPage.tsx.ejs +281 -0
  12. package/dist/esm/application/react/templates/src/domains/dashboard/OverviewPage.test.tsx.ejs +76 -0
  13. package/dist/esm/application/react/templates/src/domains/dashboard/OverviewPage.tsx.ejs +224 -0
  14. package/dist/esm/application/react/templates/src/domains/dashboard/SettingsPage.test.tsx.ejs +64 -0
  15. package/dist/esm/application/react/templates/src/domains/dashboard/SettingsPage.tsx.ejs +142 -0
  16. package/dist/esm/application/react/templates/src/domains/dashboard/constants.ts.ejs +229 -0
  17. package/dist/esm/application/react/templates/src/domains/dashboard/hooks/index.ts +2 -0
  18. package/dist/esm/application/react/templates/src/domains/dashboard/hooks/types.ts +13 -0
  19. package/dist/esm/application/react/templates/src/domains/dashboard/hooks/useDashboardNavigation.ts.ejs +94 -0
  20. package/dist/esm/application/react/templates/src/domains/dashboard/projectBreadcrumbs.test.ts +78 -0
  21. package/dist/esm/application/react/templates/src/domains/dashboard/projectBreadcrumbs.ts +48 -0
  22. package/dist/esm/application/react/templates/src/domains/dashboard/routes.ts +21 -0
  23. package/dist/esm/application/react/templates/src/domains/dashboard/selectRelativeTime.test.ts +19 -0
  24. package/dist/esm/application/react/templates/src/domains/dashboard/selectRelativeTime.ts +24 -0
  25. package/dist/esm/application/react/templates/src/domains/dashboard/styles.css +98 -0
  26. package/dist/esm/application/react/templates/src/domains/dashboard/types.ts.ejs +61 -0
  27. package/dist/esm/application/react/templates/src/domains/marketing/HomePage.tsx.ejs +14 -9
  28. package/dist/esm/application/react/templates/src/i18n/ar.ts.ejs +173 -0
  29. package/dist/esm/application/react/templates/src/i18n/en.ts.ejs +172 -0
  30. package/dist/esm/application/react/templates/src/i18n/fr.ts.ejs +170 -0
  31. package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.stories.tsx +91 -0
  32. package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.test.tsx.ejs +139 -0
  33. package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.tsx.ejs +78 -0
  34. package/dist/esm/application/react/templates/src/lib/ApplicationShell/index.ts +2 -0
  35. package/dist/esm/application/react/templates/src/lib/ApplicationShell/styles.css +43 -0
  36. package/dist/esm/application/react/templates/src/lib/ApplicationShell/types.ts +20 -0
  37. package/dist/esm/application/react/templates/src/lib/CanonicalLogo/CanonicalLogo.tsx.ejs +51 -0
  38. package/dist/esm/application/react/templates/src/lib/CanonicalLogo/index.ts +2 -0
  39. package/dist/esm/application/react/templates/src/lib/CanonicalLogo/styles.css +31 -0
  40. package/dist/esm/application/react/templates/src/lib/CanonicalLogo/types.ts +11 -0
  41. package/dist/esm/application/react/templates/src/lib/CodeMessage/CodeMessage.test.tsx +33 -0
  42. package/dist/esm/application/react/templates/src/lib/CodeMessage/CodeMessage.tsx +33 -0
  43. package/dist/esm/application/react/templates/src/lib/CodeMessage/index.ts +2 -0
  44. package/dist/esm/application/react/templates/src/lib/CodeMessage/types.ts +16 -0
  45. package/dist/esm/application/react/templates/src/lib/ExampleComponent/styles.css +1 -1
  46. package/dist/esm/application/react/templates/src/lib/LazyComponent/{LazyComponent.tsx → LazyComponent.tsx.ejs} +26 -0
  47. package/dist/esm/application/react/templates/src/lib/LoadingMessage/LoadingMessage.test.tsx.ejs +20 -0
  48. package/dist/esm/application/react/templates/src/lib/LoadingMessage/LoadingMessage.tsx.ejs +21 -0
  49. package/dist/esm/application/react/templates/src/lib/LoadingMessage/index.ts +1 -0
  50. package/dist/esm/application/react/templates/src/lib/LocaleSelector/{LocaleSelector.tsx → LocaleSelector.tsx.ejs} +23 -0
  51. package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.test.tsx.ejs +87 -0
  52. package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
  53. package/dist/esm/application/react/templates/src/lib/RouterLink/RouterLink.test.tsx +98 -0
  54. package/dist/esm/application/react/templates/src/lib/RouterLink/RouterLink.tsx +65 -0
  55. package/dist/esm/application/react/templates/src/lib/RouterLink/index.ts +1 -0
  56. package/dist/esm/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx.ejs +29 -14
  57. package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.stories.tsx.ejs +29 -0
  58. package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.test.tsx.ejs +113 -0
  59. package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.tsx.ejs +45 -0
  60. package/dist/esm/application/react/templates/src/lib/TopBar/index.ts +2 -0
  61. package/dist/esm/application/react/templates/src/lib/TopBar/styles.css +49 -0
  62. package/dist/esm/application/react/templates/src/lib/TopBar/types.ts +8 -0
  63. package/dist/esm/application/react/templates/src/lib/index.ts.ejs +10 -0
  64. package/dist/esm/application/react/templates/src/routes.tsx.ejs +70 -20
  65. package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +6 -7
  66. package/dist/esm/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +5 -0
  67. package/dist/esm/application/react/templates/src/styles/app.css +25 -12
  68. package/dist/esm/application/react/templates/src/testing/browserStubs.ts +57 -0
  69. package/dist/esm/application/react/templates/src/testing/fixtures.ts.ejs +29 -0
  70. package/dist/esm/application/react/templates/src/testing/renderWithProviders.tsx.ejs +64 -0
  71. package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +37 -1
  72. package/dist/esm/application/react/templates/vite-plugin-ds-icons.ts +7 -2
  73. package/dist/esm/application/react/templates/vitest.config.ts +6 -2
  74. package/dist/esm/application/react/textHelpers.js +9 -0
  75. package/dist/esm/application/react/textHelpers.js.map +1 -0
  76. package/dist/esm/shared/versions.js +35 -21
  77. package/dist/esm/shared/versions.js.map +1 -1
  78. package/dist/types/application/react/index.d.ts +1 -0
  79. package/dist/types/application/react/index.d.ts.map +1 -1
  80. package/dist/types/application/react/textHelpers.d.ts +18 -0
  81. package/dist/types/application/react/textHelpers.d.ts.map +1 -0
  82. package/dist/types/shared/versions.d.ts +41 -4
  83. package/dist/types/shared/versions.d.ts.map +1 -1
  84. package/package.json +9 -9
  85. package/dist/esm/application/react/templates/src/i18n/ar.ts +0 -73
  86. package/dist/esm/application/react/templates/src/i18n/en.ts +0 -72
  87. package/dist/esm/application/react/templates/src/i18n/fr.ts +0 -69
  88. /package/dist/esm/application/react/templates/src/domains/catalog/{ErrorBoundary.tests.tsx.ejs → ErrorBoundary.test.tsx.ejs} +0 -0
  89. /package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tests.tsx.ejs → ProductList.test.tsx.ejs} +0 -0
  90. /package/dist/esm/application/react/templates/src/i18n/{negotiation.tests.ts → negotiation.test.ts} +0 -0
  91. /package/dist/esm/application/react/templates/src/lib/ClientOnly/{ClientOnly.tests.tsx → ClientOnly.test.tsx} +0 -0
  92. /package/dist/esm/application/react/templates/src/lib/ExampleComponent/{ExampleComponent.tests.tsx → ExampleComponent.test.tsx} +0 -0
  93. /package/dist/esm/application/react/templates/src/lib/LocaleSelector/{LocaleSelector.tests.tsx → LocaleSelector.test.tsx} +0 -0
  94. /package/dist/esm/application/react/templates/src/relay/{environment.tests.ts → environment.test.ts} +0 -0
  95. /package/dist/esm/application/react/templates/src/relay/{schema.tests.ts → schema.test.ts} +0 -0
@@ -22,13 +22,8 @@ import { Outlet, RouterProvider } from "@canonical/router-react";
22
22
  <% } else { -%>
23
23
  import {
24
24
  Outlet,
25
- <% if (intl) { -%>
26
25
  RouterProvider,
27
- <% } -%>
28
26
  readDehydratedState,
29
- <% if (!intl) { -%>
30
- RouterProvider,
31
- <% } -%>
32
27
  } from "@canonical/router-react";
33
28
  <% } -%>
34
29
  <% if (spa) { -%>
@@ -42,24 +37,23 @@ import { RelayEnvironmentProvider } from "react-relay";
42
37
  <% if (intl) { -%>
43
38
  import { catalogs, i18nConfig } from "#i18n/index.js";
44
39
  <% } -%>
40
+ import LoadingMessage from "#lib/LoadingMessage/index.js";
45
41
  <% if (relay) { -%>
46
42
  import { getBrowserEnvironment } from "#relay/environment.js";
47
43
  <% } -%>
48
44
  import formatDocumentTitle from "../formatDocumentTitle.js";
49
- <%# Same story: the SPA arm never pulls the two SSR payload symbols, so the three -%>
50
- <%# base names fit on one line and the formatter requires them there. -%>
51
- <% if (spa) { -%>
52
- import { appRoutes, middleware, notFoundRoute } from "../routes.js";
53
- <% } else { -%>
45
+ <%# Only the SSR arm with Relay pulls the two payload symbols; every other arm's -%>
46
+ <%# three base names fit on one line, and the formatter requires them there. -%>
47
+ <% if (relay && !spa) { -%>
54
48
  import {
55
49
  appRoutes,
56
50
  middleware,
57
51
  notFoundRoute,
58
- <% if (relay) { -%>
59
52
  resolveRelayPayloads,
60
53
  type SerializedRelayPayload,
61
- <% } -%>
62
54
  } from "../routes.js";
55
+ <% } else { -%>
56
+ import { appRoutes, middleware, notFoundRoute } from "../routes.js";
63
57
  <% } -%>
64
58
  <% if (relay && spa) { -%>
65
59
 
@@ -171,7 +165,7 @@ hydrateRoot(
171
165
  <HeadProvider titleTemplate={formatDocumentTitle}>
172
166
  <RelayEnvironmentProvider environment={relayEnvironment}>
173
167
  <RouterProvider router={router}>
174
- <Outlet fallback={<p>Loading…</p>} />
168
+ <Outlet fallback={<LoadingMessage />} />
175
169
  </RouterProvider>
176
170
  </RelayEnvironmentProvider>
177
171
  </HeadProvider>
@@ -180,7 +174,7 @@ hydrateRoot(
180
174
  <HeadProvider titleTemplate={formatDocumentTitle}>
181
175
  <RelayEnvironmentProvider environment={relayEnvironment}>
182
176
  <RouterProvider router={router}>
183
- <Outlet fallback={<p>Loading…</p>} />
177
+ <Outlet fallback={<LoadingMessage />} />
184
178
  </RouterProvider>
185
179
  </RelayEnvironmentProvider>
186
180
  </HeadProvider>,
@@ -192,14 +186,14 @@ hydrateRoot(
192
186
  >
193
187
  <HeadProvider titleTemplate={formatDocumentTitle}>
194
188
  <RouterProvider router={router}>
195
- <Outlet fallback={<p>Loading…</p>} />
189
+ <Outlet fallback={<LoadingMessage />} />
196
190
  </RouterProvider>
197
191
  </HeadProvider>
198
192
  </I18nProvider>,
199
193
  <% } else { -%>
200
194
  <HeadProvider titleTemplate={formatDocumentTitle}>
201
195
  <RouterProvider router={router}>
202
- <Outlet fallback={<p>Loading…</p>} />
196
+ <Outlet fallback={<LoadingMessage />} />
203
197
  </RouterProvider>
204
198
  </HeadProvider>,
205
199
  <% } -%>
@@ -5,6 +5,9 @@ import { Head } from "@canonical/react-head";
5
5
  import { type ReactElement, Suspense } from "react";
6
6
  import { useRelayEnvironment } from "react-relay";
7
7
  import { createOperationDescriptor, getRequest } from "relay-runtime";
8
+ <% if (intl) { -%>
9
+ import { CodeMessage } from "#lib/index.js";
10
+ <% } -%>
8
11
  import ErrorBoundary from "./ErrorBoundary.js";
9
12
  import ProductList, { PAGE_SIZE, productListQuery } from "./ProductList.js";
10
13
 
@@ -27,14 +30,10 @@ export default function CatalogPage(): ReactElement {
27
30
  <% if (intl) { -%>
28
31
  <Head title={t("catalog.title")} />
29
32
  <h1 id="catalog-title">{t("catalog.heading")}</h1>
30
- {/*
31
- Developer documentation rather than user-facing copy (file names,
32
- APIs), so it deliberately stays out of the message catalogs.
33
- */}
33
+ <CodeMessage message={t("catalog.intro")} />
34
34
  <% } else { -%>
35
35
  <Head title="Catalog" />
36
36
  <h1 id="catalog-title">Catalog</h1>
37
- <% } -%>
38
37
  <p>
39
38
  This page demonstrates the Relay data layer. <code>ProductList</code>{" "}
40
39
  issues a <code>useLazyLoadQuery</code>, each <code>ProductCard</code>{" "}
@@ -43,6 +42,7 @@ export default function CatalogPage(): ReactElement {
43
42
  <code>src/relay/schema.ts</code> — set <code>VITE_GRAPHQL_URL</code> to
44
43
  point it at a real endpoint instead.
45
44
  </p>
45
+ <% } -%>
46
46
  <% if (spa) { -%>
47
47
  {/*
48
48
  There is no server to prefetch against, so `useLazyLoadQuery`
@@ -0,0 +1,35 @@
1
+ <% if (intl) { -%>
2
+ import { useTranslation } from "@canonical/i18n-react";
3
+ <% } -%>
4
+ import { Breadcrumbs } from "@canonical/react-ds-global";
5
+ import { useRoute } from "@canonical/router-react";
6
+ import type { ReactElement } from "react";
7
+ import { RouterLink } from "#lib/index.js";
8
+ import { LEGAL_URL, PROJECT_NAME } from "./constants.js";
9
+ import { useDashboardNavigation } from "./hooks/index.js";
10
+ import projectBreadcrumbs from "./projectBreadcrumbs.js";
11
+
12
+ /**
13
+ * The breadcrumb trail of the current dashboard page: the project, then the
14
+ * page's path in the side navigation's tree. The trail is projected from the
15
+ * same navigation items the side navigation renders, so a page never states
16
+ * its own position and the two cannot disagree.
17
+ */
18
+ export default function DashboardBreadcrumbs(): ReactElement {
19
+ <% if (intl) { -%>
20
+ const { t } = useTranslation();
21
+ <% } -%>
22
+ const { root } = useDashboardNavigation({ legalUrl: LEGAL_URL });
23
+ const { pathname } = useRoute();
24
+
25
+ return (
26
+ <Breadcrumbs
27
+ aria-label=<%- attr("dashboard.breadcrumbs", "Breadcrumb") %>
28
+ items={[
29
+ { label: PROJECT_NAME, url: "/dashboard" },
30
+ ...projectBreadcrumbs(root, pathname),
31
+ ]}
32
+ LinkComponent={RouterLink}
33
+ />
34
+ );
35
+ }
@@ -0,0 +1,39 @@
1
+ <% if (intl) { -%>
2
+ import { useTranslation } from "@canonical/i18n-react";
3
+ <% } -%>
4
+ import type { ReactElement, ReactNode } from "react";
5
+ import { ApplicationShell } from "#lib/index.js";
6
+ import { LEGAL_URL } from "./constants.js";
7
+ import { useDashboardNavigation } from "./hooks/index.js";
8
+
9
+ /**
10
+ * The frame of every dashboard page: the application shell, with the
11
+ * dashboard's side navigation. `src/routes.tsx` applies it to the dashboard's
12
+ * routes as a router wrapper, beneath the top bar.
13
+ */
14
+ export default function DashboardLayout({
15
+ children,
16
+ }: {
17
+ readonly children: ReactNode;
18
+ }): ReactElement {
19
+ <% if (intl) { -%>
20
+ const { t } = useTranslation();
21
+ <% } -%>
22
+ const { root, footerRoot } = useDashboardNavigation({ legalUrl: LEGAL_URL });
23
+
24
+ return (
25
+ <% if (intl) { -%>
26
+ <ApplicationShell
27
+ footerRoot={footerRoot}
28
+ label={t("dashboard.nav.label")}
29
+ root={root}
30
+ >
31
+ {children}
32
+ </ApplicationShell>
33
+ <% } else { -%>
34
+ <ApplicationShell footerRoot={footerRoot} label="Dashboard" root={root}>
35
+ {children}
36
+ </ApplicationShell>
37
+ <% } -%>
38
+ );
39
+ }
@@ -0,0 +1,144 @@
1
+ <% if (forms) { -%>
2
+ import { fireEvent, screen, waitFor, within } from "@testing-library/react";
3
+ <% } else { -%>
4
+ import { fireEvent, screen, within } from "@testing-library/react";
5
+ <% } -%>
6
+ import { beforeAll, describe, expect, it } from "vitest";
7
+ <% if (forms) { -%>
8
+ import {
9
+ installDialogStubs,
10
+ installMatchMedia,
11
+ installResizeObserver,
12
+ } from "../../testing/browserStubs.js";
13
+ <% } else { -%>
14
+ import {
15
+ installMatchMedia,
16
+ installResizeObserver,
17
+ } from "../../testing/browserStubs.js";
18
+ <% } -%>
19
+ import renderWithProviders from "../../testing/renderWithProviders.js";
20
+ import InstancesPage from "./InstancesPage.js";
21
+
22
+ beforeAll(() => {
23
+ installMatchMedia();
24
+ installResizeObserver();
25
+ <% if (forms) { -%>
26
+ installDialogStubs();
27
+ <% } -%>
28
+ });
29
+ <% if (forms) { -%>
30
+
31
+ /** The page's own Create instance button, in its title bar. */
32
+ function openCreatePanelButton(container: HTMLElement): HTMLElement {
33
+ const titlebar = container.querySelector<HTMLElement>("header.titlebar");
34
+ if (titlebar === null) {
35
+ throw new Error("InstancesPage rendered no title bar");
36
+ }
37
+ return within(titlebar).getByRole("button", { name: "Create instance" });
38
+ }
39
+ <% } -%>
40
+
41
+ function renderPage() {
42
+ return renderWithProviders(<InstancesPage />, "/dashboard/instances");
43
+ }
44
+
45
+ describe("InstancesPage", () => {
46
+ it("lists the instances, with their count beside the heading", () => {
47
+ renderPage();
48
+
49
+ const heading = screen.getByRole("heading", { level: 1 });
50
+
51
+ expect(heading).toHaveTextContent("Instances");
52
+ expect(heading).toHaveTextContent("6");
53
+ expect(
54
+ screen.getByRole("button", { name: "Show details of web-01" }),
55
+ ).toBeInTheDocument();
56
+ });
57
+
58
+ it("projects its breadcrumb trail from the navigation tree", () => {
59
+ renderPage();
60
+
61
+ const trail = screen.getByRole("navigation", { name: "Breadcrumb" });
62
+
63
+ expect(
64
+ within(trail).getByRole("link", { name: "default" }),
65
+ ).toHaveAttribute("href", "/dashboard");
66
+ expect(within(trail).getByText("Instances")).toHaveAttribute(
67
+ "aria-current",
68
+ "page",
69
+ );
70
+ });
71
+ <% if (intl) { -%>
72
+
73
+ it("labels statuses and memory in the active locale", () => {
74
+ renderWithProviders(<InstancesPage />, "/dashboard/instances", "fr");
75
+
76
+ expect(
77
+ screen.getAllByText("En cours d'exécution · Ubuntu 24.04 LTS"),
78
+ ).not.toHaveLength(0);
79
+
80
+ fireEvent.click(
81
+ screen.getByRole("button", { name: "Afficher les détails de db-01" }),
82
+ );
83
+
84
+ expect(screen.getByText("16 Gio")).toBeInTheDocument();
85
+ });
86
+ <% } -%>
87
+
88
+ describe("the details aside", () => {
89
+ it("prompts for a selection until an instance is chosen", () => {
90
+ renderPage();
91
+
92
+ expect(
93
+ screen.getByText("Select an instance to see its details."),
94
+ ).toBeInTheDocument();
95
+ });
96
+
97
+ it("shows the chosen instance and marks its button pressed", () => {
98
+ renderPage();
99
+
100
+ const button = screen.getByRole("button", {
101
+ name: "Show details of db-01",
102
+ });
103
+ fireEvent.click(button);
104
+
105
+ expect(button).toHaveAttribute("aria-pressed", "true");
106
+ expect(
107
+ screen.getByRole("heading", { level: 2, name: "db-01" }),
108
+ ).toBeInTheDocument();
109
+ expect(screen.getByText("10.42.0.21")).toBeInTheDocument();
110
+ });
111
+ });
112
+ <% if (forms) { -%>
113
+
114
+ describe("the create side panel", () => {
115
+ it("opens prefilled from the Create instance button", () => {
116
+ const { container } = renderPage();
117
+ const dialog = container.querySelector("dialog");
118
+
119
+ fireEvent.click(openCreatePanelButton(container));
120
+
121
+ expect(dialog).toHaveAttribute("open");
122
+ expect(screen.getByLabelText(/Instance name/)).toHaveValue("web-03");
123
+ });
124
+
125
+ it("adds the submitted instance, selects it and closes", async () => {
126
+ const { container } = renderPage();
127
+ const dialog = container.querySelector("dialog");
128
+
129
+ fireEvent.click(openCreatePanelButton(container));
130
+ fireEvent.submit(
131
+ screen
132
+ .getByLabelText(/Instance name/)
133
+ .closest("form") as HTMLFormElement,
134
+ );
135
+
136
+ await waitFor(() => expect(dialog).not.toHaveAttribute("open"));
137
+ expect(
138
+ screen.getByRole("button", { name: "Show details of web-03" }),
139
+ ).toHaveAttribute("aria-pressed", "true");
140
+ expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("7");
141
+ });
142
+ });
143
+ <% } -%>
144
+ });
@@ -0,0 +1,281 @@
1
+ <% if (intl) { -%>
2
+ import { useFormatters, useTranslation } from "@canonical/i18n-react";
3
+ <% } -%>
4
+ <% if (forms) { -%>
5
+ import {
6
+ ContentLayout,
7
+ SidePanel,
8
+ type SidePanelHandle,
9
+ ViewLayout,
10
+ } from "@canonical/react-ds-app";
11
+ <% } else { -%>
12
+ import { ContentLayout, ViewLayout } from "@canonical/react-ds-app";
13
+ <% } -%>
14
+ import { Badge, Button, Tile } from "@canonical/react-ds-global";
15
+ <% if (forms) { -%>
16
+ import { Field, Form } from "@canonical/react-ds-global-form";
17
+ <% } -%>
18
+ import { Head } from "@canonical/react-head";
19
+ <% if (forms) { -%>
20
+ import { type ReactElement, useRef, useState } from "react";
21
+ <% } else { -%>
22
+ import { type ReactElement, useState } from "react";
23
+ <% } -%>
24
+ <% if (intl && forms) { -%>
25
+ import { INSTANCES, RELEASE_OPTIONS, STATUS_KEYS } from "./constants.js";
26
+ <% } else if (intl) { -%>
27
+ import { INSTANCES, STATUS_KEYS } from "./constants.js";
28
+ <% } else if (forms) { -%>
29
+ import { INSTANCES, RELEASE_OPTIONS, STATUS_LABELS } from "./constants.js";
30
+ <% } else { -%>
31
+ import { INSTANCES, STATUS_LABELS } from "./constants.js";
32
+ <% } -%>
33
+ import DashboardBreadcrumbs from "./DashboardBreadcrumbs.js";
34
+ <% if (forms) { -%>
35
+ import type { Instance } from "./types.js";
36
+ <% } -%>
37
+ import "./styles.css";
38
+ <% if (!intl) { -%>
39
+
40
+ // The app has one language, so the formatter is fixed to it.
41
+ const number = new Intl.NumberFormat("en");
42
+ <% } -%>
43
+ <% if (forms) { -%>
44
+
45
+ /** The create form's prefilled values: a second web server on 24.04. */
46
+ const CREATE_DEFAULTS = {
47
+ instance_name: "web-03",
48
+ release: "noble",
49
+ enable_pro: true,
50
+ cloud_init: "#cloud-config\npackages:\n - nginx",
51
+ };
52
+
53
+ /**
54
+ * Builds the instance a submitted create form describes. A new instance is
55
+ * starting and has no address yet.
56
+ */
57
+ function createInstance(data: Record<string, unknown>): Instance {
58
+ const release = RELEASE_OPTIONS.find(
59
+ (option) => option.value === data.release,
60
+ );
61
+
62
+ return {
63
+ name: String(data.instance_name ?? ""),
64
+ status: "starting",
65
+ release: release?.label ?? String(data.release ?? ""),
66
+ ipv4: "",
67
+ memoryGib: 4,
68
+ };
69
+ }
70
+ <% } -%>
71
+
72
+ export default function InstancesPage(): ReactElement {
73
+ <% if (intl) { -%>
74
+ const { t } = useTranslation();
75
+ const formatters = useFormatters();
76
+ <% } -%>
77
+ <% if (forms) { -%>
78
+ const panelRef = useRef<SidePanelHandle>(null);
79
+ const [instances, setInstances] = useState<readonly Instance[]>(INSTANCES);
80
+ <% } else { -%>
81
+ const instances = INSTANCES;
82
+ <% } -%>
83
+ const [selectedName, setSelectedName] = useState<string | null>(null);
84
+ const selected = instances.find((instance) => instance.name === selectedName);
85
+ <% if (forms) { -%>
86
+
87
+ function handleCreate(data: Record<string, unknown>) {
88
+ const instance = createInstance(data);
89
+
90
+ setInstances((current) => [...current, instance]);
91
+ setSelectedName(instance.name);
92
+ panelRef.current?.close();
93
+ }
94
+ <% } -%>
95
+
96
+ const details = (
97
+ <section aria-labelledby="details-title" className="panel">
98
+ <% if (intl) { -%>
99
+ <h2 id="details-title">
100
+ {selected ? selected.name : t("dashboard.instances.details")}
101
+ </h2>
102
+ <% } else { -%>
103
+ <h2 id="details-title">{selected ? selected.name : "Details"}</h2>
104
+ <% } -%>
105
+ {selected ? (
106
+ <dl className="details">
107
+ <dt><%- msg("dashboard.instances.status", "Status") %></dt>
108
+ <% if (intl) { -%>
109
+ <dd>{t(STATUS_KEYS[selected.status])}</dd>
110
+ <% } else { -%>
111
+ <dd>{STATUS_LABELS[selected.status]}</dd>
112
+ <% } -%>
113
+ <dt><%- msg("dashboard.instances.release", "Ubuntu release") %></dt>
114
+ <dd>{selected.release}</dd>
115
+ <dt><%- msg("dashboard.instances.address", "IPv4 address") %></dt>
116
+ <dd>{selected.ipv4 || "—"}</dd>
117
+ <dt><%- msg("dashboard.instances.memory", "Memory") %></dt>
118
+ <% if (intl) { -%>
119
+ <dd>
120
+ {t("dashboard.instances.memoryValue", {
121
+ value: formatters.number(selected.memoryGib),
122
+ })}
123
+ </dd>
124
+ <% } else { -%>
125
+ <dd>{number.format(selected.memoryGib)} GiB</dd>
126
+ <% } -%>
127
+ </dl>
128
+ ) : (
129
+ <p className="muted"><%- msg("dashboard.instances.selectHint", "Select an instance to see its details.") %></p>
130
+ )}
131
+ </section>
132
+ );
133
+
134
+ return (
135
+ <ViewLayout aside={details} className="dashboard-view">
136
+ <ContentLayout>
137
+ <Head title=<%- attr("dashboard.instances.title", "Instances") %> />
138
+ <header className="titlebar">
139
+ <div>
140
+ <DashboardBreadcrumbs />
141
+ <% if (intl) { -%>
142
+ <h1>
143
+ {t("dashboard.instances.heading")}{" "}
144
+ <Badge value={instances.length} />
145
+ </h1>
146
+ <% } else { -%>
147
+ <h1>
148
+ Instances <Badge value={instances.length} />
149
+ </h1>
150
+ <% } -%>
151
+ </div>
152
+ <% if (forms) { -%>
153
+ <div className="actions">
154
+ <Button
155
+ icon="plus"
156
+ importance="primary"
157
+ onClick={() => panelRef.current?.open()}
158
+ >
159
+ <%- msg("dashboard.instances.create", "Create instance") %>
160
+ </Button>
161
+ </div>
162
+ <% } -%>
163
+ </header>
164
+
165
+ <% if (intl) { -%>
166
+ <section
167
+ aria-label={t("dashboard.instances.heading")}
168
+ className="section"
169
+ >
170
+ <% } else { -%>
171
+ <section aria-label="Instances" className="section">
172
+ <% } -%>
173
+ <ul className="tiles">
174
+ {instances.map((instance) => (
175
+ <li key={instance.name}>
176
+ <Tile>
177
+ <Tile.Header>{instance.name}</Tile.Header>
178
+ <Tile.Content>
179
+ <p className="muted">
180
+ <% if (intl) { -%>
181
+ {t("dashboard.instances.summary", {
182
+ status: t(STATUS_KEYS[instance.status]),
183
+ release: instance.release,
184
+ })}
185
+ <% } else { -%>
186
+ {STATUS_LABELS[instance.status]} · {instance.release}
187
+ <% } -%>
188
+ </p>
189
+ <Button
190
+ <% if (intl) { -%>
191
+ aria-label={t("dashboard.instances.show", {
192
+ name: instance.name,
193
+ })}
194
+ <% } else { -%>
195
+ aria-label={`Show details of ${instance.name}`}
196
+ <% } -%>
197
+ aria-pressed={instance.name === selectedName}
198
+ importance="tertiary"
199
+ onClick={() => setSelectedName(instance.name)}
200
+ >
201
+ <%- msg("dashboard.instances.details", "Details") %>
202
+ </Button>
203
+ </Tile.Content>
204
+ </Tile>
205
+ </li>
206
+ ))}
207
+ </ul>
208
+ </section>
209
+ </ContentLayout>
210
+ <% if (forms) { -%>
211
+
212
+ <SidePanel ref={panelRef}>
213
+ <SidePanel.Header dismissLabel=<%- attr("dashboard.instances.closePanel", "Close panel") %>>
214
+ <%- msg("dashboard.instances.create", "Create instance") %>
215
+ </SidePanel.Header>
216
+ <SidePanel.Content>
217
+ <Form
218
+ defaultValues={CREATE_DEFAULTS}
219
+ id="create-instance"
220
+ onSubmit={handleCreate}
221
+ >
222
+ <Field
223
+ inputType="text"
224
+ label=<%- attr("dashboard.instances.name", "Instance name") %>
225
+ name="instance_name"
226
+ registerProps={{
227
+ required: <%- str("dashboard.instances.nameRequired", "Instance name is required") %>,
228
+ }}
229
+ />
230
+ <Field
231
+ inputType="select"
232
+ label=<%- attr("dashboard.instances.release", "Ubuntu release") %>
233
+ name="release"
234
+ options={RELEASE_OPTIONS}
235
+ />
236
+ <Field
237
+ controlLabel=<%- attr("dashboard.instances.pro", "Enable Ubuntu Pro") %>
238
+ <% if (intl) { -%>
239
+ description={t("dashboard.instances.proDescription")}
240
+ <% } else { -%>
241
+ description="Security and compliance coverage for your instances, including extended support for the packages you care about."
242
+ <% } -%>
243
+ inputType="checkbox"
244
+ isOptional
245
+ name="enable_pro"
246
+ />
247
+ <Field
248
+ <% if (intl) { -%>
249
+ description={t("dashboard.instances.cloudInitDescription")}
250
+ <% } else { -%>
251
+ description="Configuration to run on first boot: packages to install, users to create, commands to run."
252
+ <% } -%>
253
+ inputType="textarea"
254
+ isOptional
255
+ label=<%- attr("dashboard.instances.cloudInit", "Cloud-init user data") %>
256
+ name="cloud_init"
257
+ />
258
+ </Form>
259
+ </SidePanel.Content>
260
+ <SidePanel.Footer>
261
+ <% if (intl) { -%>
262
+ <Button onClick={() => panelRef.current?.close()}>
263
+ {t("dashboard.cancel")}
264
+ </Button>
265
+ <% } else { -%>
266
+ <Button onClick={() => panelRef.current?.close()}>Cancel</Button>
267
+ <% } -%>
268
+ <Button
269
+ anticipation="constructive"
270
+ form="create-instance"
271
+ importance="primary"
272
+ type="submit"
273
+ >
274
+ <%- msg("dashboard.instances.create", "Create instance") %>
275
+ </Button>
276
+ </SidePanel.Footer>
277
+ </SidePanel>
278
+ <% } -%>
279
+ </ViewLayout>
280
+ );
281
+ }
@@ -0,0 +1,76 @@
1
+ import { fireEvent, screen, within } from "@testing-library/react";
2
+ import { beforeAll, describe, expect, it, vi } from "vitest";
3
+ import {
4
+ installMatchMedia,
5
+ installResizeObserver,
6
+ } from "../../testing/browserStubs.js";
7
+ import renderWithProviders from "../../testing/renderWithProviders.js";
8
+ import OverviewPage from "./OverviewPage.js";
9
+
10
+ beforeAll(() => {
11
+ installMatchMedia();
12
+ installResizeObserver();
13
+ });
14
+
15
+ describe("OverviewPage", () => {
16
+ it("shows the status tiles, the releases and the activity feed", () => {
17
+ renderWithProviders(<OverviewPage />, "/dashboard");
18
+
19
+ expect(
20
+ screen.getByRole("heading", { level: 1, name: "Overview" }),
21
+ ).toBeInTheDocument();
22
+ expect(screen.getByText("128")).toBeInTheDocument();
23
+ expect(screen.getByText("Ubuntu 25.10")).toBeInTheDocument();
24
+ expect(
25
+ screen.getByRole("heading", { level: 2, name: "Activity" }),
26
+ ).toBeInTheDocument();
27
+ });
28
+
29
+ it("shows figures and relative times in words", () => {
30
+ renderWithProviders(<OverviewPage />, "/dashboard");
31
+
32
+ expect(screen.getByText(/^2\.4\sTB$/u)).toBeInTheDocument();
33
+ expect(screen.getByText("2 minutes ago")).toBeInTheDocument();
34
+ expect(screen.getByText("yesterday")).toBeInTheDocument();
35
+ expect(screen.getByText("Beta · kernel 6.17")).toBeInTheDocument();
36
+ });
37
+ <% if (intl) { -%>
38
+
39
+ it("reads fully in French: labels, data, figures and relative times", () => {
40
+ renderWithProviders(<OverviewPage />, "/dashboard", "fr");
41
+
42
+ expect(
43
+ screen.getByRole("heading", { level: 1, name: "Vue d'ensemble" }),
44
+ ).toBeInTheDocument();
45
+ expect(screen.getByText(/^2,4\sTo$/u)).toBeInTheDocument();
46
+ expect(screen.getByText("web-01 démarrée")).toBeInTheDocument();
47
+ expect(screen.getByText("il y a 2 minutes")).toBeInTheDocument();
48
+ expect(screen.getByText("hier")).toBeInTheDocument();
49
+ expect(screen.getByText("Planificateur")).toBeInTheDocument();
50
+ expect(screen.getByText("Bêta · noyau 6.17")).toBeInTheDocument();
51
+ expect(
52
+ screen.getByRole("navigation", { name: "Fil d'Ariane" }),
53
+ ).toBeInTheDocument();
54
+ });
55
+ <% } -%>
56
+
57
+ it("marks Overview as the current crumb", () => {
58
+ renderWithProviders(<OverviewPage />, "/dashboard");
59
+
60
+ const trail = screen.getByRole("navigation", { name: "Breadcrumb" });
61
+
62
+ expect(within(trail).getByText("Overview")).toHaveAttribute(
63
+ "aria-current",
64
+ "page",
65
+ );
66
+ });
67
+
68
+ it("navigates to the instances from Create instance", () => {
69
+ const { router } = renderWithProviders(<OverviewPage />, "/dashboard");
70
+ const navigate = vi.spyOn(router, "navigate");
71
+
72
+ fireEvent.click(screen.getByRole("button", { name: "Create instance" }));
73
+
74
+ expect(navigate).toHaveBeenCalledWith("dashboardInstances");
75
+ });
76
+ });