appStart.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React from 'react';
  2. import { render } from 'react-dom';
  3. import { Provider } from 'react-redux';
  4. import { getLanguage } from './actions.js';
  5. import About from '../components/About';
  6. import RequestAccount from '../components/RequestAccount';
  7. import NodeInfo from '../components/NodeInfo';
  8. /**
  9. The init function of Mist
  10. @method initMist
  11. */
  12. initMist = function() {
  13. console.info('Initialize Mist Interface');
  14. initTabs();
  15. };
  16. function initTabs() {
  17. console.debug('Init tabs');
  18. Tabs.onceSynced.then(function() {
  19. if (location.search.indexOf('reset-tabs') >= 0) {
  20. console.info('Resetting UI tabs');
  21. Tabs.remove({});
  22. }
  23. // Overwrite wallet on start again,
  24. // but use $set to preserve account titles
  25. Tabs.upsert(
  26. { _id: 'wallet' },
  27. {
  28. $set: {
  29. url: `file://${dirname}/wallet/index.html`,
  30. redirect: `file://${dirname}/wallet/index.html`,
  31. position: 0,
  32. permissions: {
  33. admin: true
  34. }
  35. }
  36. }
  37. );
  38. if (!Tabs.findOne('browser')) {
  39. const url = 'https://www.stateofthedapps.com';
  40. Tabs.insert({
  41. _id: 'browser',
  42. url,
  43. redirect: url,
  44. position: 1
  45. });
  46. } else {
  47. Tabs.upsert(
  48. { _id: 'browser' },
  49. {
  50. $set: { position: 1 }
  51. }
  52. );
  53. }
  54. // On first use, show wallet to nudge user to create an account
  55. if (
  56. !LocalStore.get('selectedTab') ||
  57. !Tabs.findOne(LocalStore.get('selectedTab'))
  58. ) {
  59. LocalStore.set('selectedTab', 'wallet');
  60. }
  61. });
  62. }
  63. function renderReactComponentPopup(locationHash) {
  64. // NOTE: when adding new React components, remember to skip meteor template in templates/index.js
  65. // Example hash: '#about'. Manipulate string to return 'About'.
  66. const componentName =
  67. locationHash.charAt(1).toUpperCase() + locationHash.slice(2);
  68. // JSX can't evaluate an expression or string, so map imported components here
  69. const components = {
  70. About,
  71. RequestAccount
  72. };
  73. // Only render a component if it exists
  74. if (!!components[componentName]) {
  75. const Component = components[componentName];
  76. render(<Component />, document.getElementById('react-entry'));
  77. }
  78. }
  79. function renderReactComponentMain() {
  80. render(
  81. <Provider store={store}>
  82. <NodeInfo />
  83. </Provider>,
  84. document.getElementById('react__node-info')
  85. );
  86. }
  87. function handleLanguage() {
  88. let currentLang = store.getState().settings.i18n;
  89. store.subscribe(() => {
  90. const newLang = store.getState().settings.i18n;
  91. if (currentLang !== newLang) {
  92. i18n.changeLanguage(newLang);
  93. currentLang = newLang;
  94. }
  95. });
  96. }
  97. function renderReact() {
  98. // handle main window:
  99. if (!location.hash) {
  100. handleLanguage();
  101. EthAccounts.init();
  102. initMist();
  103. renderReactComponentMain();
  104. } else {
  105. // handle popup windows:
  106. renderReactComponentPopup(location.hash);
  107. }
  108. }
  109. Meteor.startup(function() {
  110. console.info('Meteor starting up...');
  111. renderReact();
  112. store.dispatch(getLanguage());
  113. // change moment and numeral language, when language changes
  114. Tracker.autorun(function() {
  115. if (_.isString(TAPi18n.getLanguage())) {
  116. const lang = TAPi18n.getLanguage().substr(0, 2);
  117. moment.locale(lang);
  118. try {
  119. numeral.language(lang);
  120. } catch (err) {
  121. console.warn(
  122. `numeral.js couldn't set number formating: ${err.message}`
  123. );
  124. }
  125. EthTools.setLocale(lang);
  126. }
  127. });
  128. });