Skip to content

Commit dd46cff

Browse files
committed
feat: persist domain selection
1 parent f6a1ec7 commit dd46cff

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/pages/apps/[id]/environments/[env-id]/analytics/Analytics.spec.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RenderResult, render, waitFor } from "@testing-library/react";
22
import { EnvironmentContext } from "~/pages/apps/[id]/environments/Environment.context";
3+
import { MemoryRouter } from "react-router";
34
import mockApp from "~/testing/data/mock_app";
45
import mockEnvironments from "~/testing/data/mock_environments";
56
import { mockFetchDomains } from "~/testing/nocks/nock_domains";
@@ -18,6 +19,7 @@ jest.mock("recharts", () => ({
1819

1920
interface WrapperProps {
2021
hasDomain?: boolean;
22+
selectedDomainName?: string;
2123
}
2224

2325
describe("~/pages/apps/[id]/environments/[env-id]/analytics/Analytics.tsx", () => {
@@ -26,7 +28,10 @@ describe("~/pages/apps/[id]/environments/[env-id]/analytics/Analytics.tsx", () =
2628
let currentEnv: Environment;
2729
let currentEnvs: Environment[];
2830

29-
const createWrapper = async ({ hasDomain = true }: WrapperProps) => {
31+
const createWrapper = async ({
32+
hasDomain = true,
33+
selectedDomainName = "",
34+
}: WrapperProps) => {
3035
currentApp = mockApp();
3136
currentEnvs = mockEnvironments({ app: currentApp });
3237
currentEnv = currentEnvs[0];
@@ -38,14 +43,24 @@ describe("~/pages/apps/[id]/environments/[env-id]/analytics/Analytics.tsx", () =
3843
verified: true,
3944
response: {
4045
domains: hasDomain
41-
? [{ domainName: "www.stormkit.io", verified: true, id: "15" }]
46+
? [
47+
{ domainName: "www.stormkit.io", verified: true, id: "15" },
48+
{ domainName: "app.stormkit.io", verified: true, id: "50" },
49+
]
4250
: [],
4351
},
4452
});
4553

4654
wrapper = render(
4755
<EnvironmentContext.Provider value={{ environment: currentEnv }}>
48-
<Analytics />
56+
<MemoryRouter
57+
initialEntries={[
58+
{ pathname: "/", search: `domain=${selectedDomainName}` },
59+
]}
60+
initialIndex={0}
61+
>
62+
<Analytics />
63+
</MemoryRouter>
4964
</EnvironmentContext.Provider>
5065
);
5166

@@ -83,6 +98,27 @@ describe("~/pages/apps/[id]/environments/[env-id]/analytics/Analytics.tsx", () =
8398
test("should contain countries section", () => {
8499
expect(wrapper.getByText("Countries")).toBeTruthy();
85100
});
101+
102+
test("should have the initial domain selected", async () => {
103+
await waitFor(() => {
104+
expect(wrapper.getByText("www.stormkit.io")).toBeTruthy();
105+
});
106+
});
107+
});
108+
109+
describe("with a pre-selected domain", () => {
110+
beforeEach(async () => {
111+
await createWrapper({
112+
hasDomain: true,
113+
selectedDomainName: "app.stormkit.io",
114+
});
115+
});
116+
117+
test("should have that domain selected", async () => {
118+
await waitFor(() => {
119+
expect(wrapper.getByText("app.stormkit.io")).toBeTruthy();
120+
});
121+
});
86122
});
87123

88124
describe("when environment domain name is not configured", () => {

src/pages/apps/[id]/environments/[env-id]/analytics/Analytics.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TimeSpan } from "./index.d";
22
import { useContext, useState } from "react";
3+
import { useSearchParams } from "react-router-dom";
34
import Button from "@mui/material/Button";
45
import Link from "@mui/material/Link";
56
import Box from "@mui/material/Box";
@@ -15,6 +16,7 @@ import ByCountries from "./Countries";
1516

1617
export default function Analytics() {
1718
const [timeSpan, setTimeSpan] = useState<TimeSpan>("24h");
19+
const [params, setParams] = useSearchParams();
1820
const [domain, setDomain] = useState<Domain>();
1921
const [noDomainYet, setNoDomainYet] = useState(false);
2022
const { environment } = useContext(EnvironmentContext);
@@ -60,15 +62,20 @@ export default function Analytics() {
6062
appId={environment.appId}
6163
envId={environment.id!}
6264
fullWidth={false}
63-
onFetch={d => {
64-
if (d?.[0]) {
65-
setDomain(d[0]);
65+
onFetch={domains => {
66+
if (domains?.[0]) {
67+
setDomain(
68+
domains.find(d => d.domainName === params.get("domain")) ||
69+
domains[0]
70+
);
6671
} else {
6772
setNoDomainYet(true);
6873
}
6974
}}
7075
onDomainSelect={d => {
71-
setDomain(d ? (d[0] as Domain) : undefined);
76+
const selectedDomain = d ? (d[0] as Domain) : undefined;
77+
setDomain(selectedDomain);
78+
setParams({ domain: selectedDomain?.domainName || "" });
7279
}}
7380
/>
7481
}

0 commit comments

Comments
 (0)