From 565eacb725974bc5df9d1f9a47efc2f07b3a196d Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Sun, 16 Feb 2020 17:32:55 +0100
Subject: [PATCH] Improve skeleton loading design

---
 ui/src/components/AlbumTitle.js               |  3 +-
 ui/src/components/albumGallery/AlbumBox.js    | 61 +++++++++++++------
 ui/src/components/albumGallery/AlbumBoxes.js  |  4 +-
 .../components/albumGallery/AlbumGallery.js   | 13 +++-
 ui/src/components/photoGallery/Photo.js       |  9 +++
 .../components/photoGallery/PhotoGallery.js   | 10 ++-
 6 files changed, 76 insertions(+), 24 deletions(-)

diff --git a/ui/src/components/AlbumTitle.js b/ui/src/components/AlbumTitle.js
index f516cda..7858979 100644
--- a/ui/src/components/AlbumTitle.js
+++ b/ui/src/components/AlbumTitle.js
@@ -8,6 +8,7 @@ import AlbumSidebar from './sidebar/AlbumSidebar'
 
 const Header = styled.h1`
   margin: 0 0 12px 0 !important;
+  height: 36px;
 
   & a {
     color: black;
@@ -34,7 +35,7 @@ const SettingsIcon = props => {
 }
 
 const AlbumTitle = ({ album, disableLink = false }) => {
-  if (!album) return null
+  if (!album) return <div style={{ height: 36 }}></div>
 
   let title = <span>{album.title}</span>
 
diff --git a/ui/src/components/albumGallery/AlbumBox.js b/ui/src/components/albumGallery/AlbumBox.js
index 7926dc0..561a600 100644
--- a/ui/src/components/albumGallery/AlbumBox.js
+++ b/ui/src/components/albumGallery/AlbumBox.js
@@ -1,4 +1,4 @@
-import React from 'react'
+import React, { useState } from 'react'
 import PropTypes from 'prop-types'
 import styled from 'styled-components'
 import { Link } from 'react-router-dom'
@@ -12,26 +12,51 @@ const AlbumBoxLink = styled(Link)`
   color: #222;
 `
 
-const AlbumBoxImage = ({ src, ...props }) => {
-  const Image = styled(ProtectedImage)`
-    width: 220px;
-    height: 220px;
-    margin: auto;
-    border-radius: 4%;
-    object-fit: cover;
-    object-position: center;
-  `
+const ImageWrapper = styled.div`
+  width: 240px;
+  height: 220px;
+  padding: 0 10px;
+  position: relative;
+`
 
-  const Placeholder = styled.div`
-    width: 220px;
-    height: 220px;
-    border-radius: 4%;
-    margin: auto;
-    background: linear-gradient(#f7f7f7 0%, #eee 100%);
-  `
+const Image = styled(ProtectedImage)`
+  width: 220px;
+  height: 220px;
+  margin: auto;
+  border-radius: 4%;
+  object-fit: cover;
+  object-position: center;
+`
+
+const Placeholder = styled.div`
+  width: 220px;
+  height: 220px;
+  border-radius: 4%;
+  margin: auto;
+  background: linear-gradient(#f7f7f7 0%, #eee 100%);
+
+  ${({ overlap, loaded }) =>
+    overlap &&
+    `
+    position: absolute;
+    top: 0;
+    left: 10px;
+    opacity: ${loaded ? 0 : 1};
+
+    transition: opacity 200ms;
+  `}
+`
+
+const AlbumBoxImage = ({ src, ...props }) => {
+  const [loaded, setLoaded] = useState(false)
 
   if (src) {
-    return <Image {...props} src={src} />
+    return (
+      <ImageWrapper>
+        <Image {...props} onLoad={loaded => setLoaded(loaded)} src={src} />
+        <Placeholder overlap loaded={loaded ? 1 : 0} />
+      </ImageWrapper>
+    )
   }
 
   return <Placeholder />
diff --git a/ui/src/components/albumGallery/AlbumBoxes.js b/ui/src/components/albumGallery/AlbumBoxes.js
index aa449c3..983079a 100644
--- a/ui/src/components/albumGallery/AlbumBoxes.js
+++ b/ui/src/components/albumGallery/AlbumBoxes.js
@@ -23,14 +23,14 @@ const AlbumBoxes = ({ loading, error, albums, getCustomLink }) => {
       />
     ))
   } else {
-    for (let i = 0; i < 8; i++) {
+    for (let i = 0; i < 4; i++) {
       albumElements.push(<AlbumBox key={i} />)
     }
   }
 
   return (
     <Container>
-      <Loader active={loading}>Loading albums</Loader>
+      {/* <Loader active={loading}>Loading albums</Loader> */}
       {albumElements}
     </Container>
   )
diff --git a/ui/src/components/albumGallery/AlbumGallery.js b/ui/src/components/albumGallery/AlbumGallery.js
index 3e5c0c7..c31b607 100644
--- a/ui/src/components/albumGallery/AlbumGallery.js
+++ b/ui/src/components/albumGallery/AlbumGallery.js
@@ -65,13 +65,24 @@ const AlbumGallery = ({ album, loading = false, customAlbumLink }) => {
         />
       )
     }
+  } else {
+    subAlbumElement = <AlbumBoxes loading={loading} />
   }
 
   return (
     <Layout>
       <AlbumTitle album={album} disableLink />
       {subAlbumElement}
-      {album && album.subAlbums.length > 0 && <h2>Images</h2>}
+      {
+        <h2
+          style={{
+            opacity: loading ? 0 : 1,
+            display: album && album.subAlbums.length > 0 ? 'block' : 'none',
+          }}
+        >
+          Images
+        </h2>
+      }
       <PhotoGallery
         loading={loading}
         photos={album && album.photos}
diff --git a/ui/src/components/photoGallery/Photo.js b/ui/src/components/photoGallery/Photo.js
index d36c46b..31ff0a2 100644
--- a/ui/src/components/photoGallery/Photo.js
+++ b/ui/src/components/photoGallery/Photo.js
@@ -139,3 +139,12 @@ Photo.propTypes = {
   active: PropTypes.bool.isRequired,
   setPresenting: PropTypes.func.isRequired,
 }
+
+export const PhotoThumbnail = styled.div`
+  flex-grow: 1;
+  height: 200px;
+  width: 300px;
+  margin: 4px;
+  background-color: #eee;
+  position: relative;
+`
diff --git a/ui/src/components/photoGallery/PhotoGallery.js b/ui/src/components/photoGallery/PhotoGallery.js
index 25af35a..65f4d80 100644
--- a/ui/src/components/photoGallery/PhotoGallery.js
+++ b/ui/src/components/photoGallery/PhotoGallery.js
@@ -1,7 +1,7 @@
 import React, { useEffect } from 'react'
 import styled from 'styled-components'
 import { Loader } from 'semantic-ui-react'
-import { Photo } from './Photo'
+import { Photo, PhotoThumbnail } from './Photo'
 import { PresentContainer, PresentPhoto } from './PresentView'
 import PropTypes from 'prop-types'
 import { SidebarConsumer } from '../sidebar/Sidebar'
@@ -11,6 +11,8 @@ const Gallery = styled.div`
   display: flex;
   flex-wrap: wrap;
   align-items: center;
+  min-height: 200px;
+  position: relative;
 `
 
 const PhotoFiller = styled.div`
@@ -62,7 +64,7 @@ const PhotoGallery = ({
   const activeImage = photos && activeIndex != -1 && photos[activeIndex]
 
   const getPhotoElements = updateSidebar => {
-    let photoElements = null
+    let photoElements = []
     if (photos) {
       photos.filter(photo => photo.thumbnail)
 
@@ -91,6 +93,10 @@ const PhotoGallery = ({
           />
         )
       })
+    } else {
+      for (let i = 0; i < 6; i++) {
+        photoElements.push(<PhotoThumbnail />)
+      }
     }
 
     return photoElements
