summaryrefslogtreecommitdiff
path: root/src/components/ui/HeroBannerSecondary.jsx
blob: 50db69f84f32b1e13f1f41bc69c80d0c3cbadc20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Link from '@/core/components/elements/Link/Link'
import { getRandomInt } from '@/utils/getRandomInt'
import Image from 'next/image'
import { useMemo } from 'react'
import { useQuery } from 'react-query'
import { HeroBannerSkeleton } from '../skeleton/BannerSkeleton'
import { bannerApi } from '@/api/bannerApi'
import AnimateOnLoad from './AnimateOnLoad'

const HeroBannerSecondary = () => {
  const heroBannerSecondary = useQuery('heroBannerSecondary', bannerApi({ type: 'index-a-2' }))

  const randomIndex = useMemo(() => {
    if (!heroBannerSecondary.data) return null
    const length = heroBannerSecondary.data?.length
    return getRandomInt(length)
  }, [heroBannerSecondary.data])

  if (heroBannerSecondary.isLoading) return <HeroBannerSkeleton />

  return (
    heroBannerSecondary.data && (
      <AnimateOnLoad className='h-full'>
        <Link href={heroBannerSecondary.data[randomIndex].url} className='h-full'>
          <Image
            src={heroBannerSecondary.data[randomIndex].image}
            width={512}
            height={1024}
            alt={heroBannerSecondary.data[randomIndex].name}
            className='object-cover object-center h-full'
          />
        </Link>
      </AnimateOnLoad>
    )
  )
}

export default HeroBannerSecondary