settings.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { pluck, distinctUntilChanged } from "rxjs/operators"
  2. import has from "lodash/has"
  3. import { Observable } from "rxjs"
  4. import { Ref } from "@nuxtjs/composition-api"
  5. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  6. import type { KeysMatching } from "~/types/ts-utils"
  7. import { useStream } from "~/helpers/utils/composables"
  8. export const HoppBgColors = ["system", "light", "dark", "black"] as const
  9. export type HoppBgColor = typeof HoppBgColors[number]
  10. export const HoppAccentColors = [
  11. "green",
  12. "teal",
  13. "blue",
  14. "indigo",
  15. "purple",
  16. "yellow",
  17. "orange",
  18. "red",
  19. "pink",
  20. ] as const
  21. export type HoppAccentColor = typeof HoppAccentColors[number]
  22. export const HoppFontSizes = ["small", "medium", "large"] as const
  23. export type HoppFontSize = typeof HoppFontSizes[number]
  24. export type SettingsType = {
  25. syncCollections: boolean
  26. syncHistory: boolean
  27. syncEnvironments: boolean
  28. PROXY_ENABLED: boolean
  29. PROXY_URL: string
  30. PROXY_KEY: string
  31. EXTENSIONS_ENABLED: boolean
  32. EXPERIMENTAL_URL_BAR_ENABLED: boolean
  33. URL_EXCLUDES: {
  34. auth: boolean
  35. httpUser: boolean
  36. httpPassword: boolean
  37. bearerToken: boolean
  38. oauth2Token: boolean
  39. }
  40. THEME_COLOR: HoppAccentColor
  41. BG_COLOR: HoppBgColor
  42. TELEMETRY_ENABLED: boolean
  43. LEFT_SIDEBAR: boolean
  44. RIGHT_SIDEBAR: boolean
  45. ZEN_MODE: boolean
  46. FONT_SIZE: HoppFontSize
  47. }
  48. export const defaultSettings: SettingsType = {
  49. syncCollections: true,
  50. syncHistory: true,
  51. syncEnvironments: true,
  52. PROXY_ENABLED: false,
  53. PROXY_URL: "https://proxy.hoppscotch.io/",
  54. PROXY_KEY: "",
  55. EXTENSIONS_ENABLED: true,
  56. EXPERIMENTAL_URL_BAR_ENABLED: true,
  57. URL_EXCLUDES: {
  58. auth: true,
  59. httpUser: true,
  60. httpPassword: true,
  61. bearerToken: true,
  62. oauth2Token: true,
  63. },
  64. THEME_COLOR: "blue",
  65. BG_COLOR: "system",
  66. TELEMETRY_ENABLED: true,
  67. LEFT_SIDEBAR: true,
  68. RIGHT_SIDEBAR: true,
  69. ZEN_MODE: false,
  70. FONT_SIZE: "small",
  71. }
  72. const validKeys = Object.keys(defaultSettings)
  73. const dispatchers = defineDispatchers({
  74. bulkApplySettings(
  75. _currentState: SettingsType,
  76. payload: Partial<SettingsType>
  77. ) {
  78. return payload
  79. },
  80. toggleSetting(
  81. currentState: SettingsType,
  82. { settingKey }: { settingKey: KeysMatching<SettingsType, boolean> }
  83. ) {
  84. if (!has(currentState, settingKey)) {
  85. console.log(
  86. `Toggling of a non-existent setting key '${settingKey}' ignored.`
  87. )
  88. return {}
  89. }
  90. const result: Partial<SettingsType> = {}
  91. result[settingKey] = !currentState[settingKey]
  92. return result
  93. },
  94. applySetting<K extends keyof SettingsType>(
  95. _currentState: SettingsType,
  96. { settingKey, value }: { settingKey: K; value: SettingsType[K] }
  97. ) {
  98. if (!validKeys.includes(settingKey)) {
  99. console.log(
  100. `Ignoring non-existent setting key '${settingKey}' assignment`
  101. )
  102. return {}
  103. }
  104. const result: Partial<SettingsType> = {}
  105. result[settingKey] = value
  106. return result
  107. },
  108. })
  109. export const settingsStore = new DispatchingStore(defaultSettings, dispatchers)
  110. /**
  111. * An observable value to make avail all the state information at once
  112. */
  113. export const settings$ = settingsStore.subject$.asObservable()
  114. export function getSettingSubject<K extends keyof SettingsType>(
  115. settingKey: K
  116. ): Observable<SettingsType[K]> {
  117. return settingsStore.subject$.pipe(pluck(settingKey), distinctUntilChanged())
  118. }
  119. export function bulkApplySettings(settingsObj: Partial<SettingsType>) {
  120. settingsStore.dispatch({
  121. dispatcher: "bulkApplySettings",
  122. payload: settingsObj,
  123. })
  124. }
  125. export function toggleSetting(settingKey: KeysMatching<SettingsType, boolean>) {
  126. settingsStore.dispatch({
  127. dispatcher: "toggleSetting",
  128. payload: {
  129. settingKey,
  130. },
  131. })
  132. }
  133. export function applySetting<K extends keyof SettingsType>(
  134. settingKey: K,
  135. value: SettingsType[K]
  136. ) {
  137. settingsStore.dispatch({
  138. dispatcher: "applySetting",
  139. payload: {
  140. settingKey,
  141. value,
  142. },
  143. })
  144. }
  145. export function useSetting<K extends keyof SettingsType>(
  146. settingKey: K
  147. ): Ref<SettingsType[K]> {
  148. return useStream(
  149. settingsStore.subject$.pipe(pluck(settingKey), distinctUntilChanged()),
  150. settingsStore.value[settingKey],
  151. (value: SettingsType[K]) => {
  152. settingsStore.dispatch({
  153. dispatcher: "applySetting",
  154. payload: {
  155. settingKey,
  156. value,
  157. },
  158. })
  159. }
  160. )
  161. }