Migrate editor settings controller to react (this commit includes project-wide settings)

GitOrigin-RevId: 8c47a2699a2f11fda7fd60c5173bf2e29858e37a
This commit is contained in:
M Fahru 2022-12-22 11:42:28 -07:00 committed by Copybot
parent 67c8fa560e
commit f01a2920cf
3 changed files with 107 additions and 13 deletions

View file

@ -1,2 +1 @@
div(ng-controller="SettingsController")
editor-left-menu()
editor-left-menu()

View file

@ -11,15 +11,16 @@ import type {
ProjectCompiler,
} from '../../../../../types/project-settings'
import useScopeValue from '../../../shared/hooks/use-scope-value'
import useSetProjectWideSettings from '../hooks/use-set-project-wide-settings'
type ProjectSettingsContextValue = {
compiler: ProjectCompiler
compiler?: ProjectCompiler
setCompiler: (compiler: ProjectCompiler) => void
imageName: string
imageName?: string
setImageName: (imageName: string) => void
rootDocId: string
rootDocId?: string
setRootDocId: (rootDocId: string) => void
spellCheckLanguage: string
spellCheckLanguage?: string
setSpellCheckLanguage: (spellCheckLanguage: string) => void
autoComplete: boolean
setAutoComplete: (autoComplete: boolean) => void
@ -50,13 +51,17 @@ export const ProjectSettingsContext = createContext<
export function ProjectSettingsProvider({
children,
}: PropsWithChildren<Record<string, never>>) {
const [compiler, setCompiler] =
useScopeValue<ProjectCompiler>('project.compiler')
const [imageName, setImageName] = useScopeValue<string>('project.imageName')
const [rootDocId, setRootDocId] = useScopeValue<string>('project.rootDoc_id')
const [spellCheckLanguage, setSpellCheckLanguage] = useScopeValue<string>(
'project.spellCheckLanguage'
)
const {
compiler,
setCompiler,
imageName,
setImageName,
rootDocId,
setRootDocId,
spellCheckLanguage,
setSpellCheckLanguage,
} = useSetProjectWideSettings()
const [autoComplete, setAutoComplete] = useScopeValue<boolean>(
'settings.autoComplete'
)

View file

@ -0,0 +1,90 @@
import { useCallback } from 'react'
import { ProjectCompiler } from '../../../../../types/project-settings'
import { postJSON } from '../../../infrastructure/fetch-json'
import { useProjectContext } from '../../../shared/context/project-context'
import useScopeValue from '../../../shared/hooks/use-scope-value'
type ProjectScope = {
compiler: ProjectCompiler
imageName: string
rootDoc_id: string
spellCheckLanguage: string
}
function useSaveProjectSettings() {
const { _id: projectId } = useProjectContext()
const saveProject = useCallback(
(data: Partial<ProjectScope>) => {
postJSON(`/project/${projectId}/settings`, {
body: data,
})
},
[projectId]
)
return saveProject
}
// TODO: handle ignoreUpdates
export default function useSetProjectWideSettings() {
// The value will be undefined on mount
const [project, setProject] = useScopeValue<ProjectScope | undefined>(
'project',
true
)
const saveProject = useSaveProjectSettings()
const setCompiler = useCallback(
(compiler: ProjectCompiler) => {
if (project?.compiler) {
setProject({ ...project, compiler })
saveProject({ compiler })
}
},
[saveProject, project, setProject]
)
const setImageName = useCallback(
(imageName: string) => {
if (project?.imageName) {
setProject({ ...project, imageName })
saveProject({ imageName })
}
},
[saveProject, project, setProject]
)
// TODO
const setRootDocId = useCallback(
(rootDocId: string) => {
if (project?.imageName) {
setProject({ ...project, rootDoc_id: rootDocId })
// saveProject({ root })
}
},
[project, setProject]
)
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: string) => {
if (project?.spellCheckLanguage) {
setProject({ ...project, spellCheckLanguage })
saveProject({ spellCheckLanguage })
}
},
[saveProject, project, setProject]
)
return {
compiler: project?.compiler,
setCompiler,
imageName: project?.imageName,
setImageName,
rootDocId: project?.rootDoc_id,
setRootDocId,
spellCheckLanguage: project?.spellCheckLanguage,
setSpellCheckLanguage,
}
}