Fix attachment reusal bug

The way that the atom was sometimes dereferenced and sometimes not
caused weird re-rendering artifacts, where sometimes attachment would
show up below posts that they actually did not belong to.
Refactoring all of the different cases to be handled by separate
functions seems to solve this.
This commit is contained in:
arne 2025-11-21 13:59:09 +01:00
commit 58d709f7be

View file

@ -353,24 +353,33 @@
(interleave (repeat ", ")) (interleave (repeat ", "))
(drop 1))]) (drop 1))])
(defn attachment [{:keys [attachment]}] (defn img-attachment [{:keys [attachment preview-url]}]
(let [preview-url (or (:preview_remote_url attachment) (:preview_url attachment)) [:img {:src preview-url
remote-url (or (:remote_url attachment) (:url attachment))
ext (last (str/split remote-url #"\."))]
(case (:type attachment)
"image" [:img {:src preview-url
:srcset (str preview-url ", " remote-url)
:alt (:description attachment) :alt (:description attachment)
:loading "lazy"}] :loading "lazy"}])
"video" [:video {:controls true}
(defn video-attachment [{:keys [attachment remote-url ext]}]
[:video {:controls true}
[:source {:type (str "video/" ext) :src remote-url}] [:source {:type (str "video/" ext) :src remote-url}]
[:a {:href (:remote_url attachment)} (str "Original video at " (:remote_url attachment))]] [:a {:href (:remote_url attachment)} (str "Original video at " (:remote_url attachment))]])
"gifv" (let [autoplay (r/atom false)
(defn gifv-attachment [{:keys [attachment remote-url ext]}]
(let [autoplay (r/atom false)
toggle-autoplay #(swap! autoplay not)] toggle-autoplay #(swap! autoplay not)]
(fn [] (fn []
[:video {:loop true :autoplay @autoplay :muted true :on-pointer-enter toggle-autoplay} [:video {:loop true :autoplay @autoplay :muted true :on-pointer-enter toggle-autoplay}
[:source {:type (str "video/" ext) :src remote-url}] [:source {:type (str "video/" ext) :src remote-url}]
[:a {:href (:remote_url attachment)} (str "Original video at " (:remote_url attachment))]])) [:a {:href (:remote_url attachment)} (str "Original video at " (:remote_url attachment))]])))
(defn attachment [{:keys [attachment]}]
(let [preview-url (or (:preview_remote_url attachment) (:preview_url attachment))
remote-url (or (:remote_url attachment) (:url attachment))
ext (last (str/split remote-url #"\."))
props {:attachment attachment :preview-url preview-url :remote-url remote-url :ext ext}]
(case (:type attachment)
"image" [img-attachment props]
"video" [video-attachment props]
"gifv" [gifv-attachment props]
[:div [:strong "Unsupported attachment"] [:div [:strong "Unsupported attachment"]
[debug attachment]]))) [debug attachment]])))