[ABANDONED] React/Redux front end for the Flexor social network.
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.
 
 

46 lines
1.6 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, getAuthenticatedUser } from 'src/selectors/authentication'
import { getTheme } from 'src/selectors/theme'
import { getConfig } from 'src/selectors'
import { AppState, Theme, Config, User } 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)
export function useSetting<T>(key: string): T | undefined
export function useSetting<T>(key: string, defaultValue: T): T
export function useSetting<T>(key: string, defaultValue?: T): T | undefined {
const user = useSelector<AppState, User | undefined>(getAuthenticatedUser)
if (!user || !user.settings) return defaultValue
return user.settings[key] ?? defaultValue
}