overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-set-spell-check-language.tsx
M Fahru af01ace5a0 refactor function by simplifying function argument (from two arguments to a single object)
GitOrigin-RevId: e4244a07a4ca196afa226e3fac9de74d30f7d4ea
2023-01-11 09:07:59 +00:00

34 lines
1.2 KiB
TypeScript

import { useCallback } from 'react'
import { sendMB } from '../../../infrastructure/event-tracking'
import { useProjectContext } from '../../../shared/context/project-context'
import useScopeValue from '../../../shared/hooks/use-scope-value'
import { saveProjectSettings, saveUserSettings } from '../utils/api'
export default function useSetSpellCheckLanguage() {
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
useScopeValue<string>('project.spellCheckLanguage')
const { _id: projectId } = useProjectContext()
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: string) => {
if (
spellCheckLanguageScope &&
spellCheckLanguage !== spellCheckLanguageScope
) {
sendMB('setting-changed', {
changedSetting: 'spellCheckLanguage',
changedSettingVal: spellCheckLanguage,
})
setSpellCheckLanguageScope(spellCheckLanguage)
// save to both project setting and user setting
saveProjectSettings({ projectId, spellCheckLanguage })
saveUserSettings({ spellCheckLanguage })
}
},
[projectId, setSpellCheckLanguageScope, spellCheckLanguageScope]
)
return setSpellCheckLanguage
}