"use client"; import React from "react"; import { Check, ChevronsUpDown, X } from "lucide-react"; import { isValidBranchParam } from "@/lib/frontend/params"; import { cn } from "@/lib/utils"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Skeleton } from "@/components/ui/skeleton"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; function normalizeTypedBranch(value) { if (typeof value !== "string") return null; const s = value.trim().toUpperCase(); return s ? s : null; } export default function SearchMultiBranchPicker({ branchesStatus, availableBranches, selectedBranches, onToggleBranch, isSubmitting, }) { const [open, setOpen] = React.useState(false); const [manual, setManual] = React.useState(""); const listReady = branchesStatus === "ready"; const listLoading = branchesStatus === "loading"; const listError = branchesStatus === "error"; const branchOptions = Array.isArray(availableBranches) ? availableBranches : []; const selected = Array.isArray(selectedBranches) ? selectedBranches : []; const selectedSet = React.useMemo( () => new Set(selected.map(String)), [selected] ); const typed = normalizeTypedBranch(manual); const canAddTyped = Boolean( typed && isValidBranchParam(typed) && !selectedSet.has(typed) ); return (

Niederlassungen auswählen

Wählen Sie eine oder mehrere Niederlassungen. Die Suche wird nach jeder Änderung aktualisiert.

{listLoading ? ( ) : ( Keine Treffer. {branchOptions.map((b) => { const id = String(b); const isSelected = selectedSet.has(id); return ( { const next = normalizeTypedBranch(value); if (!next || !isValidBranchParam(next)) return; if (!selectedSet.has(next)) onToggleBranch(next); setOpen(false); }} > {id} ); })} )} {listError ? (

Niederlassungen konnten nicht geladen werden. Sie können NLxx manuell hinzufügen.

setManual(e.target.value)} onKeyDown={(e) => { if (e.key !== "Enter") return; if (!canAddTyped) return; e.preventDefault(); onToggleBranch(typed); setManual(""); }} placeholder="z. B. NL17" disabled={isSubmitting} />
) : null}
{selected.length === 0 ? (

Keine Niederlassungen ausgewählt.

) : (
{selected.map((b) => ( {b} ))}
)}
); }