forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Extend version toggle to support runs routes #2478
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
morgan-wowk
wants to merge
1
commit into
master
Choose a base branch
from
add-editor-toggle-to-runs-view
base: master
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.
+120
−22
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { EditorVersionToggle } from "./EditorVersionToggle"; | ||
|
|
||
| const mockNavigate = vi.fn(); | ||
| let mockPathname = "/"; | ||
| let mockFlagEnabled = true; | ||
|
|
||
| vi.mock("@tanstack/react-router", async (importOriginal) => ({ | ||
| ...(await importOriginal()), | ||
| useNavigate: () => mockNavigate, | ||
| useLocation: () => ({ pathname: mockPathname }), | ||
| })); | ||
|
|
||
| vi.mock("./Settings/useFlags", () => ({ | ||
| useFlagValue: () => mockFlagEnabled, | ||
| })); | ||
|
|
||
| describe("EditorVersionToggle", () => { | ||
| beforeEach(() => { | ||
| mockNavigate.mockClear(); | ||
| mockPathname = "/"; | ||
| mockFlagEnabled = true; | ||
| }); | ||
|
|
||
| it("renders nothing when the v2_editor flag is disabled", () => { | ||
| mockFlagEnabled = false; | ||
| mockPathname = "/editor/my-pipeline"; | ||
|
|
||
| render(<EditorVersionToggle />); | ||
|
|
||
| expect(screen.queryByRole("button")).toBeNull(); | ||
| }); | ||
|
|
||
| it("renders nothing on routes without a v1/v2 counterpart", () => { | ||
| mockPathname = "/pipelines"; | ||
|
|
||
| render(<EditorVersionToggle />); | ||
|
|
||
| expect(screen.queryByRole("button")).toBeNull(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["/editor/my-pipeline", "/editor-v2/my-pipeline", "Switch to new editor"], | ||
| ["/runs/run-123", "/runs-v2/run-123", "Switch to new view"], | ||
| ["/runs/run-123/sub-456", "/runs-v2/run-123/sub-456", "Switch to new view"], | ||
| ])("switches %s to the new version (%s)", (from, to, label) => { | ||
| mockPathname = from; | ||
|
|
||
| render(<EditorVersionToggle />); | ||
|
|
||
| const button = screen.getByRole("button", { name: label }); | ||
| fireEvent.click(button); | ||
|
|
||
| expect(mockNavigate).toHaveBeenCalledWith({ to }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [ | ||
| "/editor-v2/my-pipeline", | ||
| "/editor/my-pipeline", | ||
| "Switch to legacy editor", | ||
| ], | ||
| ["/runs-v2/run-123", "/runs/run-123", "Switch to legacy view"], | ||
| [ | ||
| "/runs-v2/run-123/sub-456", | ||
| "/runs/run-123/sub-456", | ||
| "Switch to legacy view", | ||
| ], | ||
| ])("switches %s to the legacy version (%s)", (from, to, label) => { | ||
| mockPathname = from; | ||
|
|
||
| render(<EditorVersionToggle />); | ||
|
|
||
| const button = screen.getByRole("button", { name: label }); | ||
| fireEvent.click(button); | ||
|
|
||
| expect(mockNavigate).toHaveBeenCalledWith({ to }); | ||
| }); | ||
| }); |
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 |
|---|---|---|
|
|
@@ -2,15 +2,44 @@ import { useLocation, useNavigate } from "@tanstack/react-router"; | |
|
|
||
| import TooltipButton from "@/components/shared/Buttons/TooltipButton"; | ||
| import { Icon } from "@/components/ui/icon"; | ||
| import { APP_ROUTES, EDITOR_PATH } from "@/routes/router"; | ||
| import { APP_ROUTES, EDITOR_PATH } from "@/routes/appRoutes"; | ||
|
|
||
| import { useFlagValue } from "./Settings/useFlags"; | ||
|
|
||
| type EditorVersion = "v1" | "v2"; | ||
|
|
||
| const detectEditorVersion = (pathname: string): EditorVersion | null => { | ||
| if (pathname.startsWith(`${APP_ROUTES.EDITOR_V2}/`)) return "v2"; | ||
| if (pathname.startsWith(`${EDITOR_PATH}/`)) return "v1"; | ||
| const ROUTE_BASE_PAIRS: { v1: string; v2: string; noun: string }[] = [ | ||
| { v1: EDITOR_PATH, v2: APP_ROUTES.EDITOR_V2, noun: "editor" }, | ||
| { v1: APP_ROUTES.RUNS, v2: APP_ROUTES.RUNS_V2, noun: "view" }, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put the version toggle on the runs page behind a beta flag @Mbeaulne ? I would +1 that the page needs some work. The default layout for example. |
||
| ]; | ||
|
|
||
| type ToggleTarget = { | ||
| to: string; | ||
| tooltip: string; | ||
| targetVersion: EditorVersion; | ||
| }; | ||
|
|
||
| const getToggleTarget = (pathname: string): ToggleTarget | null => { | ||
| for (const { v1, v2, noun } of ROUTE_BASE_PAIRS) { | ||
| if (pathname.startsWith(`${v2}/`)) { | ||
| const rest = pathname.slice(v2.length + 1); | ||
| if (!rest) return null; | ||
| return { | ||
| to: `${v1}/${rest}`, | ||
| tooltip: `Switch to legacy ${noun}`, | ||
| targetVersion: "v1", | ||
| }; | ||
| } | ||
| if (pathname.startsWith(`${v1}/`)) { | ||
| const rest = pathname.slice(v1.length + 1); | ||
| if (!rest) return null; | ||
| return { | ||
| to: `${v2}/${rest}`, | ||
| tooltip: `Switch to new ${noun}`, | ||
| targetVersion: "v2", | ||
| }; | ||
| } | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
|
|
@@ -21,28 +50,16 @@ export const EditorVersionToggle = () => { | |
|
|
||
| if (!isEnabled) return null; | ||
|
|
||
| const version = detectEditorVersion(location.pathname); | ||
| if (!version) return null; | ||
|
|
||
| const lastSegment = location.pathname.split("/").pop() ?? ""; | ||
| const pipelineName = decodeURIComponent(lastSegment); | ||
| if (!pipelineName) return null; | ||
|
|
||
| const targetVersion = version === "v1" ? "v2" : "v1"; | ||
| const targetPath = | ||
| targetVersion === "v2" | ||
| ? `${APP_ROUTES.EDITOR_V2}/${encodeURIComponent(pipelineName)}` | ||
| : `${EDITOR_PATH}/${encodeURIComponent(pipelineName)}`; | ||
| const tooltip = | ||
| targetVersion === "v2" ? "Switch to new editor" : "Switch to legacy editor"; | ||
| const target = getToggleTarget(location.pathname); | ||
| if (!target) return null; | ||
|
|
||
| return ( | ||
| <TooltipButton | ||
| tooltip={tooltip} | ||
| onClick={() => navigate({ to: targetPath })} | ||
| aria-label={tooltip} | ||
| tooltip={target.tooltip} | ||
| onClick={() => navigate({ to: target.to })} | ||
| aria-label={target.tooltip} | ||
| > | ||
| <Icon name={targetVersion === "v2" ? "Sparkles" : "History"} /> | ||
| <Icon name={target.targetVersion === "v2" ? "Sparkles" : "History"} /> | ||
| </TooltipButton> | ||
| ); | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Re: line +1]
nit: Open to suggestions for a new name for this component. Just
VersionToggleseems fitting.See this comment inline on Graphite.