Skip to content

Commit 47633e7

Browse files
committed
fix: device detecetion
1 parent 8ebdd2c commit 47633e7

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/components/common/NarrowAd.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import React, { useEffect, useState } from 'react';
22
import styled from 'styled-components';
33
import media from '../../lib/styles/media';
4+
import { isTablet } from '../../lib/deviceDetection';
45

56
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-
}
7+
display: flex;
8+
justify-content: center;
9+
align-items: center;
10+
margin: 16px 0;
1311
`;
1412

1513
const AdBlock = styled.div<{ width: number; height: number }>`
@@ -48,7 +46,7 @@ function AdInsComponent({
4846
}
4947

5048
export default function NarrowAd() {
51-
const [isMobile, setIsMobile] = useState(false);
49+
const [isMobileOrTablet, setIsMobileOrTablet] = useState(false);
5250
const [mode, setMode] = useState<'wide' | 'narrow'>('wide');
5351

5452
useEffect(() => {
@@ -57,8 +55,9 @@ export default function NarrowAd() {
5755
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
5856
userAgent,
5957
);
58+
const checkIsTablet = isTablet();
6059

61-
setIsMobile(checkIsMobile);
60+
setIsMobileOrTablet(checkIsMobile || checkIsTablet);
6261

6362
// Set mode based on window width
6463
const windowWidth = window.innerWidth;
@@ -80,14 +79,12 @@ export default function NarrowAd() {
8079
}, []);
8180

8281
const size =
83-
mode === 'narrow'
84-
? { width: 300, height: 50 }
85-
: { width: 728, height: 90 };
82+
mode === 'narrow' ? { width: 300, height: 50 } : { width: 728, height: 90 };
8683
const adSlot = mode === 'narrow' ? '1778476944' : '5606751754';
8784

8885
return (
8986
<Wrapper>
90-
{isMobile && mode === 'narrow' && (
87+
{isMobileOrTablet && mode === 'narrow' && (
9188
<AdBlock width={size.width} height={size.height}>
9289
<AdInsComponent
9390
width={size.width}
@@ -96,7 +93,7 @@ export default function NarrowAd() {
9693
/>
9794
</AdBlock>
9895
)}
99-
{isMobile && mode === 'wide' && (
96+
{isMobileOrTablet && mode === 'wide' && (
10097
<AdBlock width={size.width} height={size.height}>
10198
<AdInsComponent
10299
width={size.width}

src/components/post/SideAd.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect, useState, useRef, useCallback } from 'react';
22
import styled from 'styled-components';
33
import media from '../../lib/styles/media';
44
import { getScrollTop } from '../../lib/utils';
5+
import { isTablet } from '../../lib/deviceDetection';
56

67
const Wrapper = styled.div`
78
position: relative;
@@ -80,10 +81,11 @@ export default function SideAd() {
8081
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
8182
userAgent,
8283
);
84+
const checkIsTablet = isTablet();
8385
const hasMinWidth = window.innerWidth >= 1200;
8486
const hasMinHeight = window.innerHeight >= 674;
8587

86-
setIsDesktop(!isMobile && hasMinWidth && hasMinHeight);
88+
setIsDesktop(!isMobile && !checkIsTablet && hasMinWidth && hasMinHeight);
8789

8890
// Set mode based on window height
8991
const windowHeight = window.innerHeight;
@@ -115,10 +117,11 @@ export default function SideAd() {
115117
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
116118
userAgent,
117119
);
120+
const checkIsTablet = isTablet();
118121
const hasMinWidth = windowWidth >= 1200;
119122
const hasMinHeight = windowHeight >= 674;
120123

121-
setIsDesktop(!isMobile && hasMinWidth && hasMinHeight);
124+
setIsDesktop(!isMobile && !checkIsTablet && hasMinWidth && hasMinHeight);
122125
};
123126

124127
window.addEventListener('resize', handleResize);

src/lib/deviceDetection.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function isIPad() {
2+
const ua = navigator.userAgent || navigator.vendor || (window as any).opera;
3+
const platform = navigator.platform;
4+
5+
// iPadOS 13 이상: MacIntel + 터치포인트 존재
6+
const iPadOS = platform === 'MacIntel' && navigator.maxTouchPoints > 1;
7+
8+
// iOS 12 이하 또는 구형 iPad 인식
9+
const legacyiPad = /iPad/.test(ua);
10+
11+
// 일부 iPad Pro는 iPhone처럼 나오는 경우도 있어서 iOS 디바이스 + 큰 화면 기준 추가
12+
const weirdSafariCase = /Macintosh/.test(ua) && 'ontouchend' in document;
13+
14+
return iPadOS || legacyiPad || weirdSafariCase;
15+
}
16+
17+
export function isTablet() {
18+
const ua = navigator.userAgent.toLowerCase();
19+
const checkIPad = isIPad();
20+
21+
return checkIPad || /tablet|(android(?!.*mobile))/i.test(ua);
22+
}

0 commit comments

Comments
 (0)