HomeView.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import styled from '@emotion/styled'
  2. import { ErrorBoundary } from '@sentry/react'
  3. import { sub } from 'date-fns'
  4. import React from 'react'
  5. import useVideosConnection from '@/api/hooks/videosConnection'
  6. import { CoverVideo, ErrorFallback, InfiniteVideoGrid, InterruptedVideosGallery, ViewWrapper } from '@/components'
  7. import { usePersonalData } from '@/providers'
  8. import { transitions } from '@/shared/theme'
  9. const MIN_FOLLOWED_CHANNELS_VIDEOS = 16
  10. // last three months
  11. const MIN_DATE_FOLLOWED_CHANNELS_VIDEOS = sub(new Date(), { months: 3 })
  12. export const HomeView: React.FC = () => {
  13. const {
  14. state: { followedChannels },
  15. } = usePersonalData()
  16. const channelIdIn = followedChannels.map((channel) => channel.id)
  17. const anyFollowedChannels = channelIdIn.length > 0
  18. const { videosConnection, loading, error } = useVideosConnection(
  19. {
  20. where: {
  21. channelId_in: channelIdIn,
  22. createdAt_gte: MIN_DATE_FOLLOWED_CHANNELS_VIDEOS,
  23. },
  24. },
  25. { skip: !anyFollowedChannels }
  26. )
  27. const followedChannelsVideosCount = videosConnection?.totalCount
  28. const shouldShowFollowedChannels =
  29. followedChannelsVideosCount && followedChannelsVideosCount > MIN_FOLLOWED_CHANNELS_VIDEOS
  30. if (error) {
  31. throw error
  32. }
  33. return (
  34. <ViewWrapper>
  35. <CoverVideo />
  36. <Container className={transitions.names.slide}>
  37. <InterruptedVideosGallery />
  38. <ErrorBoundary fallback={ErrorFallback}>
  39. <StyledInfiniteVideoGrid
  40. title={shouldShowFollowedChannels ? 'Recent Videos From Followed Channels' : 'Recent Videos'}
  41. channelIdIn={shouldShowFollowedChannels ? channelIdIn : null}
  42. createdAtGte={shouldShowFollowedChannels ? MIN_DATE_FOLLOWED_CHANNELS_VIDEOS : null}
  43. ready={!loading}
  44. />
  45. </ErrorBoundary>
  46. </Container>
  47. </ViewWrapper>
  48. )
  49. }
  50. const Container = styled.div`
  51. position: relative;
  52. & > * {
  53. margin-bottom: 3rem;
  54. }
  55. `
  56. const StyledInfiniteVideoGrid = styled(InfiniteVideoGrid)`
  57. margin: 0;
  58. padding-bottom: 4rem;
  59. `