From edd50dfe18e81faae9c1fc428dbaa3e7b6bbc55b Mon Sep 17 00:00:00 2001 From: Icaro Motta Date: Mon, 23 Dec 2024 20:36:01 -0300 Subject: [PATCH] Render chat by chat ID - Upon logout, the chat-list-item component is reprocessed and the subscription returns a nil chat because the app-db state has been cleaned up. We fix the timestamp formatter to handle nil values and return a default string. --- .../chat/home/chat_list_item/view.cljs | 33 +++++++++++++------ src/status_im/contexts/chat/home/view.cljs | 8 ++--- src/status_im/subs/chats.cljs | 24 ++++++-------- src/utils/datetime.cljs | 19 ++++++----- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/status_im/contexts/chat/home/chat_list_item/view.cljs b/src/status_im/contexts/chat/home/chat_list_item/view.cljs index f6bd9cf1e9..48783f5abf 100644 --- a/src/status_im/contexts/chat/home/chat_list_item/view.cljs +++ b/src/status_im/contexts/chat/home/chat_list_item/view.cljs @@ -280,18 +280,31 @@ {:style (merge style/container {:margin-horizontal 0})} [chat-item item]]) +(defn- bottom-sheet-options + [chat-id chat-type] + (cond-> {:content (fn [] + (let [chat (rf/sub [:chats/chat-by-id chat-id])] + [actions/chat-actions chat false]))} + (= chat-type constants/one-to-one-chat-type) + (assoc :selected-item + (fn [] + (let [chat (rf/sub [:chats/chat-by-id chat-id])] + [chat-user chat]))))) + (defn chat-list-item - [{:keys [chat-id chat-type] - :as item}] - (let [customization-color (rf/sub [:profile/customization-color]) - theme (quo.theme/use-theme)] + [{:keys [chat-id]}] + (let [{:keys [chat-type] + :as chat} (rf/sub [:chats/chat-by-id chat-id]) + customization-color (rf/sub [:profile/customization-color]) + theme (quo.theme/use-theme) + on-long-press (rn/use-callback + (fn [] + (rf/dispatch [:show-bottom-sheet + (bottom-sheet-options chat-id chat-type)])) + [chat-id chat-type])] [rn/touchable-highlight {:style style/container :on-press (open-chat chat-id) :underlay-color (colors/resolve-color customization-color theme 5) - :on-long-press #(rf/dispatch [:show-bottom-sheet - (cond-> {:content (fn [] [actions/chat-actions item false])} - (= chat-type constants/one-to-one-chat-type) - (assoc :selected-item - (fn [] [chat-user item])))])} - [chat-item item]])) + :on-long-press on-long-press} + [chat-item chat]])) diff --git a/src/status_im/contexts/chat/home/view.cljs b/src/status_im/contexts/chat/home/view.cljs index d64dfdaf8e..b988fcf501 100644 --- a/src/status_im/contexts/chat/home/view.cljs +++ b/src/status_im/contexts/chat/home/view.cljs @@ -23,12 +23,10 @@ [_ index] #js {:length 56 :offset (* 56 index) :index index}) -(defn filter-and-sort-items-by-tab +(defn filter-by-tab [tab items] (let [k (if (= tab :tab/groups) :group-chat :chat-id)] - (->> items - (filter k) - (sort-by :timestamp >)))) + (filter k items))) (defn empty-state-content [theme] @@ -56,7 +54,7 @@ (defn chats [{:keys [theme selected-tab set-scroll-ref scroll-shared-value]}] (let [unfiltered-items (rf/sub [:chats/chats-stack-items]) - items (filter-and-sort-items-by-tab selected-tab unfiltered-items) + items (filter-by-tab selected-tab unfiltered-items) on-scroll (rn/use-callback (fn [event] (common.banner/set-scroll-shared-value diff --git a/src/status_im/subs/chats.cljs b/src/status_im/subs/chats.cljs index 9eb01cf26a..5ba9704994 100644 --- a/src/status_im/subs/chats.cljs +++ b/src/status_im/subs/chats.cljs @@ -53,22 +53,18 @@ (fn [[{:keys [chats]}] [_ community-id chat-id]] (get chats (string/replace chat-id community-id "")))) -(re-frame/reg-sub - :chats/home-list-chats +(re-frame/reg-sub :chats/home-list-chats :<- [:chats/chats] :<- [:chats-home-list] - :<- [:multiaccount/public-key] - (fn [[chats active-chats my-public-key]] - (reduce #(if-let [item (get chats %2)] - (let [group-chat-member? (and (chat.events/group-chat? item) - (group-chats.db/member? my-public-key item))] - (conj %1 - (assoc item - :group-chat-member? - group-chat-member?))) - %1) - [] - active-chats))) + (fn [[chats active-chats-ids]] + (->> active-chats-ids + (keep #(get chats %)) + (sort-by :timestamp >) + (map (fn [{:keys [chat-id id public-key group-chat]}] + {:chat-id chat-id + :group-chat group-chat + :id id + :public-key public-key}))))) (re-frame/reg-sub :chats/chat-by-id diff --git a/src/utils/datetime.cljs b/src/utils/datetime.cljs index ffca333637..537283f983 100644 --- a/src/utils/datetime.cljs +++ b/src/utils/datetime.cljs @@ -160,15 +160,16 @@ ;;;; Timestamp formatters (defn- to-str [ms old-fmt-fn yesterday-fmt-fn today-fmt-fn] - (let [date (t.coerce/from-long ms) - ;; NOTE(edge-case): this is wrong, it uses the current timezone offset, regardless of DST. - local (t/plus date time-zone-offset) - today (t/minus (t/today-at-midnight) time-zone-offset) - yesterday (t/plus today (t/days -1))] - (cond - (t/before? date yesterday) (old-fmt-fn local) - (t/before? date today) (yesterday-fmt-fn local) - :else (today-fmt-fn local)))) + (if-let [date (t.coerce/from-long ms)] + (let [;; NOTE(edge-case): this is wrong, it uses the current timezone offset, regardless of DST. + local (t/plus date time-zone-offset) + today (t/minus (t/today-at-midnight) time-zone-offset) + yesterday (t/plus today (t/days -1))] + (cond + (t/before? date yesterday) (old-fmt-fn local) + (t/before? date today) (yesterday-fmt-fn local) + :else (today-fmt-fn local))) + "")) (defn to-short-str [ms]