From dbd0fd197323f2a83fab384d7b4b116a02165d16 Mon Sep 17 00:00:00 2001
From: xuatz <xzlow10@gmail.com>
Date: Sun, 18 May 2025 17:21:11 +0900
Subject: fix(search): add new relevance sort order (#1392)

* fix(search): add new relevance sort order

* address pr comments

* some minor fixes

---------

Co-authored-by: Mohamed Bassem <me@mbassem.com>
---
 apps/web/components/dashboard/SortOrderToggle.tsx | 36 +++++++++++++++++++----
 1 file changed, 30 insertions(+), 6 deletions(-)

(limited to 'apps/web/components/dashboard/SortOrderToggle.tsx')

diff --git a/apps/web/components/dashboard/SortOrderToggle.tsx b/apps/web/components/dashboard/SortOrderToggle.tsx
index 8c0f617d..ba3385ac 100644
--- a/apps/web/components/dashboard/SortOrderToggle.tsx
+++ b/apps/web/components/dashboard/SortOrderToggle.tsx
@@ -1,3 +1,6 @@
+"use client";
+
+import { useEffect } from "react";
 import { ButtonWithTooltip } from "@/components/ui/button";
 import {
   DropdownMenu,
@@ -5,15 +8,26 @@ import {
   DropdownMenuItem,
   DropdownMenuTrigger,
 } from "@/components/ui/dropdown-menu";
+import { useIsSearchPage } from "@/lib/hooks/bookmark-search";
 import { useTranslation } from "@/lib/i18n/client";
 import { useSortOrderStore } from "@/lib/store/useSortOrderStore";
-import { Check, SortAsc, SortDesc } from "lucide-react";
+import { Check, ListFilter, SortAsc, SortDesc } from "lucide-react";
 
 export default function SortOrderToggle() {
   const { t } = useTranslation();
+  const isInSearchPage = useIsSearchPage();
 
   const { sortOrder: currentSort, setSortOrder } = useSortOrderStore();
 
+  // also see related on page enter sortOrder.relevance init
+  // in apps/web/app/dashboard/search/page.tsx
+  useEffect(() => {
+    if (!isInSearchPage && currentSort === "relevance") {
+      // reset to default sort order
+      setSortOrder("desc");
+    }
+  }, [isInSearchPage, currentSort]);
+
   return (
     <DropdownMenu>
       <DropdownMenuTrigger asChild>
@@ -22,14 +36,24 @@ export default function SortOrderToggle() {
           delayDuration={100}
           variant="ghost"
         >
-          {currentSort === "asc" ? (
-            <SortAsc size={18} />
-          ) : (
-            <SortDesc size={18} />
-          )}
+          {currentSort === "relevance" && <ListFilter size={18} />}
+          {currentSort === "asc" && <SortAsc size={18} />}
+          {currentSort === "desc" && <SortDesc size={18} />}
         </ButtonWithTooltip>
       </DropdownMenuTrigger>
       <DropdownMenuContent className="w-fit">
+        {isInSearchPage && (
+          <DropdownMenuItem
+            className="cursor-pointer justify-between"
+            onClick={() => setSortOrder("relevance")}
+          >
+            <div className="flex items-center">
+              <ListFilter size={16} className="mr-2" />
+              <span>{t("actions.sort.relevant_first")}</span>
+            </div>
+            {currentSort === "relevance" && <Check className="ml-2 h-4 w-4" />}
+          </DropdownMenuItem>
+        )}
         <DropdownMenuItem
           className="cursor-pointer justify-between"
           onClick={() => setSortOrder("desc")}
-- 
cgit v1.2.3-70-g09d2

