"use client"; import React from "react"; import { X } from "lucide-react"; import { isValidBranchParam } from "@/lib/frontend/params"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { DebouncedSkeleton } from "@/components/ui/debounced-skeleton"; 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, onClearAllBranches, isSubmitting, }) { 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 canClearAll = typeof onClearAllBranches === "function" && selected.length > 0 && !isSubmitting; // Fail-open manual add (only relevant if branch list failed) const [manual, setManual] = React.useState(""); 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 ? (
{Array.from({ length: 15 }).map((_, i) => (
))}
) : null} {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} />
{selected.length === 0 ? (

Keine Niederlassungen ausgewählt.

) : (
{selected.map((b) => ( {b} ))}
)}
) : null} {listReady ? (
Ausgewählt: {selected.length}
{/* RHL-038 UI polish: - Use shadcn Checkbox + a “selectable card” label wrapper - Checked state highlights the whole tile (border + background) - Keep it compact so it still works with many branches */}
{branchOptions.map((b) => { const id = String(b); const checked = selectedSet.has(id); // Stable id for accessibility (Label <-> Checkbox association). const checkboxId = `branch-${id}`; return ( ); })}
) : null}
); }