system.ts 3.2 KB

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