From d5e8c154f2c3c68ad108e96cd5aeffa109708fb5 Mon Sep 17 00:00:00 2001 From: Daniel Compton Date: Tue, 19 Dec 2017 21:21:16 +1300 Subject: [PATCH] Persist filter category settings to localstorage Fixes #121 --- src/day8/re_frame/trace/db.cljs | 4 +++- src/day8/re_frame/trace/events.cljs | 13 ++++++++++++- src/day8/re_frame/trace/subs.cljs | 5 +++++ src/day8/re_frame/trace/view/traces.cljs | 16 ++++++---------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/day8/re_frame/trace/db.cljs b/src/day8/re_frame/trace/db.cljs index 41f10c9..e458a41 100644 --- a/src/day8/re_frame/trace/db.cljs +++ b/src/day8/re_frame/trace/db.cljs @@ -10,7 +10,8 @@ app-db-paths (localstorage/get "app-db-paths" '()) json-ml-paths (localstorage/get "app-db-json-ml-expansions" #{}) external-window? (localstorage/get "external-window?" false) - using-trace? (localstorage/get "using-trace?" true)] + using-trace? (localstorage/get "using-trace?" true) + categories (localstorage/get "categories" #{:event :sub/run :sub/create :sub/dispose})] (when using-trace? (rf/dispatch [:global/enable-tracing])) (rf/dispatch [:settings/panel-width% panel-width%]) @@ -19,6 +20,7 @@ (when external-window? (rf/dispatch [:global/launch-external])) (rf/dispatch [:traces/filter-items filter-items]) + (rf/dispatch [:traces/toggle-categories categories]) (rf/dispatch [:app-db/paths app-db-paths]) (rf/dispatch [:app-db/set-json-ml-paths json-ml-paths]) (rf/dispatch [:global/add-unload-hook]))) diff --git a/src/day8/re_frame/trace/events.cljs b/src/day8/re_frame/trace/events.cljs index f6b4963..c1d0b67 100644 --- a/src/day8/re_frame/trace/events.cljs +++ b/src/day8/re_frame/trace/events.cljs @@ -7,7 +7,8 @@ [goog.object] [re-frame.db] [day8.re-frame.trace.view.container :as container] - [day8.re-frame.trace.styles :as styles])) + [day8.re-frame.trace.styles :as styles] + [clojure.set :as set])) (defonce traces (r/atom [])) (defonce total-traces (r/atom 0)) @@ -213,6 +214,16 @@ (let [showing? (get-in expansions [:overrides id] (:show-all? expansions))] (update-in expansions [:overrides id] #(if showing? false (not %)))))) +(rf/reg-event-db + :traces/toggle-categories + [(rf/path [:traces :categories])] + (fn [categories [_ new-categories]] + (let [new-categories (if (set/superset? categories new-categories) + (set/difference categories new-categories) + (set/union categories new-categories))] + (localstorage/save! "categories" new-categories) + new-categories))) + ;; App DB (rf/reg-event-db diff --git a/src/day8/re_frame/trace/subs.cljs b/src/day8/re_frame/trace/subs.cljs index 9fde818..d726300 100644 --- a/src/day8/re_frame/trace/subs.cljs +++ b/src/day8/re_frame/trace/subs.cljs @@ -67,6 +67,11 @@ (fn [db _] (get-in db [:traces :expansions]))) +(rf/reg-sub + :traces/categories + (fn [db _] + (get-in db [:traces :categories]))) + (rf/reg-sub :global/unloading? (fn [db _] diff --git a/src/day8/re_frame/trace/view/traces.cljs b/src/day8/re_frame/trace/view/traces.cljs index f2fde1e..53259b0 100644 --- a/src/day8/re_frame/trace/view/traces.cljs +++ b/src/day8/re_frame/trace/view/traces.cljs @@ -85,14 +85,10 @@ filter-items (rf/subscribe [:traces/filter-items]) filter-type (r/atom :contains) input-error (r/atom false) - categories (r/atom #{:event :sub/run :sub/create :sub/dispose}) + categories (rf/subscribe [:traces/categories]) trace-detail-expansions (rf/subscribe [:traces/expansions])] (fn [] - (let [toggle-category-fn (fn [category-keys] - (swap! categories #(if (set/superset? % category-keys) - (set/difference % category-keys) - (set/union % category-keys)))) - + (let [toggle-category-fn #(rf/dispatch [:traces/toggle-categories %]) visible-traces (cond->> @traces ;; Remove cached subscriptions. Could add this back in as a setting later ;; but it's pretty low signal/noise 99% of the time. @@ -112,16 +108,16 @@ [:div.filter-control [:ul.filter-categories "show: " [:li.filter-category {:class (when (contains? @categories :event) "active") - :on-click #(toggle-category-fn #{:event})} + :on-click #(rf/dispatch [:traces/toggle-categories #{:event}])} "events"] [:li.filter-category {:class (when (contains? @categories :sub/run) "active") - :on-click #(toggle-category-fn #{:sub/run :sub/create :sub/dispose})} + :on-click #(rf/dispatch [:traces/toggle-categories #{:sub/run :sub/create :sub/dispose}])} "subscriptions"] [:li.filter-category {:class (when (contains? @categories :render) "active") - :on-click #(toggle-category-fn #{:render})} + :on-click #(rf/dispatch [:traces/toggle-categories #{:render}])} "reagent"] [:li.filter-category {:class (when (contains? @categories :re-frame.router/fsm-trigger) "active") - :on-click #(toggle-category-fn #{:re-frame.router/fsm-trigger :componentWillUnmount})} + :on-click #(rf/dispatch [:traces/toggle-categories #{:re-frame.router/fsm-trigger :componentWillUnmount}])} "internals"]] [:div.filter-fields [:select {:value @filter-type