SearchQueryBox.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use client";
  2. import React from "react";
  3. import { Clock3, Search } from "lucide-react";
  4. import { Button } from "@/components/ui/button";
  5. import { Input } from "@/components/ui/input";
  6. import { Label } from "@/components/ui/label";
  7. import {
  8. Popover,
  9. PopoverContent,
  10. PopoverTrigger,
  11. } from "@/components/ui/popover";
  12. import {
  13. Command,
  14. CommandGroup,
  15. CommandItem,
  16. CommandList,
  17. } from "@/components/ui/command";
  18. import {
  19. Tooltip,
  20. TooltipContent,
  21. TooltipTrigger,
  22. } from "@/components/ui/tooltip";
  23. function toBranchShort(value) {
  24. const normalized = String(value || "").trim().toUpperCase();
  25. const match = /^NL(\d+)$/i.exec(normalized);
  26. if (!match) return null;
  27. const digits = match[1];
  28. return digits.length === 1 ? `0${digits}` : digits;
  29. }
  30. function getMultiBranchSummary(branches) {
  31. const compact = (Array.isArray(branches) ? branches : [])
  32. .map((branch) => toBranchShort(branch))
  33. .filter(Boolean);
  34. if (compact.length === 0) return null;
  35. return `NL: ${compact.join(",")}`;
  36. }
  37. function getScopeSummary(entry) {
  38. if (entry?.scope === "all") return "Alle Niederlassungen";
  39. if (entry?.scope === "multi") {
  40. const compactBranches = getMultiBranchSummary(entry?.branches);
  41. if (compactBranches) return compactBranches;
  42. const count = Array.isArray(entry?.branches) ? entry.branches.length : 0;
  43. return count > 0
  44. ? `Mehrere Niederlassungen (${count})`
  45. : "Mehrere Niederlassungen";
  46. }
  47. return `Niederlassung ${entry?.routeBranch || ""}`.trim();
  48. }
  49. function getDateSummary(entry) {
  50. const from = typeof entry?.from === "string" ? entry.from.trim() : "";
  51. const to = typeof entry?.to === "string" ? entry.to.trim() : "";
  52. if (from && to) return `${from} bis ${to}`;
  53. if (from) return `ab ${from}`;
  54. if (to) return `bis ${to}`;
  55. return "";
  56. }
  57. function getEntryMetaSummary(entry) {
  58. const parts = [getScopeSummary(entry)];
  59. const dateSummary = getDateSummary(entry);
  60. if (dateSummary) parts.push(dateSummary);
  61. return parts.join(" • ");
  62. }
  63. export default function SearchQueryBox({
  64. qDraft,
  65. onQDraftChange,
  66. onSubmit,
  67. currentQuery,
  68. isSubmitting,
  69. canSearch,
  70. recentSearches,
  71. onSelectRecentSearch,
  72. onClearRecentSearches,
  73. }) {
  74. const [open, setOpen] = React.useState(false);
  75. const historyItems = Array.isArray(recentSearches) ? recentSearches : [];
  76. const hasHistory = historyItems.length > 0;
  77. const canClearHistory =
  78. hasHistory && typeof onClearRecentSearches === "function";
  79. React.useEffect(() => {
  80. if (hasHistory) return;
  81. setOpen(false);
  82. }, [hasHistory]);
  83. React.useEffect(() => {
  84. if (!isSubmitting) return;
  85. setOpen(false);
  86. }, [isSubmitting]);
  87. return (
  88. // Important for flex layouts:
  89. // - w-full ensures the box fills its container width.
  90. // - min-w-0 allows the input row to shrink without overflow.
  91. <div className="grid w-full min-w-0 gap-2">
  92. <Label htmlFor="q">Suchbegriff</Label>
  93. <Popover open={open} onOpenChange={setOpen}>
  94. <div className="flex w-full min-w-0 flex-col gap-2 sm:flex-row sm:items-center">
  95. <Input
  96. id="q"
  97. name="q"
  98. value={qDraft}
  99. onChange={(e) => onQDraftChange(e.target.value)}
  100. placeholder="z. B. Bridgestone, Rechnung, Kundennummer…"
  101. disabled={isSubmitting}
  102. // Make the input take the remaining space next to the buttons.
  103. className="flex-1 min-w-0"
  104. />
  105. <Tooltip>
  106. <PopoverTrigger asChild>
  107. <TooltipTrigger asChild>
  108. <Button
  109. type="button"
  110. variant="outline"
  111. size="icon"
  112. disabled={isSubmitting}
  113. aria-label="Letzte Suchen anzeigen"
  114. title="Letzte Suchen anzeigen"
  115. className="shrink-0"
  116. >
  117. <Clock3 className="h-4 w-4" />
  118. </Button>
  119. </TooltipTrigger>
  120. </PopoverTrigger>
  121. <TooltipContent side="bottom">Letzte Suchen anzeigen</TooltipContent>
  122. </Tooltip>
  123. <Button
  124. type="submit"
  125. disabled={!canSearch || isSubmitting}
  126. className="shrink-0"
  127. >
  128. <Search className="h-4 w-4" />
  129. Suchen
  130. </Button>
  131. </div>
  132. <PopoverContent align="start" className="w-[22rem] p-0">
  133. {hasHistory ? (
  134. <div className="overflow-hidden">
  135. <Command>
  136. <CommandList className="max-h-72">
  137. <CommandGroup heading="Letzte Suchen">
  138. {historyItems.map((entry, index) => (
  139. <CommandItem
  140. key={`${entry.routeBranch}|${entry.q}|${entry.createdAt}|${index}`}
  141. value={`${entry.q} ${entry.routeBranch} ${entry.scope} ${(entry.branches || []).join(" ")}`}
  142. onSelect={() => {
  143. if (typeof onSelectRecentSearch !== "function") return;
  144. onSelectRecentSearch(entry);
  145. setOpen(false);
  146. }}
  147. className="items-start gap-3 py-2"
  148. >
  149. <Clock3 className="mt-0.5 h-4 w-4 text-muted-foreground" />
  150. <div className="min-w-0 flex-1">
  151. <p className="truncate text-sm font-medium">{entry.q}</p>
  152. <p className="truncate text-xs text-muted-foreground">
  153. {getEntryMetaSummary(entry)}
  154. </p>
  155. </div>
  156. </CommandItem>
  157. ))}
  158. </CommandGroup>
  159. </CommandList>
  160. </Command>
  161. <div className="border-t p-2">
  162. <Button
  163. type="button"
  164. variant="ghost"
  165. size="sm"
  166. className="w-full justify-start"
  167. disabled={!canClearHistory}
  168. onClick={() => {
  169. if (!canClearHistory) return;
  170. onClearRecentSearches();
  171. setOpen(false);
  172. }}
  173. >
  174. Verlauf löschen
  175. </Button>
  176. </div>
  177. </div>
  178. ) : (
  179. <div className="p-3 text-sm text-muted-foreground">
  180. Keine letzten Suchen vorhanden.
  181. </div>
  182. )}
  183. </PopoverContent>
  184. </Popover>
  185. {currentQuery ? (
  186. <div className="text-xs text-muted-foreground">
  187. Aktuelle Suche:{" "}
  188. <span className="ml-1 rounded-md bg-muted px-2 py-1 text-xs text-muted-foreground">
  189. {currentQuery}
  190. </span>
  191. </div>
  192. ) : (
  193. <div className="text-xs text-muted-foreground">
  194. Tipp: Die Suche ist URL-basiert und kann als Link geteilt werden.
  195. </div>
  196. )}
  197. </div>
  198. );
  199. }