Files
supabase/packages/common/helpers.ts
Drew McLellan 773b4f767c fix(common): skip the reduced-motion query when matchMedia is missing (#53613)
## Problem

`Marketing site tests / build` fails on PRs that touch www, with
`window.matchMedia is not a function` in `lib/sentry-capture.test.tsx`.

`packages/common/helpers.ts` calls `window.matchMedia` when it is
imported. jsdom does not implement it. #53294 made
`common/consent-state` import `posthog-client`, which reaches
`helpers.ts` through `telemetry-utils`. The www `sentry-browser` test
project runs in jsdom without a `matchMedia` stub, so it now crashes on
import.

The break went unnoticed on `master` because the www workflow only runs
on pull requests, and its path filter listed just two files in
`packages/common`.

This is the same problem several other places already work around:
`consent-state.ts` duplicates `ensurePlatformSuffix` to avoid the
import, and the `packages/common` and `kb` tests stub
`window.matchMedia` before importing.

## Solution

- Guard the call in `helpers.ts` with `typeof window.matchMedia ===
'function'`, so `useReducedMotion` returns `false` where the API is
missing. Browsers are unaffected.
- Add `packages/common/helpers.test.ts`. It imports `helpers` and
`consent-state` in jsdom without `matchMedia`, and checks that the query
is still read where it exists. Both import tests fail on `master` with
this error.
- Run the Marketing site tests on changes to `packages/common/**/*.ts*`
and its `package.json`, since www imports it widely. This part is
independent of the fix and can be split out if you prefer.

The `helpers.ts` change is identical to the one in #53589, so the two
merge cleanly whichever lands first. This earlier attempt, #53596, was
closed.

Not changed: the per-test `matchMedia` stubs. They're harmless, and can
be removed as a follow-up.

## Review instructions

1. From `packages/common`, run `pnpm exec vitest run helpers.test.ts`
and check that all 3 tests pass.
2. Run `git checkout origin/master -- packages/common/helpers.ts` and
run it again. Two tests should fail with `window.matchMedia is not a
function`. Then run `git checkout HEAD -- packages/common/helpers.ts`.
3. Check that `Marketing site tests / build` passes on this PR and ran
because of the `packages/common` path.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill (not applicable)
2026-11-18 14:30:07 +00:00

61 lines
1.7 KiB
TypeScript

'use client'
import { useSyncExternalStore } from 'react'
import type * as React from 'react'
export const detectBrowser = () => {
if (!navigator) return undefined
if (navigator.userAgent.indexOf('Chrome') !== -1) {
return 'Chrome'
} else if (navigator.userAgent.indexOf('Firefox') !== -1) {
return 'Firefox'
} else if (navigator.userAgent.indexOf('Safari') !== -1) {
return 'Safari'
}
}
export const isBrowser = typeof window !== 'undefined'
const prefersReducedMotionMediaQuery =
isBrowser &&
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-reduced-motion: reduce)')
/**
* @returns boolean value If the user has expressed their preference for reduced motion.
*/
export const useReducedMotion = (): boolean => {
if (!prefersReducedMotionMediaQuery) return false
return useSyncExternalStore(
(callback) => {
prefersReducedMotionMediaQuery.addEventListener('change', callback)
return () => {
prefersReducedMotionMediaQuery.removeEventListener('change', callback)
}
},
() => prefersReducedMotionMediaQuery.matches,
() => false
)
}
export function ensurePlatformSuffix(apiUrl: string) {
let base = apiUrl
while (base.endsWith('/')) base = base.slice(0, -1)
return base.endsWith('/platform') ? base : `${base}/platform`
}
export function mergeRefs<T>(...refs: React.Ref<T>[]): React.RefCallback<T> {
return (value) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(value)
} else if (ref !== null) {
if (typeof ref === 'object' && ref !== null && 'current' in ref) {
;(ref as any).current = value
}
}
})
}
}