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.
This commit is contained in:
Icaro Motta 2024-12-23 20:36:01 -03:00
parent d5fa11a918
commit edd50dfe18
No known key found for this signature in database
GPG Key ID: 009557D9D014DF07
4 changed files with 46 additions and 38 deletions

View File

@ -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]]))

View File

@ -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

View File

@ -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

View File

@ -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]