2022-12-28 12:02:43 -05:00
|
|
|
import { useCallback } from 'react'
|
2022-12-28 12:33:12 -05:00
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2022-12-28 12:02:43 -05:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
2023-01-03 12:46:37 -05:00
|
|
|
import {
|
2023-01-04 07:17:50 -05:00
|
|
|
type ProjectSettings,
|
2023-01-03 12:46:37 -05:00
|
|
|
saveProjectSettings,
|
|
|
|
saveUserSettings,
|
|
|
|
} from '../utils/api'
|
2022-12-28 12:02:43 -05:00
|
|
|
|
2023-01-03 12:04:34 -05:00
|
|
|
export default function useSetSpellCheckLanguage() {
|
2023-01-04 07:17:50 -05:00
|
|
|
const [spellCheckLanguage, setSpellCheckLanguage] = useScopeValue<
|
|
|
|
ProjectSettings['spellCheckLanguage']
|
2023-01-03 12:46:37 -05:00
|
|
|
>('project.spellCheckLanguage')
|
2022-12-28 12:02:43 -05:00
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
2023-01-04 07:17:50 -05:00
|
|
|
return useCallback(
|
|
|
|
(newSpellCheckLanguage: ProjectSettings['spellCheckLanguage']) => {
|
2022-12-28 15:58:14 -05:00
|
|
|
const allowUpdate =
|
2023-01-04 07:17:50 -05:00
|
|
|
newSpellCheckLanguage && newSpellCheckLanguage !== spellCheckLanguage
|
2022-12-28 15:58:14 -05:00
|
|
|
|
|
|
|
if (allowUpdate) {
|
2022-12-28 12:33:12 -05:00
|
|
|
sendMB('setting-changed', {
|
|
|
|
changedSetting: 'spellCheckLanguage',
|
2023-01-04 07:17:50 -05:00
|
|
|
changedSettingVal: newSpellCheckLanguage,
|
2022-12-28 12:33:12 -05:00
|
|
|
})
|
|
|
|
|
2023-01-04 07:17:50 -05:00
|
|
|
setSpellCheckLanguage(newSpellCheckLanguage)
|
2022-12-28 12:02:43 -05:00
|
|
|
|
|
|
|
// save to both project setting and user setting
|
2023-01-04 07:17:50 -05:00
|
|
|
saveProjectSettings({
|
|
|
|
projectId,
|
|
|
|
spellCheckLanguage: newSpellCheckLanguage,
|
|
|
|
})
|
|
|
|
saveUserSettings({ spellCheckLanguage: newSpellCheckLanguage })
|
2022-12-28 12:02:43 -05:00
|
|
|
}
|
|
|
|
},
|
2023-01-04 07:17:50 -05:00
|
|
|
[projectId, setSpellCheckLanguage, spellCheckLanguage]
|
2022-12-28 12:02:43 -05:00
|
|
|
)
|
|
|
|
}
|