You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.2 KiB
37 lines
1.2 KiB
import { useEffect, useRef, EffectCallback } from 'react'
|
|
import { useSelector } from 'react-redux'
|
|
import { useHistory } from 'react-router-dom'
|
|
import isEqual from 'lodash/isEqual'
|
|
|
|
import { getAuthenticated, getChecked } from 'src/selectors/authentication'
|
|
import { getTheme } from 'src/selectors/theme'
|
|
import { getConfig } from 'src/selectors'
|
|
import { AppState, Theme, Config } from 'src/types'
|
|
|
|
export const useAuthenticationCheck = () => {
|
|
const checked = useSelector<AppState, boolean>(getChecked)
|
|
const authenticated = useSelector<AppState, boolean>(getAuthenticated)
|
|
const history = useHistory()
|
|
|
|
useEffect(() => {
|
|
if (checked && !authenticated) history.push('/login')
|
|
}, [checked, authenticated])
|
|
}
|
|
|
|
export const useConfig = () => useSelector<AppState, Config>(getConfig)
|
|
|
|
const useDeepCompareMemoize = (value: any) => {
|
|
const ref = useRef()
|
|
|
|
if (!isEqual(value, ref.current)) {
|
|
ref.current = value
|
|
}
|
|
|
|
return ref.current
|
|
}
|
|
|
|
export const useDeepCompareEffect = (callback: EffectCallback, deps?: readonly any[] | undefined) => {
|
|
useEffect(callback, useDeepCompareMemoize(deps))
|
|
}
|
|
|
|
export const useTheme = () => useSelector<AppState, Theme>(getTheme)
|