-
-
Notifications
You must be signed in to change notification settings - Fork 170
Add additional Machines list filters #507
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
siemenvdn
wants to merge
2
commits into
tale:main
Choose a base branch
from
siemenvdn:feature/extend-machine-filtering
base: main
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.
+271
−21
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,152 @@ | ||
| import { ChevronDown, X } from "lucide-react"; | ||
| import type { JSX } from "react"; | ||
|
|
||
| import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from "~/components/menu"; | ||
| import type { User } from "~/types/User"; | ||
| import cn from "~/utils/cn"; | ||
| import type { PopulatedNode } from "~/utils/node-info"; | ||
| import { getUserDisplayName } from "~/utils/user"; | ||
|
|
||
| import { useMachineFilterParams } from "../hooks/use-machine-filter-params"; | ||
|
|
||
| const STATUS_OPTIONS = [ | ||
| { value: "online", label: "Online" }, | ||
| { value: "offline", label: "Offline" }, | ||
| { value: "expired", label: "Expired" }, | ||
| ] as const; | ||
|
|
||
| const ROUTE_OPTIONS = [ | ||
| { value: "exit-node", label: "Exit node" }, | ||
| { value: "subnet", label: "Subnet router" }, | ||
| ] as const; | ||
|
|
||
| function FilterDropdown({ | ||
| label, | ||
| value, | ||
| options, | ||
| onChange, | ||
| }: { | ||
| label: string; | ||
| value: string | null; | ||
| options: readonly { value: string; label: string }[]; | ||
| onChange: (value: string | null) => void; | ||
| }): JSX.Element { | ||
| const activeOption = options.find((o) => o.value === value) ?? null; | ||
| const isActive = activeOption !== null; | ||
|
|
||
| return ( | ||
| <Menu> | ||
| <MenuTrigger | ||
| className={cn( | ||
| "px-3 py-1.5 rounded-full text-sm font-medium", | ||
| "border transition-colors", | ||
| "flex items-center gap-1.5", | ||
| isActive | ||
| ? "border-indigo-300 bg-indigo-50 text-indigo-700 dark:border-indigo-700 dark:bg-indigo-950/50 dark:text-indigo-300" | ||
| : "border-mist-200 dark:border-mist-700 text-mist-700 dark:text-mist-300 hover:border-mist-300 dark:hover:border-mist-600", | ||
| )} | ||
| > | ||
| {activeOption?.label ?? label} | ||
| <ChevronDown className="h-3.5 w-3.5" /> | ||
| </MenuTrigger> | ||
| <MenuContent> | ||
| {options.map((option) => ( | ||
| <MenuItem | ||
| key={option.value} | ||
| onClick={() => onChange(value === option.value ? null : option.value)} | ||
| > | ||
| {option.value === value ? ( | ||
| <span className="font-medium text-indigo-600 dark:text-indigo-400"> | ||
| {option.label} | ||
| </span> | ||
| ) : ( | ||
| option.label | ||
| )} | ||
| </MenuItem> | ||
| ))} | ||
| {isActive && ( | ||
| <> | ||
| <MenuSeparator /> | ||
| <MenuItem onClick={() => onChange(null)}>Clear filter</MenuItem> | ||
| </> | ||
| )} | ||
| </MenuContent> | ||
| </Menu> | ||
| ); | ||
| } | ||
|
|
||
| interface MachineFiltersProps { | ||
| users: User[]; | ||
| populatedNodes: PopulatedNode[]; | ||
| } | ||
|
|
||
| export function MachineFilters({ users, populatedNodes }: MachineFiltersProps): JSX.Element { | ||
| const { | ||
| filterUser, | ||
| filterTag, | ||
| filterStatus, | ||
| filterRoute, | ||
| hasActiveFilters, | ||
| setParam, | ||
| clearFilters, | ||
| } = useMachineFilterParams(); | ||
|
|
||
| const tagOwnedExists = populatedNodes.some((n) => !n.user); | ||
| const userOptions = [ | ||
| ...(tagOwnedExists ? [{ value: "tag-owned", label: "Tag-owned" }] : []), | ||
| ...users.map((u) => ({ value: u.id, label: getUserDisplayName(u) })), | ||
| ]; | ||
|
|
||
| const tagOptions = Array.from(new Set(populatedNodes.flatMap((n) => n.tags))) | ||
| .filter(Boolean) | ||
| .sort() | ||
| .map((tag) => ({ value: tag, label: tag })); | ||
|
|
||
| return ( | ||
| <> | ||
| {userOptions.length > 0 && ( | ||
| <FilterDropdown | ||
| label="User" | ||
| onChange={(v) => setParam("user", v)} | ||
| options={userOptions} | ||
| value={filterUser} | ||
| /> | ||
| )} | ||
| {tagOptions.length > 0 && ( | ||
| <FilterDropdown | ||
| label="Tag" | ||
| onChange={(v) => setParam("tag", v)} | ||
| options={tagOptions} | ||
| value={filterTag} | ||
| /> | ||
| )} | ||
| <FilterDropdown | ||
| label="Status" | ||
| onChange={(v) => setParam("status", v)} | ||
| options={STATUS_OPTIONS} | ||
| value={filterStatus} | ||
| /> | ||
| <FilterDropdown | ||
| label="Route" | ||
| onChange={(v) => setParam("route", v)} | ||
| options={ROUTE_OPTIONS} | ||
| value={filterRoute} | ||
| /> | ||
| {hasActiveFilters && ( | ||
| <button | ||
| className={cn( | ||
| "flex items-center gap-1 px-3 py-1.5 rounded-full text-sm font-medium", | ||
| "border border-mist-200 dark:border-mist-700", | ||
| "text-mist-600 dark:text-mist-400", | ||
| "hover:border-mist-300 dark:hover:border-mist-600", | ||
| )} | ||
| onClick={clearFilters} | ||
| type="button" | ||
| > | ||
| Clear filters | ||
| <X className="h-3.5 w-3.5" /> | ||
| </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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useSearchParams } from "react-router"; | ||
|
|
||
| export interface MachineFilterParams { | ||
| filterUser: string | null; | ||
| filterTag: string | null; | ||
| filterStatus: "online" | "offline" | "expired" | null; | ||
| filterRoute: "exit-node" | "subnet" | null; | ||
| hasActiveFilters: boolean; | ||
| setParam: (key: string, value: string | null) => void; | ||
| clearFilters: () => void; | ||
| } | ||
|
|
||
| export function useMachineFilterParams(): MachineFilterParams { | ||
| const [searchParams, setSearchParams] = useSearchParams(); | ||
|
|
||
| const filterUser = searchParams.get("user"); | ||
| const filterTag = searchParams.get("tag"); | ||
| const filterStatus = searchParams.get("status") as MachineFilterParams["filterStatus"]; | ||
| const filterRoute = searchParams.get("route") as MachineFilterParams["filterRoute"]; | ||
|
|
||
| const hasActiveFilters = | ||
| filterUser !== null || filterTag !== null || filterStatus !== null || filterRoute !== null; | ||
|
|
||
| const setParam = (key: string, value: string | null) => { | ||
| setSearchParams((prev) => { | ||
| const next = new URLSearchParams(prev); | ||
| if (value === null) next.delete(key); | ||
| else next.set(key, value); | ||
| return next; | ||
| }); | ||
| }; | ||
|
|
||
| const clearFilters = () => { | ||
| setSearchParams((prev) => { | ||
| const next = new URLSearchParams(); | ||
| const q = prev.get("q"); | ||
| if (q) next.set("q", q); | ||
| return next; | ||
| }); | ||
| }; | ||
|
|
||
| return { | ||
| filterUser, | ||
| filterTag, | ||
| filterStatus, | ||
| filterRoute, | ||
| hasActiveFilters, | ||
| setParam, | ||
| clearFilters, | ||
| }; | ||
| } |
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.
I see what you're doing here, but I think it's slightly unexpected behavior where the tooltip/user select dropdown shows names, but the URL shows IDs. They should both be the usernames.