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

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