-
Notifications
You must be signed in to change notification settings - Fork 25
feat: make dashboard view default #2858
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // ABOUTME: Resolves the homepage destination based on a configured view property, | ||
| // ABOUTME: a well-known view name, or falls back to the health page. | ||
|
|
||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { Navigate } from "react-router-dom"; | ||
| import { | ||
| getViewIdByName, | ||
| getViewIdByNamespaceAndName | ||
| } from "../api/services/views"; | ||
| import { useFeatureFlagsContext } from "../context/FeatureFlagsContext"; | ||
| import FullPageSkeletonLoader from "../ui/SkeletonLoader/FullPageSkeletonLoader"; | ||
|
|
||
| import { | ||
| DASHBOARD_VIEW_PROPERTY, | ||
| FALLBACK_VIEW_NAME, | ||
| UUID_REGEX | ||
| } from "./dashboardViewConstants"; | ||
|
|
||
| async function resolveViewId(value: string): Promise<string | undefined> { | ||
| if (value.includes("/")) { | ||
| const [namespace, name] = value.split("/", 2); | ||
| return getViewIdByNamespaceAndName(namespace, name); | ||
| } | ||
| return getViewIdByName(value); | ||
| } | ||
|
|
||
| export function HomepageRedirect() { | ||
| const { featureFlags } = useFeatureFlagsContext(); | ||
|
|
||
| const dashboardViewValue = featureFlags.find( | ||
| (f) => f.name === DASHBOARD_VIEW_PROPERTY | ||
| )?.value; | ||
|
|
||
| const isUUID = dashboardViewValue && UUID_REGEX.test(dashboardViewValue); | ||
|
|
||
| const { data: redirectPath, isLoading } = useQuery({ | ||
| queryKey: ["homepage-redirect", dashboardViewValue], | ||
| queryFn: async () => { | ||
| if (dashboardViewValue) { | ||
| const viewId = await resolveViewId(dashboardViewValue); | ||
| if (viewId) return `/views/${viewId}`; | ||
| return "/health"; | ||
| } | ||
|
|
||
| const viewId = await getViewIdByName(FALLBACK_VIEW_NAME); | ||
| if (viewId) return `/views/${viewId}`; | ||
| return "/health"; | ||
| }, | ||
| enabled: !isUUID | ||
| }); | ||
|
|
||
| if (isUUID) { | ||
| return <Navigate to={`/views/${dashboardViewValue}`} replace />; | ||
| } | ||
|
|
||
| if (isLoading || !redirectPath) { | ||
| return <FullPageSkeletonLoader />; | ||
| } | ||
|
|
||
| return <Navigate to={redirectPath} replace />; | ||
| } |
169 changes: 169 additions & 0 deletions
169
src/components/__tests__/HomepageRedirect.unit.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,169 @@ | ||
| // ABOUTME: Tests for HomepageRedirect component that resolves the homepage | ||
| // ABOUTME: destination based on properties and view lookups. | ||
|
|
||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import { MemoryRouter, Route, Routes } from "react-router-dom"; | ||
| import { HomepageRedirect } from "../HomepageRedirect"; | ||
| import { FeatureFlagsContext } from "../../context/FeatureFlagsContext"; | ||
| import type { FeatureFlagsState } from "../../context/FeatureFlagsContext"; | ||
| import type { FeatureFlag } from "../../services/permissions/permissionsService"; | ||
| import { DASHBOARD_VIEW_PROPERTY } from "../dashboardViewConstants"; | ||
|
|
||
| import { | ||
| getViewIdByName, | ||
| getViewIdByNamespaceAndName | ||
| } from "../../api/services/views"; | ||
|
|
||
| jest.mock("../../api/services/views", () => ({ | ||
| getViewIdByName: jest.fn(), | ||
| getViewIdByNamespaceAndName: jest.fn() | ||
| })); | ||
|
|
||
| const mockedGetViewIdByName = getViewIdByName as jest.MockedFunction< | ||
| typeof getViewIdByName | ||
| >; | ||
| const mockedGetViewIdByNamespaceAndName = | ||
| getViewIdByNamespaceAndName as jest.MockedFunction< | ||
| typeof getViewIdByNamespaceAndName | ||
| >; | ||
|
|
||
| function createQueryClient() { | ||
| return new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { retry: false } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function buildFeatureFlagsState( | ||
| featureFlags: FeatureFlag[] = [] | ||
| ): FeatureFlagsState { | ||
| return { | ||
| featureFlags, | ||
| featureFlagsLoaded: true, | ||
| refreshFeatureFlags: () => {}, | ||
| isFeatureDisabled: () => false | ||
| }; | ||
| } | ||
|
|
||
| function renderWithProviders(featureFlags: FeatureFlag[] = []) { | ||
| const queryClient = createQueryClient(); | ||
| return render( | ||
| <QueryClientProvider client={queryClient}> | ||
| <FeatureFlagsContext.Provider | ||
| value={buildFeatureFlagsState(featureFlags)} | ||
| > | ||
| <MemoryRouter initialEntries={["/"]}> | ||
| <Routes> | ||
| <Route path="/" element={<HomepageRedirect />} /> | ||
| <Route | ||
| path="/health" | ||
| element={<div data-testid="health-page">Health</div>} | ||
| /> | ||
| <Route | ||
| path="/views/:id" | ||
| element={<div data-testid="view-page">View</div>} | ||
| /> | ||
| </Routes> | ||
| </MemoryRouter> | ||
| </FeatureFlagsContext.Provider> | ||
| </QueryClientProvider> | ||
| ); | ||
| } | ||
|
|
||
| function dashboardViewFlag(value: string): FeatureFlag { | ||
| return { | ||
| name: DASHBOARD_VIEW_PROPERTY, | ||
| value, | ||
| description: "", | ||
| source: "", | ||
| type: "" | ||
| }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("HomepageRedirect", () => { | ||
| it("redirects to /views/{uuid} when property is a UUID", async () => { | ||
| const uuid = "550e8400-e29b-41d4-a716-446655440000"; | ||
| renderWithProviders([dashboardViewFlag(uuid)]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("view-page")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(mockedGetViewIdByName).not.toHaveBeenCalled(); | ||
| expect(mockedGetViewIdByNamespaceAndName).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("looks up by namespace/name when property contains a slash", async () => { | ||
| const viewId = "aaa-bbb-ccc"; | ||
| mockedGetViewIdByNamespaceAndName.mockResolvedValue(viewId); | ||
|
|
||
| renderWithProviders([dashboardViewFlag("my-namespace/my-view")]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("view-page")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(mockedGetViewIdByNamespaceAndName).toHaveBeenCalledWith( | ||
| "my-namespace", | ||
| "my-view" | ||
| ); | ||
| }); | ||
|
|
||
| it("looks up by name when property is a plain string", async () => { | ||
| const viewId = "ddd-eee-fff"; | ||
| mockedGetViewIdByName.mockResolvedValue(viewId); | ||
|
|
||
| renderWithProviders([dashboardViewFlag("my-dashboard")]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("view-page")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(mockedGetViewIdByName).toHaveBeenCalledWith("my-dashboard"); | ||
| }); | ||
|
|
||
| it("falls back to mission-control-dashboard view when no property is set", async () => { | ||
| const viewId = "ggg-hhh-iii"; | ||
| mockedGetViewIdByName.mockResolvedValue(viewId); | ||
|
|
||
| renderWithProviders([]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("view-page")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(mockedGetViewIdByName).toHaveBeenCalledWith( | ||
| "mission-control-dashboard" | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to /health when no property and no mission-control-dashboard view", async () => { | ||
| mockedGetViewIdByName.mockResolvedValue(undefined); | ||
|
|
||
| renderWithProviders([]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("health-page")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(mockedGetViewIdByName).toHaveBeenCalledWith( | ||
| "mission-control-dashboard" | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to /health when property name lookup fails", async () => { | ||
| mockedGetViewIdByName.mockResolvedValue(undefined); | ||
|
|
||
| renderWithProviders([dashboardViewFlag("nonexistent-view")]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId("health-page")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
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,9 @@ | ||
| // ABOUTME: Shared constants for resolving the dashboard view from properties. | ||
| // ABOUTME: Used by HomepageRedirect and the sidebar navigation. | ||
|
|
||
| export const UUID_REGEX = | ||
| /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; | ||
|
|
||
| export const FALLBACK_VIEW_NAME = "mission-control-dashboard"; | ||
|
|
||
| export const DASHBOARD_VIEW_PROPERTY = "defaults.dashboard_view"; |
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
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.
🧩 Analysis chain
🏁 Script executed:
Repository: flanksource/flanksource-ui
Length of output: 599
🏁 Script executed:
Repository: flanksource/flanksource-ui
Length of output: 1472
🏁 Script executed:
Repository: flanksource/flanksource-ui
Length of output: 564
🏁 Script executed:
Repository: flanksource/flanksource-ui
Length of output: 10534
🏁 Script executed:
rg 'export const getViewIdByNamespaceAndName' -A 8 src/api/services/views.tsRepository: flanksource/flanksource-ui
Length of output: 381
Dashboard view may not appear in sidebar if
sidebarflag isfalse.findDashboardViewsearches theviewsarray returned bygetViewsForSidebar, which filters onsidebar=eq.true. If the configured dashboard view hassidebar=false, it won't be found here, so no dashboard nav item will be added to the sidebar — even thoughHomepageRedirectwill still redirect to it (since it queries all views viagetViewIdByNameorgetViewIdByNamespaceAndName, both without the sidebar filter).If the intent is that the dashboard view always appears in the sidebar nav regardless of its
sidebarflag, this needs a separate fetch or an adjusted query. If the expectation is that the dashboard view must havesidebar=true, this is fine but worth documenting.🤖 Prompt for AI Agents