* 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
30 lines
629 B
TypeScript
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 }
|
|
}
|