use-set-spell-check-language.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { useCallback } from 'react'
  2. import useScopeValue from '../../../shared/hooks/use-scope-value'
  3. import { type ProjectSettings, saveUserSettings } from '../utils/api'
  4. import useSaveProjectSettings from './use-save-project-settings'
  5. export default function useSetSpellCheckLanguage() {
  6. const [spellCheckLanguage, setSpellCheckLanguage] = useScopeValue<
  7. ProjectSettings['spellCheckLanguage']
  8. >('project.spellCheckLanguage')
  9. const saveProjectSettings = useSaveProjectSettings()
  10. return useCallback(
  11. (newSpellCheckLanguage: ProjectSettings['spellCheckLanguage']) => {
  12. const allowUpdate =
  13. spellCheckLanguage != null &&
  14. newSpellCheckLanguage !== spellCheckLanguage
  15. if (allowUpdate) {
  16. setSpellCheckLanguage(newSpellCheckLanguage)
  17. // Save project settings is created from hooks because it will save the value on
  18. // both server-side and client-side (angular scope)
  19. saveProjectSettings('spellCheckLanguage', newSpellCheckLanguage)
  20. // For user settings, we only need to save it on server-side,
  21. // so we import the function directly without hooks
  22. saveUserSettings('spellCheckLanguage', newSpellCheckLanguage)
  23. }
  24. },
  25. [setSpellCheckLanguage, spellCheckLanguage, saveProjectSettings]
  26. )
  27. }