logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

config.js (5915B)


  1. import Cookies from 'js-cookie'
  2. import { applyConfig } from '../services/style_setter/style_setter.js'
  3. import messages from '../i18n/messages'
  4. import { set } from 'lodash'
  5. import localeService from '../services/locale/locale.service.js'
  6. import { useI18nStore } from 'src/stores/i18n.js'
  7. import { useInterfaceStore } from 'src/stores/interface.js'
  8. import { defaultState } from './default_config_state.js'
  9. const BACKEND_LANGUAGE_COOKIE_NAME = 'userLanguage'
  10. const APPEARANCE_SETTINGS_KEYS = new Set([
  11. 'sidebarColumnWidth',
  12. 'contentColumnWidth',
  13. 'notifsColumnWidth',
  14. 'textSize',
  15. 'navbarSize',
  16. 'panelHeaderSize',
  17. 'forcedRoundness',
  18. 'emojiSize',
  19. 'emojiReactionsScale'
  20. ])
  21. /* TODO this is a bit messy.
  22. * We need to declare settings with their types and also deal with
  23. * instance-default settings in some way, hopefully try to avoid copy-pasta
  24. * in general.
  25. */
  26. export const multiChoiceProperties = [
  27. 'postContentType',
  28. 'subjectLineBehavior',
  29. 'conversationDisplay', // tree | linear
  30. 'conversationOtherRepliesButton', // below | inside
  31. 'mentionLinkDisplay', // short | full_for_remote | full
  32. 'userPopoverAvatarAction', // close | zoom | open
  33. 'unsavedPostAction' // save | discard | confirm
  34. ]
  35. // caching the instance default properties
  36. export const instanceDefaultProperties = Object.entries(defaultState)
  37. .filter(([, value]) => value === undefined)
  38. .map(([key]) => key)
  39. const config = {
  40. state: { ...defaultState },
  41. getters: {
  42. defaultConfig (state, getters, rootState) {
  43. const { instance } = rootState
  44. return {
  45. ...defaultState,
  46. ...Object.fromEntries(
  47. instanceDefaultProperties.map(key => [key, instance[key]])
  48. )
  49. }
  50. },
  51. mergedConfig (state, getters, rootState, rootGetters) {
  52. const { defaultConfig } = rootGetters
  53. return {
  54. ...defaultConfig,
  55. // Do not override with undefined
  56. ...Object.fromEntries(Object.entries(state).filter(([, v]) => v !== undefined))
  57. }
  58. }
  59. },
  60. mutations: {
  61. setOptionTemporarily (state, { name, value }) {
  62. set(state, name, value)
  63. applyConfig(state)
  64. },
  65. setOption (state, { name, value }) {
  66. set(state, name, value)
  67. },
  68. setHighlight (state, { user, color, type }) {
  69. const data = this.state.config.highlight[user]
  70. if (color || type) {
  71. state.highlight[user] = { color: color || data.color, type: type || data.type }
  72. } else {
  73. delete state.highlight[user]
  74. }
  75. }
  76. },
  77. actions: {
  78. loadSettings ({ dispatch }, data) {
  79. const knownKeys = new Set(Object.keys(defaultState))
  80. const presentKeys = new Set(Object.keys(data))
  81. const intersection = new Set()
  82. for (const elem of presentKeys) {
  83. if (knownKeys.has(elem)) {
  84. intersection.add(elem)
  85. }
  86. }
  87. intersection.forEach(
  88. name => dispatch('setOption', { name, value: data[name] })
  89. )
  90. },
  91. setHighlight ({ commit }, { user, color, type }) {
  92. commit('setHighlight', { user, color, type })
  93. },
  94. setOptionTemporarily ({ commit, dispatch, state }, { name, value }) {
  95. if (useInterfaceStore().temporaryChangesTimeoutId !== null) {
  96. console.warn('Can\'t track more than one temporary change')
  97. return
  98. }
  99. const oldValue = state[name]
  100. commit('setOptionTemporarily', { name, value })
  101. const confirm = () => {
  102. dispatch('setOption', { name, value })
  103. commit('clearTemporaryChanges')
  104. }
  105. const revert = () => {
  106. commit('setOptionTemporarily', { name, value: oldValue })
  107. commit('clearTemporaryChanges')
  108. }
  109. commit('setTemporaryChanges', {
  110. timeoutId: setTimeout(revert, 10000),
  111. confirm,
  112. revert
  113. })
  114. },
  115. setThemeV2 ({ commit, dispatch }, { customTheme, customThemeSource }) {
  116. commit('setOption', { name: 'theme', value: 'custom' })
  117. commit('setOption', { name: 'customTheme', value: customTheme })
  118. commit('setOption', { name: 'customThemeSource', value: customThemeSource })
  119. dispatch('setTheme', { themeData: customThemeSource, recompile: true })
  120. },
  121. setOption ({ commit, dispatch, state }, { name, value }) {
  122. const exceptions = new Set([
  123. 'useStreamingApi'
  124. ])
  125. if (exceptions.has(name)) {
  126. switch (name) {
  127. case 'useStreamingApi': {
  128. const action = value ? 'enableMastoSockets' : 'disableMastoSockets'
  129. dispatch(action).then(() => {
  130. commit('setOption', { name: 'useStreamingApi', value })
  131. }).catch((e) => {
  132. console.error('Failed starting MastoAPI Streaming socket', e)
  133. dispatch('disableMastoSockets')
  134. dispatch('setOption', { name: 'useStreamingApi', value: false })
  135. })
  136. break
  137. }
  138. }
  139. } else {
  140. commit('setOption', { name, value })
  141. if (APPEARANCE_SETTINGS_KEYS.has(name)) {
  142. applyConfig(state)
  143. }
  144. if (name.startsWith('theme3hacks')) {
  145. dispatch('applyTheme', { recompile: true })
  146. }
  147. switch (name) {
  148. case 'theme':
  149. if (value === 'custom') break
  150. dispatch('setTheme', { themeName: value, recompile: true, saveData: true })
  151. break
  152. case 'themeDebug': {
  153. dispatch('setTheme', { recompile: true })
  154. break
  155. }
  156. case 'interfaceLanguage':
  157. messages.setLanguage(useI18nStore().i18n, value)
  158. dispatch('loadUnicodeEmojiData', value)
  159. Cookies.set(
  160. BACKEND_LANGUAGE_COOKIE_NAME,
  161. localeService.internalToBackendLocaleMulti(value)
  162. )
  163. break
  164. case 'thirdColumnMode':
  165. useInterfaceStore().setLayoutWidth(undefined)
  166. break
  167. }
  168. }
  169. }
  170. }
  171. }
  172. export default config