overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-set-spell-check-language.tsx
M Fahru e3d0380e6f Remove multiple source of type imports; add type on type imports.
GitOrigin-RevId: a6630bb284c456bfbe61901f7331cdd3c13c7f8b
2023-01-11 09:08:42 +00:00

39 lines
1.3 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 {
type ProjectSettingsScope,
saveProjectSettings,
saveUserSettings,
} from '../utils/api'
export default function useSetSpellCheckLanguage() {
const [spellCheckLanguageScope, setSpellCheckLanguageScope] = useScopeValue<
ProjectSettingsScope['spellCheckLanguage']
>('project.spellCheckLanguage')
const { _id: projectId } = useProjectContext()
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage']) => {
const allowUpdate =
spellCheckLanguage && spellCheckLanguage !== spellCheckLanguageScope
if (allowUpdate) {
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
}