[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.

42 lines
1.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
  1. import { useEffect, useRef, EffectCallback } from 'react'
  2. import { useSelector } from 'react-redux'
  3. import { useHistory } from 'react-router-dom'
  4. import isEqual from 'lodash/isEqual'
  5. import { getAuthenticated, getChecked, getAuthenticatedUser } from '../selectors/authentication'
  6. import { getTheme } from '../selectors/theme'
  7. export const useAuthenticationCheck = () => {
  8. const checked = useSelector(getChecked)
  9. const authenticated = useSelector(getAuthenticated)
  10. const history = useHistory()
  11. useEffect(() => {
  12. if (checked && !authenticated) history.push('/login')
  13. }, [checked, authenticated])
  14. }
  15. const useDeepCompareMemoize = (value: any) => {
  16. const ref = useRef()
  17. if (!isEqual(value, ref.current)) {
  18. ref.current = value
  19. }
  20. return ref.current
  21. }
  22. export const useDeepCompareEffect = (callback: EffectCallback, deps?: readonly any[] | undefined) => {
  23. useEffect(callback, useDeepCompareMemoize(deps))
  24. }
  25. export const useTheme = () => useSelector(getTheme)
  26. export function useSetting<T>(key: string): T | undefined
  27. export function useSetting<T>(key: string, defaultValue: T): T
  28. export function useSetting<T>(key: string, defaultValue?: T): T | undefined {
  29. const user = useSelector(getAuthenticatedUser)
  30. if (!user || !user.settings) return defaultValue
  31. return user.settings[key] ?? defaultValue
  32. }