-
Notifications
You must be signed in to change notification settings - Fork 9
feat(web): add multi-network support with network switcher #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
silent-cipher
wants to merge
6
commits into
refactor/multi-network-config
Choose a base branch
from
refactor/web/multi-network-ui
base: refactor/multi-network-config
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
852f4e8
feat: add multi-network support with in-page network switcher
silent-cipher c9ccc37
Merge branch 'refactor/multi-network-config' into refactor/web/multi-…
silent-cipher 706421c
Merge branch 'refactor/multi-network-config' into refactor/web/multi-…
silent-cipher 2d1b307
fix(web): simplify network parameter handling
silent-cipher 2b359c2
refactor(web): consolidate provider loading state checks
silent-cipher 6ef6d83
chore: address pr comments
silent-cipher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
apps/web/src/components/shared/Network/EnvironmentSwitcher.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { server } from "@test/mocks/server"; | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import { HttpResponse, http } from "msw"; | ||
| import { MemoryRouter } from "react-router-dom"; | ||
| import { SWRConfig } from "swr"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import EnvironmentSwitcher from "./EnvironmentSwitcher"; | ||
|
|
||
| const configUrl = "/api/config"; | ||
|
|
||
| function makeConfig(network: "mainnet" | "calibration") { | ||
| return { | ||
| networks: [ | ||
| { | ||
| network, | ||
| dealsPerSpPerHour: 4, | ||
| retrievalsPerSpPerHour: 4, | ||
| dataSetCreationsPerSpPerHour: 0, | ||
| pullChecksPerSpPerHour: 0, | ||
| dataRetentionPollIntervalSeconds: 3600, | ||
| providersRefreshIntervalSeconds: 3600, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| // Each render gets a fresh SWR cache so cases setting different /api/config | ||
| // responses don't reuse a value cached by a previous case. | ||
| function renderSwitcher() { | ||
| return render( | ||
| <SWRConfig value={{ provider: () => new Map() }}> | ||
| <MemoryRouter> | ||
| <EnvironmentSwitcher /> | ||
| </MemoryRouter> | ||
| </SWRConfig>, | ||
| ); | ||
| } | ||
|
|
||
| describe("EnvironmentSwitcher", () => { | ||
| it("links to Staging when current deployment monitors mainnet (Production)", async () => { | ||
| server.use(http.get(configUrl, () => HttpResponse.json(makeConfig("mainnet")))); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const link = await screen.findByRole("link", { name: /Switch to Staging/i }); | ||
| expect(link).toHaveAttribute("href", "https://staging.dealbot.filoz.org"); | ||
| }); | ||
|
|
||
| it("links to Production when current deployment monitors calibration (Staging)", async () => { | ||
| server.use(http.get(configUrl, () => HttpResponse.json(makeConfig("calibration")))); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const link = await screen.findByRole("link", { name: /Switch to Production/i }); | ||
| expect(link).toHaveAttribute("href", "https://dealbot.filoz.org"); | ||
| }); | ||
|
|
||
| it("shows loading skeleton initially", () => { | ||
| server.use(http.get(configUrl, () => new Promise(() => {}))); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| expect(container.querySelector(".animate-pulse")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides component when config fails to load", async () => { | ||
| server.use(http.get(configUrl, () => new HttpResponse(null, { status: 500 }))); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| it("opens link with rel=noreferrer", async () => { | ||
| server.use(http.get(configUrl, () => HttpResponse.json(makeConfig("mainnet")))); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const link = await screen.findByRole("link", { name: /Switch to Staging/i }); | ||
| expect(link).toHaveAttribute("rel", "noreferrer"); | ||
| }); | ||
|
|
||
| it("shows amber dot when linking to Staging (calibration)", async () => { | ||
| server.use(http.get(configUrl, () => HttpResponse.json(makeConfig("mainnet")))); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await screen.findByRole("link", { name: /Switch to Staging/i }); | ||
| expect(container.querySelector(".bg-amber-500")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows emerald dot when linking to Production (mainnet)", async () => { | ||
| server.use(http.get(configUrl, () => HttpResponse.json(makeConfig("calibration")))); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await screen.findByRole("link", { name: /Switch to Production/i }); | ||
| expect(container.querySelector(".bg-emerald-500")).toBeInTheDocument(); | ||
| }); | ||
| }); |
35 changes: 35 additions & 0 deletions
35
apps/web/src/components/shared/Network/EnvironmentSwitcher.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { ArrowLeftRight } from "lucide-react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useActiveNetworks } from "@/hooks/useActiveNetworks"; | ||
| import { ENVIRONMENT_LABEL, NETWORK_DEPLOYMENT_URL, NETWORK_DOT_CLASS } from "./constants"; | ||
|
|
||
| /** | ||
| * Header control that links to the sibling deployment for the other environment. | ||
| * | ||
| * Environment is determined by which networks the current deployment monitors: | ||
| * - Active networks include mainnet → Production deployment → link to Staging | ||
| * - Active networks are calibration-only → Staging deployment → link to Production | ||
| */ | ||
| export default function EnvironmentSwitcher() { | ||
| const { activeNetworks, loading, error } = useActiveNetworks(); | ||
|
|
||
| if (loading) { | ||
| return <div className="h-8 w-8 sm:w-40 animate-pulse rounded-md bg-muted" aria-hidden />; | ||
| } | ||
|
|
||
| if (error || activeNetworks.length === 0) return null; | ||
|
|
||
| const isProduction = activeNetworks.includes("mainnet"); | ||
| const targetEnv = isProduction ? "calibration" : "mainnet"; | ||
| const label = `Switch to ${ENVIRONMENT_LABEL[targetEnv]}`; | ||
|
|
||
| return ( | ||
| <Button asChild variant="outline" size="sm" title={label} aria-label={label}> | ||
| <a href={NETWORK_DEPLOYMENT_URL[targetEnv]} rel="noreferrer"> | ||
| <span className={`h-2 w-2 rounded-full ${NETWORK_DOT_CLASS[targetEnv]}`} aria-hidden /> | ||
| <ArrowLeftRight className="h-3.5 w-3.5" /> | ||
| <span className="hidden sm:inline">{ENVIRONMENT_LABEL[targetEnv]}</span> | ||
| </a> | ||
| </Button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 31 additions & 111 deletions
142
apps/web/src/components/shared/Network/NetworkSwitcher.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,131 +1,51 @@ | ||
| import { server } from "@test/mocks/server"; | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import { HttpResponse, http } from "msw"; | ||
| import { MemoryRouter } from "react-router-dom"; | ||
| import { SWRConfig } from "swr"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import NetworkSwitcher from "./NetworkSwitcher"; | ||
|
|
||
| const configUrl = "/api/config"; | ||
|
|
||
| // Each render gets a fresh SWR cache so cases setting different /api/config | ||
| // responses don't reuse a value cached by a previous case. | ||
| function renderSwitcher() { | ||
| return render( | ||
| <SWRConfig value={{ provider: () => new Map() }}> | ||
| <MemoryRouter> | ||
| <NetworkSwitcher /> | ||
| </MemoryRouter> | ||
| </SWRConfig>, | ||
| ); | ||
| } | ||
|
|
||
| describe("NetworkSwitcher", () => { | ||
| it("shows current network and a link to switch to the other deployment (mainnet → calibration)", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => | ||
| HttpResponse.json({ | ||
| network: "mainnet", | ||
| jobs: {}, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const switchLink = await screen.findByRole("link", { name: /Switch to Calibration/i }); | ||
| expect(switchLink).toHaveAttribute("href", "https://staging.dealbot.filoz.org"); | ||
| it("renders nothing when only one network is active", () => { | ||
| const { container } = render(<NetworkSwitcher networks={["mainnet"]} selected="mainnet" onChange={vi.fn()} />); | ||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
|
|
||
| it("offers switching to mainnet when current instance monitors calibration", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => | ||
| HttpResponse.json({ | ||
| network: "calibration", | ||
| jobs: {}, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const switchLink = await screen.findByRole("link", { name: /Switch to Mainnet/i }); | ||
| expect(switchLink).toHaveAttribute("href", "https://dealbot.filoz.org"); | ||
| it("renders a tab for each network when multiple are active", () => { | ||
| render(<NetworkSwitcher networks={["mainnet", "calibration"]} selected="mainnet" onChange={vi.fn()} />); | ||
| expect(screen.getByRole("tab", { name: /mainnet/i })).toBeInTheDocument(); | ||
| expect(screen.getByRole("tab", { name: /calibration/i })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows loading state initially", () => { | ||
| server.use(http.get(configUrl, () => new Promise(() => {}))); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| const loadingElement = container.querySelector(".animate-pulse"); | ||
| expect(loadingElement).toBeInTheDocument(); | ||
| expect(loadingElement).toHaveClass("bg-muted"); | ||
| it("marks the selected network tab as aria-selected", () => { | ||
| render(<NetworkSwitcher networks={["mainnet", "calibration"]} selected="calibration" onChange={vi.fn()} />); | ||
| expect(screen.getByRole("tab", { name: /calibration/i })).toHaveAttribute("aria-selected", "true"); | ||
| expect(screen.getByRole("tab", { name: /mainnet/i })).toHaveAttribute("aria-selected", "false"); | ||
| }); | ||
|
|
||
| it("hides component when network config fails to load", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => { | ||
| return new HttpResponse(null, { status: 500 }); | ||
| }), | ||
| ); | ||
| it("calls onChange with the clicked network", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<NetworkSwitcher networks={["mainnet", "calibration"]} selected="mainnet" onChange={onChange} />); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
| await user.click(screen.getByRole("tab", { name: /calibration/i })); | ||
| expect(onChange).toHaveBeenCalledWith("calibration"); | ||
| }); | ||
|
|
||
| it("opens link in new tab with proper security attributes", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => | ||
| HttpResponse.json({ | ||
| network: "mainnet", | ||
| jobs: {}, | ||
| }), | ||
| ), | ||
| it("shows emerald dot for mainnet tab", () => { | ||
| const { container } = render( | ||
| <NetworkSwitcher networks={["mainnet", "calibration"]} selected="mainnet" onChange={vi.fn()} />, | ||
| ); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| const switchLink = await screen.findByRole("link", { name: /Switch to Calibration/i }); | ||
| expect(switchLink).toHaveAttribute("rel", "noreferrer"); | ||
| expect(container.querySelector(".bg-emerald-500")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays correct network indicator color for calibration", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => | ||
| HttpResponse.json({ | ||
| network: "calibration", | ||
| jobs: {}, | ||
| }), | ||
| ), | ||
| it("shows amber dot for calibration tab", () => { | ||
| const { container } = render( | ||
| <NetworkSwitcher networks={["mainnet", "calibration"]} selected="mainnet" onChange={vi.fn()} />, | ||
| ); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await screen.findByRole("link", { name: /Switch to Mainnet/i }); | ||
|
|
||
| const networkDot = container.querySelector(".bg-emerald-500"); | ||
| expect(networkDot).toBeInTheDocument(); | ||
| expect(container.querySelector(".bg-amber-500")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays correct network indicator color for mainnet", async () => { | ||
| server.use( | ||
| http.get(configUrl, () => | ||
| HttpResponse.json({ | ||
| network: "mainnet", | ||
| jobs: {}, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const { container } = renderSwitcher(); | ||
|
|
||
| await screen.findByRole("link", { name: /Switch to Calibration/i }); | ||
|
|
||
| const networkDot = container.querySelector(".bg-amber-500"); | ||
| expect(networkDot).toBeInTheDocument(); | ||
| it("has role=tablist on the container", () => { | ||
| render(<NetworkSwitcher networks={["mainnet", "calibration"]} selected="mainnet" onChange={vi.fn()} />); | ||
| expect(screen.getByRole("tablist")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.