Files
supabase/apps/studio/data/misc/clock-skew-query.ts
Danny White c2231301e0 chore(studio): improve HeaderBanner (#41525)
* design polish

* better banner

* defensive truncation

* better incident banner

* better prop names

* improve warnings

* fix variant

* OrganizationResourceBanner

* notice banner

* improve ClockSkewBanner

* add ARIA label

* rabbit

* lil dot

* 📝 Add docstrings to `dnywh/chore/improve-header-banner` (#41526)

* 📝 Add docstrings to `dnywh/chore/improve-header-banner`

Docstrings generation was requested by @dnywh.

* https://github.com/supabase/supabase/pull/41525#issuecomment-3680124020

The following files were modified:

* `apps/studio/hooks/misc/useOrganizationRestrictions.ts`

* new line

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>

* fix clock docs link

* Small nits

* Fix URL for grace period warning to point to usage instead of billing

* rabbit

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-12-29 15:16:10 +11:00

40 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { fetchHandler } from 'data/fetchers'
import { BASE_PATH, IS_PLATFORM } from 'lib/constants'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { miscKeys } from './keys'
// Warn if the clock skew is greater than 2 minutes
const CLOCK_SKEW_THRESHOLD = 2 * 60 * 1000
// Check every 30 minutes
const CLOCK_SKEW_CHECK_INTERVAL = 30 * 60 * 1000
export async function getClockSkew() {
try {
const data = await fetchHandler(`${BASE_PATH}/api/get-utc-time`).then((res) => res.json())
const serverTime = new Date(data.utcTime).getTime()
const clientTime = new Date().getTime()
const clockSkew = Math.abs(clientTime - serverTime)
return clockSkew > CLOCK_SKEW_THRESHOLD
} catch (error) {
throw error
}
}
export type ClockSkewData = Awaited<ReturnType<typeof getClockSkew>>
export type ClockSkewError = ResponseError
export const useClockSkewQuery = <TData = ClockSkewData>({
enabled = true,
refetchInterval = CLOCK_SKEW_CHECK_INTERVAL,
...options
}: UseCustomQueryOptions<ClockSkewData, ClockSkewError, TData> = {}) =>
useQuery<ClockSkewData, ClockSkewError, TData>({
queryKey: miscKeys.clockSkew(),
queryFn: () => getClockSkew(),
enabled: enabled && IS_PLATFORM,
refetchInterval,
...options,
})