1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/components/hooks/useSession.ts
Kevin Heis 04f38e4779 Parse color_mode cookie in browser (#29738)
* Parse color_mode cookie in browser

* Update useTheme.ts

* Update use-theme.js

* Update use-theme.js

* Add support for dark_high_contrast

Per https://primer.style/css/support/theming

I also checked all the color mode options, this is the only additional working with Primer 20 so far

* Remove gray bg

* Remove  type

* Use defaults instead of types for fn args

* Thicker types
2022-08-09 16:48:18 +00:00

30 lines
629 B
TypeScript

import { useEffect } from 'react'
import useSWR from 'swr'
export default async function fetcher<JSON = any>(
input: RequestInfo,
init?: RequestInit
): Promise<JSON> {
const res = await fetch(input, init)
return res.json()
}
export type Session = {
isSignedIn: boolean
csrfToken?: string
userLanguage: string // en, es, ja, cn
}
// React hook version
export function useSession() {
const { data: session, error } = useSWR<Session>('/api/session', fetcher)
useEffect(() => {
if (error) {
console.warn('An error occurred loading the user session', error)
}
}, [error])
return { session }
}