api.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }
  32. type SaveUserSettings = Partial<
  33. UserSettings & {
  34. spellCheckLanguage: ProjectSettings['spellCheckLanguage']
  35. }
  36. >
  37. export function saveUserSettings(
  38. key: keyof SaveUserSettings,
  39. value: SaveUserSettings[keyof SaveUserSettings]
  40. ) {
  41. sendMB('setting-changed', {
  42. changedSetting: key,
  43. changedSettingVal: value,
  44. })
  45. postJSON('/user/settings', {
  46. body: {
  47. [key]: value,
  48. },
  49. }).catch(debugConsole.error)
  50. }
  51. export const saveProjectSettings = async (
  52. projectId: string,
  53. data: Partial<ProjectSettings>
  54. ) => {
  55. await postJSON<never>(`/project/${projectId}/settings`, {
  56. body: {
  57. ...data,
  58. },
  59. })
  60. }