From c910b46ee7867bf2f465e1c2707e2b9864c267d3 Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Fri, 19 Apr 2024 19:08:53 +0200 Subject: fix: fix index out-of-bounds error in media queue removal logic --- src/frontend/tubo/components/play_queue.cljs | 2 +- src/frontend/tubo/events.cljs | 23 +++++++++++++++++------ src/frontend/tubo/subs.cljs | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/tubo/components/play_queue.cljs b/src/frontend/tubo/components/play_queue.cljs index ddb81d5..c2dbe7a 100644 --- a/src/frontend/tubo/components/play_queue.cljs +++ b/src/frontend/tubo/components/play_queue.cljs @@ -20,7 +20,7 @@ media-queue-pos @(rf/subscribe [:media-queue-pos]) media-queue @(rf/subscribe [:media-queue])] [:div.relative.w-full - {:ref #(when (and (> (count media-queue) 0) (= media-queue-pos i)) + {:ref #(when (= media-queue-pos i) (rf/dispatch [::events/scroll-into-view %]))} [:div.flex.cursor-pointer.py-2 {:class (when (= i media-queue-pos) "bg-[#f0f0f0] dark:bg-stone-800") diff --git a/src/frontend/tubo/events.cljs b/src/frontend/tubo/events.cljs index c237b1c..b8f3b1f 100644 --- a/src/frontend/tubo/events.cljs +++ b/src/frontend/tubo/events.cljs @@ -390,14 +390,25 @@ ::remove-from-media-queue [(rf/inject-cofx :store)] (fn [{:keys [db store]} [_ pos]] - (let [updated-db (update db :media-queue #(into (subvec % 0 pos) (subvec % (inc pos))))] + (let [updated-db (update db :media-queue #(into (subvec % 0 pos) (subvec % (inc pos)))) + media-queue-pos (:media-queue-pos db) + media-queue-length (count (:media-queue updated-db))] {:db updated-db :store (assoc store :media-queue (:media-queue updated-db)) - :fx (if (and (= pos (:media-queue-pos db)) (not (= (count (:media-queue updated-db)) 0))) - [[:dispatch [::change-media-queue-pos pos]]] - (if (= (count (:media-queue updated-db)) 0) - [[:dispatch [::dispose-audio-player]] - [:dispatch [::show-media-queue false]]]))}))) + :fx (cond + (and (not (= media-queue-length 0)) + (or (< pos media-queue-pos) + (= pos media-queue-pos) + (= media-queue-pos media-queue-length))) + [[:dispatch [::change-media-queue-pos + (cond + (= pos media-queue-length) 0 + (= pos media-queue-pos) pos + :else (dec media-queue-pos))]]] + (= (count (:media-queue updated-db)) 0) + [[:dispatch [::dispose-audio-player]] + [:dispatch [::show-media-queue false]]] + :else [])}))) (rf/reg-event-fx ::change-media-queue-pos diff --git a/src/frontend/tubo/subs.cljs b/src/frontend/tubo/subs.cljs index eec5ec8..f2a96ed 100644 --- a/src/frontend/tubo/subs.cljs +++ b/src/frontend/tubo/subs.cljs @@ -160,7 +160,7 @@ (fn [_] [(rf/subscribe [:media-queue]) (rf/subscribe [:media-queue-pos])]) (fn [[queue pos] _] - (and (not-empty queue) (nth queue pos)))) + (and (not-empty queue) (< pos (count queue)) (nth queue pos)))) (rf/reg-sub :bookmarks -- cgit v1.2.3