SearchResults.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use client";
  2. import React from "react";
  3. import { Loader2 } from "lucide-react";
  4. import ExplorerLoading from "@/components/explorer/states/ExplorerLoading";
  5. import ExplorerEmpty from "@/components/explorer/states/ExplorerEmpty";
  6. import ExplorerError from "@/components/explorer/states/ExplorerError";
  7. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  8. import { Button } from "@/components/ui/button";
  9. import {
  10. sortSearchItems,
  11. SEARCH_RESULTS_SORT,
  12. } from "@/lib/frontend/search/resultsSorting";
  13. import SearchResultsToolbar from "@/components/search/SearchResultsToolbar";
  14. import SearchResultsTable from "@/components/search/SearchResultsTable";
  15. export default function SearchResults({
  16. branch,
  17. scope,
  18. status,
  19. items,
  20. total,
  21. error,
  22. onRetry,
  23. nextCursor,
  24. onLoadMore,
  25. isLoadingMore,
  26. loadMoreError,
  27. needsBranchSelection = false,
  28. }) {
  29. const [sortMode, setSortMode] = React.useState(SEARCH_RESULTS_SORT.RELEVANCE);
  30. const sortedItems = React.useMemo(() => {
  31. return sortSearchItems(items, sortMode);
  32. }, [items, sortMode]);
  33. if (status === "idle") {
  34. if (needsBranchSelection) {
  35. return (
  36. <ExplorerEmpty
  37. title="Niederlassungen auswählen"
  38. description="Bitte wählen Sie mindestens eine Niederlassung aus, um die Suche zu starten."
  39. upHref={null}
  40. />
  41. );
  42. }
  43. return (
  44. <ExplorerEmpty
  45. title="Suche starten"
  46. description="Bitte geben Sie einen Suchbegriff ein und klicken Sie auf „Suchen“."
  47. upHref={null}
  48. />
  49. );
  50. }
  51. if (status === "loading") {
  52. return <ExplorerLoading variant="table" count={8} />;
  53. }
  54. if (status === "error" && error) {
  55. return (
  56. <ExplorerError
  57. title={error.title}
  58. description={error.description}
  59. onRetry={onRetry}
  60. />
  61. );
  62. }
  63. const list = Array.isArray(sortedItems) ? sortedItems : [];
  64. if (list.length === 0) {
  65. return (
  66. <ExplorerEmpty
  67. title="Keine Treffer"
  68. description="Für Ihre Suche wurden keine Treffer gefunden."
  69. upHref={null}
  70. />
  71. );
  72. }
  73. return (
  74. <div className="space-y-4">
  75. <SearchResultsToolbar
  76. countLoaded={list.length}
  77. total={total}
  78. sortMode={sortMode}
  79. onSortModeChange={setSortMode}
  80. />
  81. <SearchResultsTable routeBranch={branch} scope={scope} items={list} />
  82. {loadMoreError ? (
  83. <Alert variant="destructive">
  84. <AlertTitle>{loadMoreError.title}</AlertTitle>
  85. <AlertDescription>{loadMoreError.description}</AlertDescription>
  86. </Alert>
  87. ) : null}
  88. {nextCursor ? (
  89. <div className="flex justify-center">
  90. <Button
  91. type="button"
  92. variant="outline"
  93. onClick={onLoadMore}
  94. disabled={isLoadingMore}
  95. title="Weitere Ergebnisse laden"
  96. >
  97. {isLoadingMore ? (
  98. <>
  99. <Loader2 className="h-4 w-4 animate-spin" />
  100. Lädt…
  101. </>
  102. ) : (
  103. "Mehr laden"
  104. )}
  105. </Button>
  106. </div>
  107. ) : null}
  108. </div>
  109. );
  110. }