21 lines
974 B
TypeScript
21 lines
974 B
TypeScript
import { createContext, use } from 'react'
|
|
import regionPages from '../../seo/region.json'
|
|
import type { RegionData } from '../api/client/types.gen'
|
|
|
|
export type LocateStatus = 'idle' | 'locating' | 'denied' | 'error'
|
|
|
|
export type RegionApi = { region: RegionData; setRegion: (r: RegionData) => void; locate: (onFound?: () => void) => void; locateStatus: LocateStatus }
|
|
|
|
export const DEFAULT_REGION: RegionData = { code: '1144000000', name: '서울 마포구', short_name: '마포구', lat: 37.56071, lon: 126.91053 }
|
|
|
|
/** 구 하나만 보여 주는 지역 페이지: 일반 시군구, 또는 구로 나뉜 시의 구 페이지 */
|
|
export const ownPage = (code: string) => regionPages.find((p) => p.regions.length === 1 && p.regions[0].code === code)
|
|
|
|
export const RegionContext = createContext<RegionApi | null>(null)
|
|
|
|
export function useRegion() {
|
|
const ctx = use(RegionContext)
|
|
if (!ctx) throw new Error('useRegion must be used inside RegionProvider')
|
|
return ctx
|
|
}
|