SearchForm.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use client";
  2. import React from "react";
  3. import { AlertCircleIcon } from "lucide-react";
  4. import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
  5. import SearchQueryBox from "@/components/search/form/SearchQueryBox";
  6. import SearchScopeSelect from "@/components/search/form/SearchScopeSelect";
  7. import SearchLimitSelect from "@/components/search/form/SearchLimitSelect";
  8. import SearchSingleBranchCombobox from "@/components/search/form/SearchSingleBranchCombobox";
  9. import SearchMultiBranchPicker from "@/components/search/form/SearchMultiBranchPicker";
  10. import SearchDateRangePicker from "@/components/search/form/SearchDateRangePicker";
  11. import SearchDateFilterChip from "@/components/search/form/SearchDateFilterChip";
  12. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  13. export default function SearchForm({
  14. branch,
  15. qDraft,
  16. onQDraftChange,
  17. onSubmit,
  18. currentQuery,
  19. isSubmitting,
  20. isAdminDev,
  21. scope,
  22. onScopeChange,
  23. onSingleBranchChange,
  24. availableBranches,
  25. branchesStatus,
  26. selectedBranches,
  27. onToggleBranch,
  28. onClearAllBranches,
  29. limit,
  30. onLimitChange,
  31. from,
  32. to,
  33. onDateRangeChange,
  34. validationError,
  35. }) {
  36. const canSearch = typeof qDraft === "string" && qDraft.trim().length > 0;
  37. // SINGLE scope (admin/dev only) shows an extra branch combobox.
  38. const showSingleBranchCombobox = Boolean(
  39. isAdminDev && scope === SEARCH_SCOPE.SINGLE
  40. );
  41. const hasDateFilter = Boolean(from || to);
  42. return (
  43. <div className="space-y-4">
  44. <form
  45. onSubmit={(e) => {
  46. e.preventDefault();
  47. if (!canSearch) return;
  48. onSubmit();
  49. }}
  50. className="space-y-4"
  51. >
  52. {/* Row 1: query full-width */}
  53. <div className="w-full">
  54. <SearchQueryBox
  55. qDraft={qDraft}
  56. onQDraftChange={onQDraftChange}
  57. onSubmit={onSubmit}
  58. currentQuery={currentQuery}
  59. isSubmitting={isSubmitting}
  60. canSearch={canSearch}
  61. />
  62. </div>
  63. {/* Row 2: left controls + spacer + limit on the right */}
  64. <div className="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
  65. {/* Left group: scope (+ optional single branch combobox) + date range */}
  66. <div className="flex flex-col gap-3 sm:flex-row sm:items-end">
  67. {isAdminDev ? (
  68. <div className="flex items-end gap-3">
  69. <SearchScopeSelect
  70. branch={branch}
  71. scope={scope}
  72. onScopeChange={onScopeChange}
  73. isSubmitting={isSubmitting}
  74. />
  75. {/* Animated presence (kept for smoothness when SINGLE shows the extra combobox) */}
  76. <div
  77. className={[
  78. "overflow-hidden transition-all duration-200 ease-in-out",
  79. showSingleBranchCombobox
  80. ? "max-w-44 max-h-24 opacity-100"
  81. : "max-w-0 max-h-0 opacity-0 pointer-events-none",
  82. ].join(" ")}
  83. aria-hidden={showSingleBranchCombobox ? "false" : "true"}
  84. >
  85. <SearchSingleBranchCombobox
  86. branch={branch}
  87. branchesStatus={branchesStatus}
  88. availableBranches={availableBranches}
  89. onSingleBranchChange={onSingleBranchChange}
  90. isSubmitting={isSubmitting || !showSingleBranchCombobox}
  91. />
  92. </div>
  93. </div>
  94. ) : null}
  95. <div className="shrink-0 flex items-end gap-4">
  96. <SearchDateRangePicker
  97. from={from}
  98. to={to}
  99. onDateRangeChange={onDateRangeChange}
  100. isSubmitting={isSubmitting}
  101. />
  102. {/* Active date filter chip (quick clear) */}
  103. {hasDateFilter ? (
  104. <div className="flex flex-wrap items-center gap-2 mb-2">
  105. <span className="text-xs text-muted-foreground">
  106. Aktive Filter:
  107. </span>
  108. <SearchDateFilterChip
  109. from={from}
  110. to={to}
  111. isSubmitting={isSubmitting}
  112. onClear={() => {
  113. if (typeof onDateRangeChange !== "function") return;
  114. onDateRangeChange({ from: null, to: null });
  115. }}
  116. />
  117. </div>
  118. ) : null}
  119. </div>
  120. </div>
  121. {/* Right group: limit aligned right */}
  122. <div className="shrink-0">
  123. <SearchLimitSelect
  124. limit={limit}
  125. onLimitChange={onLimitChange}
  126. isSubmitting={isSubmitting}
  127. />
  128. </div>
  129. </div>
  130. </form>
  131. {/* Validation feedback belongs near the inputs (not in results). */}
  132. {validationError ? (
  133. <Alert variant="destructive">
  134. <AlertCircleIcon />
  135. <AlertTitle>{validationError.title}</AlertTitle>
  136. <AlertDescription>{validationError.description}</AlertDescription>
  137. </Alert>
  138. ) : null}
  139. {/* Multi scope branch picker remains below */}
  140. {isAdminDev && scope === SEARCH_SCOPE.MULTI ? (
  141. <SearchMultiBranchPicker
  142. branchesStatus={branchesStatus}
  143. availableBranches={availableBranches}
  144. selectedBranches={selectedBranches}
  145. onToggleBranch={onToggleBranch}
  146. onClearAllBranches={onClearAllBranches}
  147. isSubmitting={isSubmitting}
  148. />
  149. ) : null}
  150. </div>
  151. );
  152. }