-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Add search bar in AdminCP #729
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
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
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
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,2 @@ | ||
| export const normalizeUrl = (url: string): string => | ||
| url.endsWith("/") && url.length > 1 ? url.slice(0, -1) : url; |
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,3 @@ | ||
| export const MAX_SEARCH_RESULTS = 10; | ||
| export const MIN_USERS_QUERY_LENGTH = 3; | ||
| export const USERS_DEBOUNCE_MS = 400; |
164 changes: 164 additions & 0 deletions
164
packages/vitnode/src/views/admin/layouts/search/flatten-nav.test.ts
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,164 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import type { NavAdminParent } from "../sidebar/nav/get-admin-nav"; | ||
|
|
||
| import { | ||
| buildSearchText, | ||
| flattenAdminNav, | ||
| matchesAdminNavItem, | ||
| } from "./flatten-nav"; | ||
|
|
||
| const nav: NavAdminParent[] = [ | ||
| { | ||
| id: "core", | ||
| title: "Core", | ||
| items: [ | ||
| { href: "/admin/core/", title: "Dashboard" }, | ||
| { | ||
| href: "/admin/core/system", | ||
| title: "System", | ||
| items: [ | ||
| { href: "/admin/core/system/integrations", title: "Integrations" }, | ||
| { href: "/admin/core/system/files", title: "Files" }, | ||
| ], | ||
| }, | ||
| { | ||
| href: "/admin/core/users", | ||
| title: "Users", | ||
| items: [ | ||
| { href: "/admin/core/users", title: "User List" }, | ||
| { href: "/admin/core/users/roles", title: "Roles" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| id: "@vitnode/blog", | ||
| title: "Blog", | ||
| items: [{ href: "/admin/blog/posts", title: "Posts" }], | ||
| }, | ||
| ]; | ||
|
|
||
| describe("flattenAdminNav", () => { | ||
| it("emits only leaves, never a parent that has children", () => { | ||
| const hrefs = flattenAdminNav(nav).map(item => item.href); | ||
|
|
||
| // `/admin/core/system` has no page of its own - only its children do. | ||
| expect(hrefs).not.toContain("/admin/core/system"); | ||
| expect(hrefs).toStrictEqual([ | ||
| "/admin/core/", | ||
| "/admin/core/system/integrations", | ||
| "/admin/core/system/files", | ||
| "/admin/core/users", | ||
| "/admin/core/users/roles", | ||
| "/admin/blog/posts", | ||
| ]); | ||
| }); | ||
|
|
||
| it("keeps a sub-item whose href repeats its parent's exactly once", () => { | ||
| const users = flattenAdminNav(nav).filter( | ||
| item => item.href === "/admin/core/users", | ||
| ); | ||
|
|
||
| expect(users).toHaveLength(1); | ||
| // The child wins, so the row reads "Users › User List". | ||
| expect(users[0].title).toBe("User List"); | ||
| expect(users[0].parentTitle).toBe("Users"); | ||
| }); | ||
|
|
||
| it("dedupes on the normalized href, so a trailing slash is not a new row", () => { | ||
| const items = flattenAdminNav([ | ||
| { | ||
| id: "core", | ||
| title: "Core", | ||
| items: [ | ||
| { href: "/admin/core/thing", title: "First" }, | ||
| { href: "/admin/core/thing/", title: "Second" }, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| expect(items).toHaveLength(1); | ||
| expect(items[0].title).toBe("First"); | ||
| }); | ||
|
|
||
| it("carries the group title and inherits the parent icon", () => { | ||
| const icon = "icon-node"; | ||
| const [item] = flattenAdminNav([ | ||
| { | ||
| id: "core", | ||
| title: "Core", | ||
| items: [ | ||
| { | ||
| href: "/admin/core/users", | ||
| icon, | ||
| title: "Users", | ||
| items: [{ href: "/admin/core/users/roles", title: "Roles" }], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| expect(item.groupTitle).toBe("Core"); | ||
| expect(item.icon).toBe(icon); | ||
| }); | ||
|
|
||
| it("builds searchText from title, parent and group, lower-cased", () => { | ||
| const roles = flattenAdminNav(nav).find( | ||
| item => item.href === "/admin/core/users/roles", | ||
| ); | ||
|
|
||
| expect(roles?.searchText).toBe("roles users core"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildSearchText", () => { | ||
| it("drops empty parts and duplicates", () => { | ||
| expect(buildSearchText(["Roles", undefined, "", "roles", "Core"])).toBe( | ||
| "roles core", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("matchesAdminNavItem", () => { | ||
| const [item] = flattenAdminNav(nav).filter( | ||
| entry => entry.href === "/admin/core/users", | ||
| ); | ||
|
|
||
| it("matches every item on an empty query", () => { | ||
| expect(matchesAdminNavItem(item, "")).toBe(true); | ||
| expect(matchesAdminNavItem(item, " ")).toBe(true); | ||
| }); | ||
|
|
||
| it("matches on a partial, case-insensitive fragment", () => { | ||
| expect(matchesAdminNavItem(item, "LIS")).toBe(true); | ||
| }); | ||
|
|
||
| it("matches on the parent or group title", () => { | ||
| expect(matchesAdminNavItem(item, "users")).toBe(true); | ||
| expect(matchesAdminNavItem(item, "core")).toBe(true); | ||
| }); | ||
|
|
||
| it("requires every token, in any order", () => { | ||
| expect(matchesAdminNavItem(item, "user list")).toBe(true); | ||
| expect(matchesAdminNavItem(item, "list user")).toBe(true); | ||
| expect(matchesAdminNavItem(item, "list nope")).toBe(false); | ||
| }); | ||
|
|
||
| it("does not match across a token boundary", () => { | ||
| expect(matchesAdminNavItem(item, "userlist")).toBe(false); | ||
| }); | ||
|
|
||
| it("finds a row by another locale's title once searchText carries it", () => { | ||
| // What `getSearchNavItems` produces for a Polish request: the row displays | ||
| // "Role" but is still findable by the English "Roles". | ||
| const polish = { | ||
| ...item, | ||
| searchText: buildSearchText(["Role", "Użytkownicy", "Roles", "Users"]), | ||
| title: "Role", | ||
| }; | ||
|
|
||
| expect(matchesAdminNavItem(polish, "roles")).toBe(true); | ||
| expect(matchesAdminNavItem(polish, "użytkownicy")).toBe(true); | ||
| }); | ||
| }); |
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.