SearchQueryBox.jsx 5.9 KB

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