Browse Source

Adjust followed channels on home page to meet design requirements (#1012)

Rafał Pawłow 3 years ago
parent
commit
54f149eee1

+ 8 - 3
src/components/InfiniteGrids/InfiniteVideoGrid.tsx

@@ -31,6 +31,7 @@ type InfiniteVideoGridProps = {
   showChannel?: boolean
   showChannel?: boolean
   className?: string
   className?: string
   currentlyWatchedVideoId?: string
   currentlyWatchedVideoId?: string
+  onDemand?: boolean
   additionalLink?: {
   additionalLink?: {
     name: string
     name: string
     url: string
     url: string
@@ -55,6 +56,7 @@ export const InfiniteVideoGrid: React.FC<InfiniteVideoGridProps> = ({
   showChannel = true,
   showChannel = true,
   className,
   className,
   currentlyWatchedVideoId,
   currentlyWatchedVideoId,
+  onDemand = false,
   additionalLink,
   additionalLink,
 }) => {
 }) => {
   const [videosPerRow, setVideosPerRow] = useState(INITIAL_VIDEOS_PER_ROW)
   const [videosPerRow, setVideosPerRow] = useState(INITIAL_VIDEOS_PER_ROW)
@@ -85,7 +87,7 @@ export const InfiniteVideoGrid: React.FC<InfiniteVideoGridProps> = ({
     }))
     }))
   }, [cachedCategoryId, targetRowsCount])
   }, [cachedCategoryId, targetRowsCount])
 
 
-  const { placeholdersCount, displayedItems, error, allItemsLoaded, loading } = useInfiniteGrid<
+  const { placeholdersCount, displayedItems, error, totalCount, loading } = useInfiniteGrid<
     GetVideosConnectionQuery,
     GetVideosConnectionQuery,
     GetVideosConnectionQuery['videosConnection'],
     GetVideosConnectionQuery['videosConnection'],
     GetVideosConnectionQueryVariables
     GetVideosConnectionQueryVariables
@@ -95,7 +97,8 @@ export const InfiniteVideoGrid: React.FC<InfiniteVideoGridProps> = ({
     skipCount,
     skipCount,
     queryVariables,
     queryVariables,
     targetRowsCount,
     targetRowsCount,
-    onDemand: true,
+    onDemand,
+    onScrollToBottom: !onDemand ? fetchMore : undefined,
     dataAccessor: (rawData) => {
     dataAccessor: (rawData) => {
       if (currentlyWatchedVideoId) {
       if (currentlyWatchedVideoId) {
         return (
         return (
@@ -157,6 +160,8 @@ export const InfiniteVideoGrid: React.FC<InfiniteVideoGridProps> = ({
     return null
     return null
   }
   }
 
 
+  const shouldShowLoadMoreButton = onDemand && !loading && displayedItems.length < totalCount
+
   // TODO: We should probably postpone doing first fetch until `onResize` gets called.
   // TODO: We should probably postpone doing first fetch until `onResize` gets called.
   // Right now we'll make the first request and then right after another one based on the resized columns
   // Right now we'll make the first request and then right after another one based on the resized columns
   return (
   return (
@@ -178,7 +183,7 @@ export const InfiniteVideoGrid: React.FC<InfiniteVideoGridProps> = ({
         </GridHeadingContainer>
         </GridHeadingContainer>
       )}
       )}
       <Grid onResize={(sizes) => setVideosPerRow(sizes.length)}>{gridContent}</Grid>
       <Grid onResize={(sizes) => setVideosPerRow(sizes.length)}>{gridContent}</Grid>
-      {!allItemsLoaded && !loading && (
+      {shouldShowLoadMoreButton && (
         <LoadMoreButtonWrapper>
         <LoadMoreButtonWrapper>
           <LoadMoreButton onClick={fetchMore} />
           <LoadMoreButton onClick={fetchMore} />
         </LoadMoreButtonWrapper>
         </LoadMoreButtonWrapper>

+ 5 - 2
src/components/InfiniteGrids/useInfiniteGrid.ts

@@ -35,10 +35,11 @@ type UseInfiniteGridParams<TRawData, TPaginatedData extends PaginatedData<unknow
   targetRowsCount: number
   targetRowsCount: number
   itemsPerRow: number
   itemsPerRow: number
   skipCount: number
   skipCount: number
-  onScrollToBottom: () => void
   onError?: (error: unknown) => void
   onError?: (error: unknown) => void
   queryVariables: TArgs
   queryVariables: TArgs
-} & ({ onDemand?: false; onScrollToBottom: () => void } | { onDemand: true; onScrollToBottom?: undefined })
+  onDemand?: boolean
+  onScrollToBottom?: () => void
+}
 
 
 type UseInfiniteGridReturn<TPaginatedData extends PaginatedData<unknown>> = {
 type UseInfiniteGridReturn<TPaginatedData extends PaginatedData<unknown>> = {
   displayedItems: TPaginatedData['edges'][0]['node'][]
   displayedItems: TPaginatedData['edges'][0]['node'][]
@@ -46,6 +47,7 @@ type UseInfiniteGridReturn<TPaginatedData extends PaginatedData<unknown>> = {
   error?: ApolloError
   error?: ApolloError
   allItemsLoaded: boolean
   allItemsLoaded: boolean
   loading: boolean
   loading: boolean
+  totalCount: number
 }
 }
 
 
 export const useInfiniteGrid = <
 export const useInfiniteGrid = <
@@ -143,5 +145,6 @@ export const useInfiniteGrid = <
     allItemsLoaded,
     allItemsLoaded,
     error,
     error,
     loading,
     loading,
+    totalCount: data?.totalCount || 0,
   }
   }
 }
 }

+ 4 - 17
src/views/viewer/HomeView.tsx

@@ -1,24 +1,18 @@
 import styled from '@emotion/styled'
 import styled from '@emotion/styled'
-import { sub } from 'date-fns'
 import React from 'react'
 import React from 'react'
 
 
 import useVideosConnection from '@/api/hooks/videosConnection'
 import useVideosConnection from '@/api/hooks/videosConnection'
 import {
 import {
   InfiniteVideoGrid,
   InfiniteVideoGrid,
-  InterruptedVideosGallery,
   LimitedWidthContainer,
   LimitedWidthContainer,
-  VideoHero,
   OfficialJoystreamUpdate,
   OfficialJoystreamUpdate,
+  VideoHero,
   ViewErrorFallback,
   ViewErrorFallback,
 } from '@/components'
 } from '@/components'
 import { usePersonalDataStore } from '@/providers'
 import { usePersonalDataStore } from '@/providers'
 import { transitions } from '@/shared/theme'
 import { transitions } from '@/shared/theme'
 import { SentryLogger } from '@/utils/logs'
 import { SentryLogger } from '@/utils/logs'
 
 
-const MIN_FOLLOWED_CHANNELS_VIDEOS = 16
-// last three months
-const MIN_DATE_FOLLOWED_CHANNELS_VIDEOS = sub(new Date(), { months: 3 })
-
 export const HomeView: React.FC = () => {
 export const HomeView: React.FC = () => {
   const followedChannels = usePersonalDataStore((state) => state.followedChannels)
   const followedChannels = usePersonalDataStore((state) => state.followedChannels)
 
 
@@ -29,15 +23,12 @@ export const HomeView: React.FC = () => {
     {
     {
       where: {
       where: {
         channelId_in: channelIdIn,
         channelId_in: channelIdIn,
-        createdAt_gte: MIN_DATE_FOLLOWED_CHANNELS_VIDEOS,
       },
       },
     },
     },
     { skip: !anyFollowedChannels, onError: (error) => SentryLogger.error('Failed to fetch videos', 'HomeView', error) }
     { skip: !anyFollowedChannels, onError: (error) => SentryLogger.error('Failed to fetch videos', 'HomeView', error) }
   )
   )
 
 
   const followedChannelsVideosCount = videosConnection?.totalCount
   const followedChannelsVideosCount = videosConnection?.totalCount
-  const shouldShowFollowedChannels =
-    followedChannelsVideosCount && followedChannelsVideosCount > MIN_FOLLOWED_CHANNELS_VIDEOS
 
 
   if (error) {
   if (error) {
     return <ViewErrorFallback />
     return <ViewErrorFallback />
@@ -47,13 +38,9 @@ export const HomeView: React.FC = () => {
     <LimitedWidthContainer big>
     <LimitedWidthContainer big>
       <VideoHero />
       <VideoHero />
       <Container className={transitions.names.slide}>
       <Container className={transitions.names.slide}>
-        <InterruptedVideosGallery />
-        <StyledInfiniteVideoGrid
-          title={shouldShowFollowedChannels ? 'Recent Videos From Followed Channels' : 'Recent Videos'}
-          channelIdIn={shouldShowFollowedChannels ? channelIdIn : null}
-          createdAtGte={shouldShowFollowedChannels ? MIN_DATE_FOLLOWED_CHANNELS_VIDEOS : null}
-          ready={!loading}
-        />
+        {!loading && followedChannelsVideosCount ? (
+          <StyledInfiniteVideoGrid title="Followed channels" channelIdIn={channelIdIn} ready={!loading} onDemand />
+        ) : null}
         <OfficialJoystreamUpdate />
         <OfficialJoystreamUpdate />
       </Container>
       </Container>
     </LimitedWidthContainer>
     </LimitedWidthContainer>