Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

970 B
Raw Blame
import { useQuery } from '@tanstack/react-query'

import { miscKeys } from './keys'
import { fetchHandler } from '@/data/fetchers'
import { BASE_PATH, IS_PLATFORM } from '@/lib/constants'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

export async function getUserIPAddress() {
  try {
    const data = await fetchHandler(`${BASE_PATH}/api/get-ip-address`).then((res) => res.json())
    return data.ipAddress
  } catch (error) {
    throw error
  }
}

export type UserIPAddressData = Awaited<ReturnType<typeof getUserIPAddress>>
export type UserIPAddressError = ResponseError

export const useUserIPAddressQuery = <TData = UserIPAddressData>({
  enabled = true,
  ...options
}: UseCustomQueryOptions<UserIPAddressData, UserIPAddressError, TData> = {}) =>
  useQuery<UserIPAddressData, UserIPAddressError, TData>({
    queryKey: miscKeys.ipAddress(),
    queryFn: () => getUserIPAddress(),
    enabled: enabled && !IS_PLATFORM,
    ...options,
  })