39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useEffect, useState, useCallback } from 'react'
|
|
import * as Notifications from 'expo-notifications'
|
|
import { getOpenSRCount } from '../services/api'
|
|
|
|
const REFRESH_MS = 5 * 60 * 1000 // 5분
|
|
|
|
/**
|
|
* 기능 #15 — 앱 아이콘 뱃지 카운트 훅
|
|
* 미처리(open) + 내 담당 SR 수를 5분마다 갱신하여 앱 뱃지에 반영.
|
|
* expo-notifications 플러그인은 app.json 에 등록하지 않고
|
|
* Notifications.setBadgeCountAsync 만 사용한다.
|
|
*/
|
|
export function useBadgeCount() {
|
|
const [count, setCount] = useState(0)
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
const res = await getOpenSRCount()
|
|
const c: number =
|
|
res.data?.total ??
|
|
res.data?.totalElements ??
|
|
(Array.isArray(res.data?.content) ? res.data.content.length : undefined) ??
|
|
(Array.isArray(res.data) ? res.data.length : 0)
|
|
setCount(c)
|
|
try { await Notifications.setBadgeCountAsync(c) } catch {}
|
|
} catch {
|
|
// 네트워크 오류 시 뱃지 유지
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
refresh()
|
|
const interval = setInterval(refresh, REFRESH_MS)
|
|
return () => clearInterval(interval)
|
|
}, [refresh])
|
|
|
|
return { count, refresh }
|
|
}
|