api.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type {
  2. FontFamily,
  3. LineHeight,
  4. OverallTheme,
  5. } from '../../source-editor/extensions/theme'
  6. import type {
  7. Keybindings,
  8. PdfViewer,
  9. ProjectCompiler,
  10. } from '../../../../../types/project-settings'
  11. import { sendMB } from '../../../infrastructure/event-tracking'
  12. import { postJSON } from '../../../infrastructure/fetch-json'
  13. import { debugConsole } from '@/utils/debugging'
  14. export type UserSettings = {
  15. pdfViewer: PdfViewer
  16. autoComplete: boolean
  17. autoPairDelimiters: boolean
  18. syntaxValidation: boolean
  19. editorTheme: string
  20. overallTheme: OverallTheme
  21. mode: Keybindings
  22. fontSize: string
  23. fontFamily: FontFamily
  24. lineHeight: LineHeight
  25. }
  26. export type ProjectSettings = {
  27. compiler: ProjectCompiler
  28. imageName: string
  29. rootDocId: string
  30. spellCheckLanguage: string
  31. name: string
  32. }
  33. type SaveUserSettings = Partial<
  34. UserSettings & {
  35. spellCheckLanguage: ProjectSettings['spellCheckLanguage']
  36. }
  37. >
  38. export function saveUserSettings(
  39. key: keyof SaveUserSettings,
  40. value: SaveUserSettings[keyof SaveUserSettings]
  41. ) {
  42. sendMB('setting-changed', {
  43. changedSetting: key,
  44. changedSettingVal: value,
  45. })
  46. postJSON('/user/settings', {
  47. body: {
  48. [key]: value,
  49. },
  50. }).catch(debugConsole.error)
  51. }
  52. export const saveProjectSettings = async (
  53. projectId: string,
  54. data: Partial<ProjectSettings>
  55. ) => {
  56. await postJSON<never>(`/project/${projectId}/settings`, {
  57. body: {
  58. ...data,
  59. },
  60. })
  61. }