feat: 앱인토스 토스판 빌드(보기 전용, 위치·공유 SDK 분기, appName nalssigak)
This commit is contained in:
1 parent
1fa0695c84
commit
7451599727
16 files changed
+1704
-58
No files matched your search
+33
-15
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useState, type ReactNode } from 'react
|
||||
import { nearestRegion } from '../api/client/sdk.gen'
|
||||
import type { RegionData } from '../api/client/types.gen'
|
||||
import { track } from '../lib/analytics'
|
||||
import { IS_TOSS, tossLocation } from '../lib/platform'
|
||||
import { DEFAULT_REGION, RegionContext, type LocateStatus } from './regionContext'
|
||||
|
||||
const KEY = 'nalssigak.b.region'
|
||||
@@ -28,7 +29,7 @@ function writeStored(r: RegionData) {
|
||||
export function RegionProvider({ children }: { children: ReactNode }) {
|
||||
const [stored] = useState(readStored)
|
||||
const [region, setRegionState] = useState<RegionData>(stored ?? DEFAULT_REGION)
|
||||
const canLocate = typeof navigator !== 'undefined' && 'geolocation' in navigator
|
||||
const canLocate = IS_TOSS || (typeof navigator !== 'undefined' && 'geolocation' in navigator)
|
||||
const [locateStatus, setLocateStatus] = useState<LocateStatus>(!stored && canLocate ? 'locating' : 'idle')
|
||||
|
||||
const setRegion = useCallback((r: RegionData) => {
|
||||
@@ -36,23 +37,40 @@ export function RegionProvider({ children }: { children: ReactNode }) {
|
||||
writeStored(r)
|
||||
}, [])
|
||||
|
||||
const findNearest = useCallback((onFound?: () => void) => {
|
||||
const fromCoords = useCallback((lat: number, lon: number, onFound?: () => void) => {
|
||||
nearestRegion({ query: { lat, lon } })
|
||||
.then(({ data: res }) => {
|
||||
if (!res.region) throw new Error('가까운 구를 찾지 못함')
|
||||
setRegion(res.region)
|
||||
setLocateStatus('idle')
|
||||
track('region_change', { region_name: res.region.name, method: 'location' })
|
||||
onFound?.()
|
||||
})
|
||||
.catch(() => setLocateStatus('error'))
|
||||
}, [setRegion])
|
||||
|
||||
// ask=false 는 처음 들어왔을 때: 토스에서는 권한 창을 띄우지 않고, 이미 허용한 사람만 위치로 맞춘다
|
||||
const findNearest = useCallback((onFound?: () => void, ask = true) => {
|
||||
if (IS_TOSS) {
|
||||
tossLocation(ask).then(
|
||||
(loc) => {
|
||||
if (loc === null) setLocateStatus('idle')
|
||||
else if (loc === 'denied') setLocateStatus('denied')
|
||||
else fromCoords(loc.lat, loc.lon, onFound)
|
||||
},
|
||||
(e: unknown) => {
|
||||
console.error('[region] toss location', e)
|
||||
setLocateStatus('error')
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(pos) => {
|
||||
nearestRegion({ query: { lat: pos.coords.latitude, lon: pos.coords.longitude } })
|
||||
.then(({ data: res }) => {
|
||||
if (!res.region) throw new Error('가까운 구를 찾지 못함')
|
||||
setRegion(res.region)
|
||||
setLocateStatus('idle')
|
||||
track('region_change', { region_name: res.region.name, method: 'location' })
|
||||
onFound?.()
|
||||
})
|
||||
.catch(() => setLocateStatus('error'))
|
||||
},
|
||||
(pos) => fromCoords(pos.coords.latitude, pos.coords.longitude, onFound),
|
||||
(err) => setLocateStatus(err.code === err.PERMISSION_DENIED ? 'denied' : 'error'),
|
||||
{ timeout: 10000, maximumAge: 600000 },
|
||||
)
|
||||
}, [setRegion])
|
||||
}, [fromCoords])
|
||||
|
||||
const locate = useCallback((onFound?: () => void) => {
|
||||
if (!canLocate) {
|
||||
@@ -65,7 +83,7 @@ export function RegionProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
// 처음 온 사람만 위치 권한을 물어 가장 가까운 구로 맞춘다
|
||||
useEffect(() => {
|
||||
if (!stored && canLocate) findNearest()
|
||||
if (!stored && canLocate) findNearest(undefined, false)
|
||||
}, [stored, canLocate, findNearest])
|
||||
|
||||
const api = useMemo(() => ({ region, setRegion, locate, locateStatus }), [region, setRegion, locate, locateStatus])
|
||||
|
||||
Reference in new issue
Block a user