Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,312 @@
import { KeyboardEvent, Ref, forwardRef, useImperativeHandle, useRef, ReactNode, Fragment, useEffect, useMemo, useState } from "react";
import { Listbox, Transition } from "@headlessui/react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router";
import { Colors } from "~/application/enums/shared/Colors";
import EntityIcon from "~/components/layouts/icons/EntityIcon";
import clsx from "clsx";
import HintTooltip from "../../tooltips/HintTooltip";
import ColorBadge from "../../badges/ColorBadge";
import { ChevronsUpDown, SearchIcon, PlusIcon, CheckIcon } from "~/components/ui/icons/vendor";
export interface RefInputSelectorDarkMode {
focus: () => void;
}
type GroupDto = { name: string; items: ItemDto[] };
type ItemDto = {
group?: string;
name: string | ReactNode;
value: string | number | undefined;
color?: Colors;
disabled?: boolean;
link?: string;
img?: ReactNode;
};
interface Props {
name?: string;
title?: string;
value?: string | number | undefined;
disabled?: boolean;
options: ItemDto[];
setValue?: React.Dispatch<React.SetStateAction<string | number | undefined>>;
className?: string;
withSearch?: boolean;
withLabel?: boolean;
withColors?: boolean;
selectPlaceholder?: string;
onNew?: () => void;
onNewRoute?: string;
required?: boolean;
help?: string;
hint?: ReactNode;
icon?: string;
borderless?: boolean;
autoFocus?: boolean;
readOnly?: boolean;
renderSelected?: (item: ItemDto) => ReactNode;
}
const InputSelectorDarkMode = (
{
name,
title,
value,
options,
disabled,
setValue,
className,
withSearch = true,
withLabel = true,
withColors = false,
selectPlaceholder,
onNew,
required,
onNewRoute,
help,
hint,
icon,
borderless,
autoFocus,
readOnly,
renderSelected,
}: Props,
ref: Ref<RefInputSelectorDarkMode>
) => {
const { t } = useTranslation();
const button = useRef<HTMLButtonElement>(null);
const inputSearch = useRef<HTMLInputElement>(null);
const [selected, setSelected] = useState<{ name: string | ReactNode; value: string | number | undefined; color?: Colors }>();
const [searchInput, setSearchInput] = useState("");
const normalizedSearch = searchInput.trim().toUpperCase();
const filteredItems = useMemo(() => {
if (!normalizedSearch) {
return options;
}
return options.filter((item) => {
const name = item.name?.toString().toUpperCase() ?? "";
const optionValue = item.value?.toString().toUpperCase() ?? "";
return name.includes(normalizedSearch) || optionValue.includes(normalizedSearch);
});
}, [normalizedSearch, options]);
const itemsByGroup = useMemo(() => {
const groups = new Map<string, GroupDto>();
for (const item of filteredItems) {
const groupName = item.group ?? "";
const group = groups.get(groupName);
if (group) {
group.items.push(item);
} else {
groups.set(groupName, { name: groupName, items: [item] });
}
}
return Array.from(groups.values());
}, [filteredItems]);
useEffect(() => {
const selected = options.find((f) => f.value === value);
setSelected(selected);
}, [options, value]);
useEffect(() => {
if (selected && setValue && value !== selected?.value) {
setValue(selected?.value);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selected]);
useImperativeHandle(ref, () => ({ focus }));
function focus() {
setTimeout(() => {
button.current?.focus();
button.current?.click();
}, 1);
}
function trySelect(event: KeyboardEvent) {
if (event.code === "Enter") {
if (filteredItems.length > 0) {
setSelected(filteredItems[0]);
}
}
}
return (
<Listbox value={selected} onChange={setSelected} disabled={disabled || readOnly}>
{({ open }) => (
<div className={clsx(className, "text-slate-50")}>
{withLabel && title && (
<Listbox.Label htmlFor={name} className="text-muted-foreground mb-1 flex justify-between space-x-2 text-xs font-medium">
<div className="flex items-center space-x-1">
<div className="truncate">
{title}
{required && <span className="ml-1 text-red-600">*</span>}
</div>
{help && <HintTooltip text={help} />}
</div>
{hint}
</Listbox.Label>
)}
<div className="relative">
<Listbox.Button
autoFocus={autoFocus}
type="button"
ref={button}
className={clsx(
"focus:border-border focus:ring-accent-700 relative w-full cursor-default rounded-sm border border-slate-800 bg-slate-800 py-2 pr-10 pl-3 text-left text-slate-50 shadow-2xs hover:bg-slate-700 focus:ring-1 focus:outline-hidden sm:text-sm",
disabled || readOnly ? "cursor-not-allowed bg-slate-700" : "bg-slate-800 hover:bg-slate-700 focus:bg-slate-900",
borderless && "border-transparent"
)}
>
<input type="hidden" name={name} value={selected?.value ?? ""} hidden readOnly />
{icon && (
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<EntityIcon className="h-5 w-5 text-slate-400" icon={icon} />
</div>
)}
<span className="inline-flex w-full items-center space-x-2 truncate">
{withColors && selected && <ColorBadge color={selected?.color ?? Colors.UNDEFINED} />}
<div className="truncate">
{selected ? (
<span>{renderSelected ? renderSelected(selected) : selected.name}</span>
) : (
<span className="text-sm text-slate-500">{selectPlaceholder ?? t("shared.select")}...</span>
)}
</div>
</span>
<span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronsUpDown className="h-5 w-5 text-slate-400" />
</span>
</Listbox.Button>
<Transition show={open} as={Fragment} leave="transition ease-in duration-100" leaveFrom="opacity-100" leaveTo="opacity-0">
<Listbox.Options
// onFocus={() => inputSearch.current?.focus()}
onBlur={() => setSearchInput("")}
className={clsx(
"ring-opacity-5 absolute z-10 mt-1 max-h-72 w-full overflow-auto rounded-sm bg-slate-800 py-1 text-base text-white shadow-lg ring-1 ring-black focus:outline-hidden sm:text-sm"
)}
>
{(withSearch || onNew || onNewRoute) && (
<div className="flex rounded-sm p-2">
<div className="relative flex grow items-stretch focus-within:z-10">
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<SearchIcon className="h-5 w-5 text-slate-400" />
</div>
<input
ref={inputSearch}
id="search"
autoComplete="off"
onKeyDown={trySelect}
placeholder={t("shared.searchDot")}
value={searchInput}
onChange={(e) => setSearchInput(e.target.value)}
className="focus:border-accent-300 bg-background border-border block w-full rounded-none rounded-l-md border px-3 py-2 pl-10 text-sm focus:ring-slate-300 focus:outline-hidden sm:text-sm"
/>
</div>
{onNew && (
<button
title={t("shared.new")}
type="button"
onClick={onNew}
className="focus:border-border focus:ring-ring bg-secondary border-border relative -ml-px inline-flex items-center space-x-2 rounded-r-md border px-2 py-2 text-sm font-medium text-slate-700 hover:bg-slate-100 focus:ring-1 focus:outline-hidden"
>
<PlusIcon className="h-4 w-4" />
</button>
)}
{onNewRoute && (
<Link
to={onNewRoute}
className="focus:border-border focus:ring-ring bg-secondary border-border relative -ml-px inline-flex items-center space-x-2 rounded-r-md border px-2 py-2 text-sm font-medium text-slate-700 hover:bg-slate-100 focus:ring-1 focus:outline-hidden"
>
<PlusIcon className="h-4 w-4" />
</Link>
)}
</div>
)}
{itemsByGroup.map((group, idx) => (
<Fragment key={idx}>
{group.name && (
<Listbox.Option
disabled
className={({ active }) =>
clsx("relative cursor-default py-1 pr-9 pl-3 select-none", active ? "bg-accent-500 text-black" : "text-slate-50")
}
value={group}
>
{({ selected, active }) => (
<Fragment>
<span className={clsx("text-muted-foreground text-xs font-medium", selected ? "font-semibold" : "font-normal")}>{group.name}</span>
</Fragment>
)}
</Listbox.Option>
)}
{group.items.map((item, idx) => (
<Listbox.Option
key={idx}
disabled={item.disabled}
className={({ active }) =>
clsx(
"relative cursor-default py-2 pr-9 pl-3 text-gray-200 select-none hover:text-white",
!item.disabled && active && "bg-accent-500",
!item.disabled && !active && "",
item.disabled && "cursor-not-allowed"
)
}
value={item}
>
{({ selected, active }) => (
<Fragment>
{item.link ? (
<Link to={item.link}>
<Item item={item} selected={selected} active={active} withColors={withColors} />
</Link>
) : (
<Item item={item} selected={selected} active={active} withColors={withColors} />
)}
</Fragment>
)}
</Listbox.Option>
))}
</Fragment>
))}
{withSearch && filteredItems.length === 0 && <div className="px-3 py-2 text-sm text-slate-400">{t("shared.noRecords")}</div>}
</Listbox.Options>
</Transition>
</div>
</div>
)}
</Listbox>
);
};
function Item({ item, selected, active, withColors = true }: { item: ItemDto; selected: boolean; active: boolean; withColors?: boolean }) {
return (
<Fragment>
<div className="flex items-center space-x-2 truncate pr-3">
{item.img && item.img}
<div className="flex items-center space-x-2 truncate">
{withColors && item.color !== undefined && <ColorBadge color={item.color} />}
<div className={clsx(selected ? "font-medium" : "font-normal", "truncate")}>{item.name}</div>
</div>
</div>
{selected ? (
<span className={clsx(active ? "text-white" : "text-accent-400", "absolute inset-y-0 right-0 flex items-center pr-4")}>
<CheckIcon className="h-4 w-4" />
</span>
) : null}
</Fragment>
);
}
export default forwardRef(InputSelectorDarkMode);