Skip to content

Commit 7e20864

Browse files
authored
Merge pull request #88 from DDD-Community/feat/#46
[feat/#46] 알림 수신 불가능한 현상 수정, 알림 꺼도 계속 수신되는 현상 수정
2 parents 19dcc0a + 42e6238 commit 7e20864

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/components/PoseDetector.tsx

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const PoseDetector: React.FC = () => {
2929
const [isModelLoaded, setIsModelLoaded] = useState<boolean>(false)
3030
// const [isSnapShotSaved, setIsSnapSaved] = useState<boolean>(false)
3131

32-
const { showNotification } = usePushNotification()
32+
const { showNotification, hasPermission: hasNotiPermisson } = usePushNotification()
3333

3434
const { isPopupOpen, handleClosePopup, openPopup } = useGuidePopup()
3535

@@ -46,7 +46,7 @@ const PoseDetector: React.FC = () => {
4646
const shoulderTwistCnt = useRef<number>(0)
4747
const chinUtpCnt = useRef<number>(0)
4848
const tailboneSitCnt = useRef<number>(0)
49-
49+
const isShowImmediNotiRef = useRef<boolean>(false)
5050
const canvasRef = useRef<HTMLCanvasElement>(null)
5151

5252
const { isSnapShotSaved, snapshot, setSnapShot } = useSnapShotStore()
@@ -56,7 +56,6 @@ const PoseDetector: React.FC = () => {
5656
// const userNoti = useNotificationStore((state) => state.notification)
5757
const { notification } = useNotification()
5858

59-
const { requestNotificationPermission } = usePushNotification()
6059
const { hasPermission } = useCameraPermission()
6160

6261
const location = useLocation() // 페이지 이동 감지
@@ -119,8 +118,7 @@ const PoseDetector: React.FC = () => {
119118
timerRef: React.MutableRefObject<any>,
120119
poseType: poseType,
121120
isSnapShotSaved: boolean,
122-
cntRef: React.MutableRefObject<any>,
123-
isShowNoti: boolean | undefined
121+
cntRef: React.MutableRefObject<any>
124122
): void => {
125123
if (condition && isSnapShotSaved) {
126124
if (!timerRef.current) {
@@ -130,7 +128,7 @@ const PoseDetector: React.FC = () => {
130128
const req = { snapshot: { keypoints, score }, type: poseType }
131129
sendPoseMutation.mutate(req)
132130
cntRef.current = cntRef.current + 1
133-
if (isShowNoti)
131+
if (isShowImmediNotiRef.current)
134132
showNotification(`척추 건강 위험! ${getPoseName(poseType)} 감지! 자세를 바르게 앉아주세요.`)
135133
}
136134
}, 30 * 1000)
@@ -151,25 +149,17 @@ const PoseDetector: React.FC = () => {
151149
const _isTextNeck = detectTextNeck(snapRef.current, results, true, 0.88)
152150
const _isHandOnChin = detectHandOnChin(snapRef.current, results)
153151
const _isTailboneSit = detectTailboneSit(snapRef.current, results)
154-
const _isShowNoti = notification?.duration === "IMMEDIATELY" && notification?.isActive
155152

156153
if (_isShoulderTwist !== null) setIsShoulderTwist(_isShoulderTwist)
157154
if (_isTextNeck !== null) setIsTextNeck(_isTextNeck)
158155
if (_isHandOnChin !== null) setIsHandOnChin(_isHandOnChin)
159156
if (_isTailboneSit !== null) setIsTailboneSit(_isTailboneSit)
160157

161158
// 공통 타이머 관리 함수 호출
162-
managePoseTimer(_isTextNeck, turtleNeckTimer, "TURTLE_NECK", isSnapShotSaved, turtleNeckCnt, _isShowNoti)
163-
managePoseTimer(
164-
_isShoulderTwist,
165-
shoulderTwistTimer,
166-
"SHOULDER_TWIST",
167-
isSnapShotSaved,
168-
shoulderTwistCnt,
169-
_isShowNoti
170-
)
171-
managePoseTimer(_isTailboneSit, tailboneSitTimer, "TAILBONE_SIT", isSnapShotSaved, tailboneSitCnt, _isShowNoti)
172-
managePoseTimer(_isHandOnChin, chinUtpTimer, "CHIN_UTP", isSnapShotSaved, chinUtpCnt, _isShowNoti)
159+
managePoseTimer(_isTextNeck, turtleNeckTimer, "TURTLE_NECK", isSnapShotSaved, turtleNeckCnt)
160+
managePoseTimer(_isShoulderTwist, shoulderTwistTimer, "SHOULDER_TWIST", isSnapShotSaved, shoulderTwistCnt)
161+
managePoseTimer(_isTailboneSit, tailboneSitTimer, "TAILBONE_SIT", isSnapShotSaved, tailboneSitCnt)
162+
managePoseTimer(_isHandOnChin, chinUtpTimer, "CHIN_UTP", isSnapShotSaved, chinUtpCnt)
173163
const isRight = !_isTextNeck && !_isHandOnChin && !_isShoulderTwist && !_isTailboneSit
174164
if (canvasRef.current) drawPose(results, canvasRef.current, isRight)
175165
} else {
@@ -288,7 +278,6 @@ const PoseDetector: React.FC = () => {
288278
}, [location])
289279

290280
useEffect(() => {
291-
requestNotificationPermission()
292281
getScript()
293282
}, [])
294283

@@ -328,6 +317,12 @@ const PoseDetector: React.FC = () => {
328317
}
329318
}, [notification, isSnapShotSaved])
330319

320+
// 즉시 알림을 사용 하는 경우, 푸시를 보낼지 여부를 저장
321+
useEffect(() => {
322+
isShowImmediNotiRef.current =
323+
notification?.duration === "IMMEDIATELY" && (notification?.isActive as boolean) && hasNotiPermisson
324+
}, [notification?.duration, notification?.isActive, hasNotiPermisson])
325+
331326
// 팝업 열기
332327
const handleShowPopup = (): void => {
333328
openPopup()

src/hooks/usePushNotification.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react"
1+
import { useState, useEffect, useRef } from "react"
22

33
interface UsePushNotificationResult {
44
hasPermission: boolean
@@ -12,6 +12,14 @@ const usePushNotification = (): UsePushNotificationResult => {
1212
const [hasPermission, setHasPermission] = useState(false) // 권한이 허용되었는지 여부
1313
const [isPermissionDenied, setIsPermissionDenied] = useState(false) // 권한이 거부되었는지 여부
1414

15+
// 최신 상태 추적용 useRef
16+
const hasPermissionRef = useRef(hasPermission)
17+
18+
useEffect(() => {
19+
// 최신 hasPermission 값을 항상 유지
20+
hasPermissionRef.current = hasPermission
21+
}, [hasPermission])
22+
1523
// 권한 변경을 처리하는 함수
1624
const handlePermissionChange = (permission: NotificationPermission): void => {
1725
if (permission === "granted") {
@@ -42,7 +50,7 @@ const usePushNotification = (): UsePushNotificationResult => {
4250

4351
// 알림 표시 함수
4452
const showNotification = (body: string): void => {
45-
if (hasPermission) {
53+
if (hasPermissionRef.current) {
4654
new Notification("자세공작소", {
4755
body: body,
4856
})

0 commit comments

Comments
 (0)