mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-22 16:55:44 +00:00
♻️ refactor
This commit is contained in:
@@ -1063,7 +1063,7 @@ export function InternalResourceForm({
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
enableAutocomplete={true}
|
enableAutocomplete
|
||||||
autocompleteOptions={
|
autocompleteOptions={
|
||||||
allRoles
|
allRoles
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,9 @@ import type { ListClientsResponse } from "@server/routers/client";
|
|||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { useDebounce } from "use-debounce";
|
import { useDebounce } from "use-debounce";
|
||||||
import {
|
|
||||||
Command,
|
|
||||||
CommandEmpty,
|
|
||||||
CommandGroup,
|
|
||||||
CommandInput,
|
|
||||||
CommandItem,
|
|
||||||
CommandList
|
|
||||||
} from "./ui/command";
|
|
||||||
import { cn } from "@app/lib/cn";
|
|
||||||
|
|
||||||
import { CheckIcon } from "lucide-react";
|
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
|
import { MultiSelectTags } from "./multi-select-tags";
|
||||||
|
|
||||||
export type SelectedMachine = Pick<
|
export type SelectedMachine = Pick<
|
||||||
ListClientsResponse["clients"][number],
|
ListClientsResponse["clients"][number],
|
||||||
@@ -57,52 +48,71 @@ export function MachinesSelector({
|
|||||||
return allMachines;
|
return allMachines;
|
||||||
}, [machines, selectedMachines, debouncedValue]);
|
}, [machines, selectedMachines, debouncedValue]);
|
||||||
|
|
||||||
const selectedMachinesIds = new Set(
|
// const selectedMachinesIds = new Set(
|
||||||
selectedMachines.map((m) => m.clientId)
|
// selectedMachines.map((m) => m.clientId)
|
||||||
);
|
// );
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Command shouldFilter={false}>
|
<MultiSelectTags
|
||||||
<CommandInput
|
emptyPlaceholder={t("machineNotFound")}
|
||||||
placeholder={t("machineSearch")}
|
searchPlaceholder={t("machineSearch")}
|
||||||
value={machineSearchQuery}
|
value={selectedMachines.map((m) => ({
|
||||||
onValueChange={setMachineSearchQuery}
|
...m,
|
||||||
/>
|
text: m.name,
|
||||||
<CommandList>
|
id: m.clientId.toString()
|
||||||
<CommandEmpty>{t("machineNotFound")}</CommandEmpty>
|
}))}
|
||||||
<CommandGroup>
|
onChange={(values) => {
|
||||||
{machinesShown.map((m) => (
|
onSelectMachines(values);
|
||||||
<CommandItem
|
|
||||||
value={`${m.name}:${m.clientId}`}
|
|
||||||
key={m.clientId}
|
|
||||||
onSelect={() => {
|
|
||||||
let newMachineClients = [];
|
|
||||||
if (selectedMachinesIds.has(m.clientId)) {
|
|
||||||
newMachineClients = selectedMachines.filter(
|
|
||||||
(mc) => mc.clientId !== m.clientId
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
newMachineClients = [
|
|
||||||
...selectedMachines,
|
|
||||||
m
|
|
||||||
];
|
|
||||||
}
|
|
||||||
onSelectMachines(newMachineClients);
|
|
||||||
}}
|
}}
|
||||||
>
|
options={machinesShown.map((m) => ({
|
||||||
<CheckIcon
|
...m,
|
||||||
className={cn(
|
id: m.clientId.toString(),
|
||||||
"mr-2 h-4 w-4",
|
text: m.name
|
||||||
selectedMachinesIds.has(m.clientId)
|
}))}
|
||||||
? "opacity-100"
|
onSearch={setMachineSearchQuery}
|
||||||
: "opacity-0"
|
searchQuery={machineSearchQuery}
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
{`${m.name}`}
|
// <Command shouldFilter={false}>
|
||||||
</CommandItem>
|
// <CommandInput
|
||||||
))}
|
// placeholder={t("machineSearch")}
|
||||||
</CommandGroup>
|
// value={machineSearchQuery}
|
||||||
</CommandList>
|
// onValueChange={setMachineSearchQuery}
|
||||||
</Command>
|
// />
|
||||||
|
// <CommandList>
|
||||||
|
// <CommandEmpty>{t("machineNotFound")}</CommandEmpty>
|
||||||
|
// <CommandGroup>
|
||||||
|
// {machinesShown.map((m) => (
|
||||||
|
// <CommandItem
|
||||||
|
// value={`${m.name}:${m.clientId}`}
|
||||||
|
// key={m.clientId}
|
||||||
|
// onSelect={() => {
|
||||||
|
// let newMachineClients = [];
|
||||||
|
// if (selectedMachinesIds.has(m.clientId)) {
|
||||||
|
// newMachineClients = selectedMachines.filter(
|
||||||
|
// (mc) => mc.clientId !== m.clientId
|
||||||
|
// );
|
||||||
|
// } else {
|
||||||
|
// newMachineClients = [
|
||||||
|
// ...selectedMachines,
|
||||||
|
// m
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
// onSelectMachines(newMachineClients);
|
||||||
|
// }}
|
||||||
|
// >
|
||||||
|
// <CheckIcon
|
||||||
|
// className={cn(
|
||||||
|
// "mr-2 h-4 w-4",
|
||||||
|
// selectedMachinesIds.has(m.clientId)
|
||||||
|
// ? "opacity-100"
|
||||||
|
// : "opacity-0"
|
||||||
|
// )}
|
||||||
|
// />
|
||||||
|
// {`${m.name}`}
|
||||||
|
// </CommandItem>
|
||||||
|
// ))}
|
||||||
|
// </CommandGroup>
|
||||||
|
// </CommandList>
|
||||||
|
// </Command>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
77
src/components/multi-select-tags.tsx
Normal file
77
src/components/multi-select-tags.tsx
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import type { Ref } from "react";
|
||||||
|
import {
|
||||||
|
Command,
|
||||||
|
CommandEmpty,
|
||||||
|
CommandGroup,
|
||||||
|
CommandInput,
|
||||||
|
CommandItem,
|
||||||
|
CommandList
|
||||||
|
} from "./ui/command";
|
||||||
|
import { cn } from "@app/lib/cn";
|
||||||
|
import { CheckIcon } from "lucide-react";
|
||||||
|
|
||||||
|
export type TagValue = { text: string; id: string };
|
||||||
|
|
||||||
|
export type MultiSelectTagsProps<T extends TagValue> = {
|
||||||
|
emptyPlaceholder: string;
|
||||||
|
searchPlaceholder: string;
|
||||||
|
searchQuery?: string;
|
||||||
|
options: Array<T>;
|
||||||
|
value: Array<T>;
|
||||||
|
onChange: (newValue: Array<T>) => void;
|
||||||
|
onSearch: (query: string) => void;
|
||||||
|
ref?: Ref<HTMLButtonElement>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function MultiSelectTags<T extends TagValue>({
|
||||||
|
emptyPlaceholder,
|
||||||
|
searchPlaceholder,
|
||||||
|
searchQuery,
|
||||||
|
value,
|
||||||
|
options,
|
||||||
|
onSearch,
|
||||||
|
onChange
|
||||||
|
}: MultiSelectTagsProps<T>) {
|
||||||
|
const selectedValues = new Set(value.map((v) => v.id));
|
||||||
|
return (
|
||||||
|
<Command shouldFilter={false}>
|
||||||
|
<CommandInput
|
||||||
|
placeholder={searchPlaceholder}
|
||||||
|
value={searchQuery}
|
||||||
|
onValueChange={onSearch}
|
||||||
|
/>
|
||||||
|
<CommandList>
|
||||||
|
<CommandEmpty>{emptyPlaceholder}</CommandEmpty>
|
||||||
|
<CommandGroup>
|
||||||
|
{options.map((option) => (
|
||||||
|
<CommandItem
|
||||||
|
value={option.id}
|
||||||
|
key={option.id}
|
||||||
|
onSelect={() => {
|
||||||
|
let newValues = [];
|
||||||
|
if (selectedValues.has(option.id)) {
|
||||||
|
newValues = value.filter(
|
||||||
|
(v) => v.id !== option.id
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
newValues = [...value, option];
|
||||||
|
}
|
||||||
|
onChange(newValues);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<CheckIcon
|
||||||
|
className={cn(
|
||||||
|
"mr-2 h-4 w-4",
|
||||||
|
selectedValues.has(option.id)
|
||||||
|
? "opacity-100"
|
||||||
|
: "opacity-0"
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
{`${option.text}`}
|
||||||
|
</CommandItem>
|
||||||
|
))}
|
||||||
|
</CommandGroup>
|
||||||
|
</CommandList>
|
||||||
|
</Command>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -522,7 +522,7 @@ export function TagInput({ ref, ...props }: TagInputProps) {
|
|||||||
onBlur={handleInputBlur}
|
onBlur={handleInputBlur}
|
||||||
{...inputProps}
|
{...inputProps}
|
||||||
className={cn(
|
className={cn(
|
||||||
"border-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
"border-0 px-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
||||||
// className,
|
// className,
|
||||||
styleClasses?.input
|
styleClasses?.input
|
||||||
)}
|
)}
|
||||||
@@ -692,7 +692,7 @@ export function TagInput({ ref, ...props }: TagInputProps) {
|
|||||||
onBlur={handleInputBlur}
|
onBlur={handleInputBlur}
|
||||||
{...inputProps}
|
{...inputProps}
|
||||||
className={cn(
|
className={cn(
|
||||||
"border-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
"border-0 px-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
||||||
// className,
|
// className,
|
||||||
styleClasses?.input
|
styleClasses?.input
|
||||||
)}
|
)}
|
||||||
@@ -770,7 +770,7 @@ export function TagInput({ ref, ...props }: TagInputProps) {
|
|||||||
onBlur={handleInputBlur}
|
onBlur={handleInputBlur}
|
||||||
{...inputProps}
|
{...inputProps}
|
||||||
className={cn(
|
className={cn(
|
||||||
"border-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
"border-0 px-0 h-5 bg-transparent focus-visible:ring-0 focus-visible:ring-transparent focus-visible:ring-offset-0 flex-1 w-fit shadow-none inset-shadow-none",
|
||||||
// className,
|
// className,
|
||||||
styleClasses?.input
|
styleClasses?.input
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user