|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import media from '../../lib/styles/media'; |
| 4 | + |
| 5 | +const Wrapper = styled.div` |
| 6 | + display: none; |
| 7 | + ${media.medium} { |
| 8 | + display: flex; |
| 9 | + justify-content: center; |
| 10 | + align-items: center; |
| 11 | + margin: 16px 0; |
| 12 | + } |
| 13 | +`; |
| 14 | + |
| 15 | +const AdBlock = styled.div<{ width: number; height: number }>` |
| 16 | + width: ${(props) => props.width}px; |
| 17 | + height: ${(props) => props.height}px; |
| 18 | + display: flex; |
| 19 | + justify-content: center; |
| 20 | + align-items: center; |
| 21 | +`; |
| 22 | + |
| 23 | +function AdInsComponent({ |
| 24 | + width, |
| 25 | + height, |
| 26 | + slot, |
| 27 | +}: { |
| 28 | + width: number; |
| 29 | + height: number; |
| 30 | + slot: string; |
| 31 | +}) { |
| 32 | + useEffect(() => { |
| 33 | + (window.adsbygoogle = window.adsbygoogle || []).push({}); |
| 34 | + }, []); |
| 35 | + |
| 36 | + return ( |
| 37 | + <ins |
| 38 | + className="adsbygoogle" |
| 39 | + style={{ |
| 40 | + display: 'inline-block', |
| 41 | + width: `${width}px`, |
| 42 | + height: `${height}px`, |
| 43 | + }} |
| 44 | + data-ad-client="ca-pub-5574866530496701" |
| 45 | + data-ad-slot={slot} |
| 46 | + ></ins> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +export default function NarrowAd() { |
| 51 | + const [isMobile, setIsMobile] = useState(false); |
| 52 | + const [mode, setMode] = useState<'wide' | 'narrow'>('wide'); |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + const userAgent = navigator.userAgent.toLowerCase(); |
| 56 | + const checkIsMobile = |
| 57 | + /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test( |
| 58 | + userAgent, |
| 59 | + ); |
| 60 | + |
| 61 | + setIsMobile(checkIsMobile); |
| 62 | + |
| 63 | + // Set mode based on window width |
| 64 | + const windowWidth = window.innerWidth; |
| 65 | + setMode(windowWidth < 768 ? 'narrow' : 'wide'); |
| 66 | + }, []); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const handleResize = () => { |
| 70 | + const windowWidth = window.innerWidth; |
| 71 | + |
| 72 | + // Update mode based on width |
| 73 | + setMode(windowWidth < 768 ? 'narrow' : 'wide'); |
| 74 | + }; |
| 75 | + |
| 76 | + window.addEventListener('resize', handleResize); |
| 77 | + return () => { |
| 78 | + window.removeEventListener('resize', handleResize); |
| 79 | + }; |
| 80 | + }, []); |
| 81 | + |
| 82 | + const size = |
| 83 | + mode === 'narrow' |
| 84 | + ? { width: 300, height: 50 } |
| 85 | + : { width: 728, height: 90 }; |
| 86 | + const adSlot = mode === 'narrow' ? '1778476944' : '5606751754'; |
| 87 | + |
| 88 | + return ( |
| 89 | + <Wrapper> |
| 90 | + {isMobile && mode === 'narrow' && ( |
| 91 | + <AdBlock width={size.width} height={size.height}> |
| 92 | + <AdInsComponent |
| 93 | + width={size.width} |
| 94 | + height={size.height} |
| 95 | + slot={adSlot} |
| 96 | + /> |
| 97 | + </AdBlock> |
| 98 | + )} |
| 99 | + {isMobile && mode === 'wide' && ( |
| 100 | + <AdBlock width={size.width} height={size.height}> |
| 101 | + <AdInsComponent |
| 102 | + width={size.width} |
| 103 | + height={size.height} |
| 104 | + slot={adSlot} |
| 105 | + /> |
| 106 | + </AdBlock> |
| 107 | + )} |
| 108 | + </Wrapper> |
| 109 | + ); |
| 110 | +} |
0 commit comments