Skip to content

Commit

Permalink
Test for lazy images
Browse files Browse the repository at this point in the history
  • Loading branch information
easafe committed Jun 27, 2024
1 parent c744a2f commit 29cb780
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/Shared/Avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createImg() {
let i = new Image();

i.setAttribute('async', '');
i.setAttribute('loading', 'defer');

return i;
}
17 changes: 14 additions & 3 deletions src/Shared/Avatar.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import Prelude

import Data.Maybe (Maybe)
import Data.Maybe as DM
import Effect (Effect)
import Flame.Html.Attribute as HA
import Flame.Types (NodeData)
import Shared.Resource (Bundle(..), Media(..), ResourceType(..))
import Flame.Html.Element (class ToNode)
import Flame.Html.Element as HE
import Flame.Types (Html, NodeData)
import Shared.Resource (Media(..), ResourceType(..))
import Shared.Resource as SP
import Shared.Unsafe as SU
import Web.DOM (Node)

foreign import createImg :: Effect Node

defaultAvatar String
defaultAvatar = avatarPath 1
Expand Down Expand Up @@ -45,11 +51,16 @@ avatarColorClass index = className <> show (mod (SU.fromJust index) totalColorCl
totalColorClasses = 4

parseAvatar Maybe String Maybe String
parseAvatar avatar = (\a → SP.mediaPath (Upload a) Included) <$> avatar
parseAvatar av = (\a → SP.mediaPath (Upload a) Included) <$> av

async message. NodeData message
async = HA.createAttribute "async" ""

decoding (message Type). String NodeData message
decoding value = HA.createAttribute "decoding" value

-- avoid lagging pictures when browsing suggestions
avatar :: forall nd34 message35. ToNode nd34 message35 NodeData => nd34 -> Html message35
avatar attributes = HE.managed { createNode, updateNode } attributes unit
where createNode _ = createImg
updateNode _ _ _ = createImg
6 changes: 3 additions & 3 deletions src/Shared/Im/View/SuggestionCall.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ suggestionCall { contacts, suggesting, chatting, suggestions, toggleModal }
[ let
previousIndex = map (_ - 1) suggesting
in
HE.img [ SA.async, SA.decoding "lazy", HA.class' $ "avatar-contact-list" <> SA.avatarColorClass previousIndex, HA.src $ SA.avatarForRecipient previousIndex $ getAvatar previousIndex ]
SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass previousIndex, HA.src $ SA.avatarForRecipient previousIndex $ getAvatar previousIndex ]
]
, HE.div [ HA.class' "avatar-contact-list-div margin-less-z", HA.onClick FocusCurrentSuggestion, HA.title "Move to this chat suggestion" ]
[ HE.img [ SA.async, SA.decoding "lazy", HA.class' $ avatarClasses avatar, HA.src $ SA.avatarForRecipient suggesting avatar ]
[ SA.avatar [ HA.class' $ avatarClasses avatar, HA.src $ SA.avatarForRecipient suggesting avatar ]
]
, HE.div [ HA.class' "avatar-contact-list-div margin-less faded", HA.onClick $ SpecialRequest NextSuggestion, HA.title "Move to this chat suggestion" ]
[ let
nextIndex = map (_ + 1) suggesting
in
HE.img [ SA.async, SA.decoding "lazy", HA.class' $ "avatar-contact-list" <> SA.avatarColorClass nextIndex, HA.src $ SA.avatarForRecipient nextIndex $ getAvatar nextIndex ]
SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass nextIndex, HA.src $ SA.avatarForRecipient nextIndex $ getAvatar nextIndex ]
]
, HE.div [ HA.class' "contact-profile", HA.title "Your chat suggestions" ]
[ HE.span (HA.class' "contact-name") name
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Im/View/SuggestionProfile.purs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fullProfile presentation index model@{ toggleContextMenu, freeToFetchSuggestions

displayProfile message. Maybe Int ImUser ImUser Maybe message Array (Html message)
displayProfile index loggedUser { karmaPosition, name, availability, temporary, avatar, age, karma, headline, gender, onlineStatus, country, languages, tags, description } temporaryUserMessage =
[ HE.img [ SA.async, SA.decoding "lazy", HA.class' avatarClasses, HA.src $ SA.avatarForRecipient index avatar ]
[ SA.avatar [ HA.class' avatarClasses, HA.src $ SA.avatarForRecipient index avatar ]
, HE.h1 (HA.class' "profile-name") name
, HE.div (HA.class' "headline") headline
, HE.div [ HA.class' { "online-status": true, hidden: not loggedUser.onlineStatus || not onlineStatus, duller: availability /= Online } ] $ show availability
Expand Down

0 comments on commit 29cb780

Please sign in to comment.