system.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Import modules. */
  2. import { defineStore } from 'pinia'
  3. /* Initialize constants. */
  4. const UPDATE_TICKER_INTERVAL = 30000 // 30 seconds
  5. /**
  6. * System Store
  7. */
  8. export const useSystemStore = defineStore('system', {
  9. state: () => ({
  10. /* Set constants. */
  11. ONE_SAT: BigInt('1'),
  12. ONE_NEX: BigInt('100'),
  13. ONE_KEX: BigInt('100000'),
  14. ONE_MEX: BigInt('100000000'),
  15. ONE_META: BigInt('1000000000000000000'),
  16. /* Initialize notifications. */
  17. notif: {
  18. isShowing: false,
  19. icon: null,
  20. title: null,
  21. description: null,
  22. delay: 7000,
  23. },
  24. /**
  25. * Flags
  26. *
  27. * 1. Dark mode
  28. * 2. Unconfirmed transactions
  29. */
  30. _flags: null,
  31. /**
  32. * Locale
  33. *
  34. * Controls the localization language.
  35. * (default is english)
  36. */
  37. _locale: null,
  38. /**
  39. * Notices
  40. *
  41. * System notices that nag/remind the user of some important action or
  42. * information; which can be permanently disabled ("Do Not Show Again")
  43. * via checkbox and confirmation.
  44. *
  45. * NOTE: Unique 1-byte (hex) codes (up to 255) are used to reduce the size
  46. * of this storage field.
  47. */
  48. _notices: null,
  49. /**
  50. * Tickers
  51. *
  52. * Support for multiple exchange tickers across multiple currencies.
  53. */
  54. _tickers: null,
  55. }),
  56. getters: {
  57. nex() {
  58. if (!this._tickers?.NEXA) {
  59. return null
  60. }
  61. return this._tickers.NEXA.quote.USD.price
  62. },
  63. usd() {
  64. if (!this.nex) {
  65. return null
  66. }
  67. return this.nex * 10**6
  68. },
  69. locale() {
  70. if (!this._locale) {
  71. return null
  72. }
  73. return this._locale
  74. },
  75. },
  76. actions: {
  77. /**
  78. * Initialize Application
  79. *
  80. * Performs startup activities.
  81. */
  82. async init() {
  83. /* Initialize locals. */
  84. let status
  85. /* Increment application starts. */
  86. this._appStarts++
  87. /* Validate tickers. */
  88. if (!this._tickers) {
  89. /* Initialize tickers. */
  90. this._tickers = {}
  91. }
  92. /* Initialize ticker interval. */
  93. setInterval(this.updateTicker, UPDATE_TICKER_INTERVAL)
  94. /* Update ticker. */
  95. this.updateTicker()
  96. if (this._locale === null) {
  97. /* Set (library) locale from (store) locale. */
  98. this._locale = navigator.language || navigator.userLanguage
  99. console.log(`User's preferred language is:`, this.locale)
  100. }
  101. /* Initialize (library) locale. */
  102. // const { locale } = useI18n()
  103. /* Set (library) locale. */
  104. // locale.value = this.locale
  105. },
  106. async updateTicker () {
  107. if (!this._tickers.NEXA) {
  108. this._tickers.NEXA = {}
  109. }
  110. this._tickers.NEXA = await $fetch('https://nexa.exchange/ticker')
  111. },
  112. },
  113. })