Skip to content

Commit

Permalink
peers table: add filtering on unassigned peers
Browse files Browse the repository at this point in the history
  • Loading branch information
EdouardVanbelle committed Jan 30, 2025
1 parent c8e3b50 commit b44ec8b
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 98 deletions.
81 changes: 70 additions & 11 deletions src/modules/groups/GroupSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ import { Group } from "@/interfaces/Group";

interface MultiSelectProps {
values: string[];
onChange: (items: string[]) => void;
exactValue?: string;
onChange: (items: string[], exactItem?: string) => void;
disabled?: boolean;
popoverWidth?: "auto" | number;
groups: Group[] | undefined;
unassignedCount?: number;
defaultGroupName?: string;
}
export function GroupSelector({
onChange,
values,
exactValue,
disabled = false,
popoverWidth = 400,
groups,
unassignedCount,
defaultGroupName = "All", //defined as a property, no clue if this value may change in the future
}: MultiSelectProps) {
const searchRef = React.useRef<HTMLInputElement>(null);
const [inputRef, { width }] = useElementSize<HTMLButtonElement>();
Expand All @@ -40,9 +46,20 @@ export function GroupSelector({
const toggle = (code: string) => {
const isSelected = values.find((c) => c == code) != undefined;
if (isSelected) {
onChange && onChange(values.filter((c) => c != code));
onChange && onChange(values.filter((c) => c != code), undefined);
} else {
onChange && onChange([...values, code]);
onChange && onChange([...values, code], undefined);
setSearch("");
}
};

const toggleExactGroup = (code: string) => {
const isSelected = exactValue == code;
if (isSelected) {
onChange && onChange([], undefined);
setSearch("");
} else {
onChange && onChange([], code);
setSearch("");
}
};
Expand All @@ -62,14 +79,16 @@ export function GroupSelector({
}}
>
<PopoverTrigger asChild={true}>
<Button variant={"secondary"} disabled={disabled} ref={inputRef}>
<Button variant={"secondary"} disabled={disabled} ref={inputRef} className="w-[200px] justify-between">
<FolderGit2 size={16} className={"shrink-0"} />
<div className={"w-full flex justify-between"}>
{values.length > 0 ? (
<div>{values.length} Group(s)</div>
) : (
"All Groups"
)}
{
exactValue != undefined
? ("Unassigned peers")
: values.length > 0
? (`${values.length} Group(s)`)
: ("All Groups")
}
<div className={"pl-2"}>
<ChevronsUpDown size={18} className={"shrink-0"} />
</div>
Expand Down Expand Up @@ -132,7 +151,6 @@ export function GroupSelector({
</div>
</div>
</div>

<ScrollArea
className={
"max-h-[380px] overflow-y-auto flex flex-col gap-1 pl-2 py-2 pr-3"
Expand All @@ -141,7 +159,48 @@ export function GroupSelector({
<CommandGroup>
<div className={""}>
<div className={"grid grid-cols-1 gap-1"}>
{orderBy(groups, "name")?.map((item) => {
<CommandItem
className={"p-1"}
onSelect={() => {
toggleExactGroup( defaultGroupName);
searchRef.current?.focus();
}}
onClick={(e) => e.preventDefault()}
>
<div
className={
"text-neutral-500 dark:text-nb-gray-300 font-medium flex items-center gap-3 py-1 px-1 w-full"
}
>
<Checkbox checked={exactValue == defaultGroupName}/>
<div
className={
"flex justify-between items-center w-full"
}
>
<div
className={
"flex items-center gap-2 whitespace-nowrap text-sm"
}
>
<FolderGit2 size={13} className={"shrink-0"} />
<TextWithTooltip text={"Unassigned peers"} />
</div>
<div
className={
"flex items-center gap-2 text-xs text-nb-gray-200/60"
}
>
<MonitorSmartphoneIcon size={13} />
{unassignedCount} Peer(s)
</div>
</div>
</div>
</CommandItem>
<hr />
{orderBy(groups, "name")
?.filter((group) => group.name != defaultGroupName) // Ignore default group
?.map((item) => {
const value = item?.name || "";
if (value === "") return null;
const isSelected =
Expand Down
Loading

0 comments on commit b44ec8b

Please sign in to comment.