2022-12-28 12:02:43 -05:00
|
|
|
import { useCallback } from 'react'
|
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
2023-01-06 20:45:37 -05:00
|
|
|
import { type ProjectSettings, saveUserSettings } from '../utils/api'
|
|
|
|
import useSaveProjectSettings from './use-save-project-settings'
|
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')
|
2023-01-06 20:45:37 -05:00
|
|
|
const saveProjectSettings = useSaveProjectSettings()
|
2022-12-28 12:02:43 -05:00
|
|
|
|
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-10 10:17:39 -05:00
|
|
|
spellCheckLanguage != null &&
|
|
|
|
newSpellCheckLanguage !== spellCheckLanguage
|
2022-12-28 15:58:14 -05:00
|
|
|
|
|
|
|
if (allowUpdate) {
|
2023-01-04 07:17:50 -05:00
|
|
|
setSpellCheckLanguage(newSpellCheckLanguage)
|
2022-12-28 12:02:43 -05:00
|
|
|
|
2023-01-06 20:45:37 -05:00
|
|
|
// Save project settings is created from hooks because it will save the value on
|
|
|
|
// both server-side and client-side (angular scope)
|
|
|
|
saveProjectSettings('spellCheckLanguage', newSpellCheckLanguage)
|
|
|
|
|
|
|
|
// For user settings, we only need to save it on server-side,
|
|
|
|
// so we import the function directly without hooks
|
2023-01-06 09:29:03 -05:00
|
|
|
saveUserSettings('spellCheckLanguage', newSpellCheckLanguage)
|
2022-12-28 12:02:43 -05:00
|
|
|
}
|
|
|
|
},
|
2023-01-06 20:45:37 -05:00
|
|
|
[setSpellCheckLanguage, spellCheckLanguage, saveProjectSettings]
|
2022-12-28 12:02:43 -05:00
|
|
|
)
|
|
|
|
}
|