From d3bc5f60c475d8b4cdc1be1b4bc12f66bd770bd9 Mon Sep 17 00:00:00 2001 From: Mega Yu Date: Sat, 28 Feb 2026 22:31:40 +0800 Subject: [PATCH] refactor: optimize use-effect for highlighted item handling and cache management --- src/main/frontend/components/cmdk/core.cljs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/frontend/components/cmdk/core.cljs b/src/main/frontend/components/cmdk/core.cljs index 67e2d331bf..76602c7f0c 100644 --- a/src/main/frontend/components/cmdk/core.cljs +++ b/src/main/frontend/components/cmdk/core.cljs @@ -1057,10 +1057,8 @@ (on-change new-value)))) 200) [])] - ;; use-effect [results-ordered input] to check whether the highlighted item is still in the results, - ;; if not then clear that puppy out! - ;; This was moved to a functional component (hooks/use-effect! (fn [] + (reset! (::all-items-cache state) (vec all-items)) (when highlighted-item (let [idx (:item-index highlighted-item) ;; Fast path via cached :item-index; fall back to .indexOf if stale. @@ -1078,11 +1076,11 @@ (.focus el) (.select el))) 0))] + (load-results :default state) (fn [] (when timeout-id (js/clearTimeout timeout-id))))) []) - (hooks/use-effect! (fn [] (load-results :default state)) []) ;; fired when highlighted item changes (normal keyboard navigation) (hooks/use-effect! (fn [] @@ -1278,8 +1276,7 @@ (:group (rum/react (::filter state)))) results-ordered (state->results-ordered state search-mode) all-items (mapcat last results-ordered) - first-item (first all-items) - _ (reset! (::all-items-cache state) (vec all-items))] + first-item (first all-items)] [:div.cp__cmdk {:ref #(when-not @(::ref state) (reset! (::ref state) %)) :class (cond-> "w-full h-full relative flex flex-col justify-start" (not sidebar?) (str " rounded-lg"))}