index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Actions, reducer, Selectors } from '@andyet/simplewebrtc';
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import { Provider } from 'react-redux';
  5. import {
  6. applyMiddleware,
  7. combineReducers,
  8. compose as ReduxCompose,
  9. createStore
  10. } from 'redux';
  11. import Thunk from 'redux-thunk';
  12. import App from './App';
  13. import { PlaceholderGenerator } from './types';
  14. import getConfigFromMetaTag from './utils/metaConfig';
  15. import randomRoomName from './utils/randomRoomName';
  16. const configUrl = getConfigFromMetaTag('config-url');
  17. const CONFIG_URL = configUrl ? configUrl : '';
  18. const userData = getConfigFromMetaTag('user-data');
  19. const USER_DATA = userData ? userData : '';
  20. const compose =
  21. (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || ReduxCompose;
  22. const store = createStore(
  23. combineReducers({ simplewebrtc: reducer }),
  24. { simplewebrtc: {} },
  25. compose(applyMiddleware(Thunk))
  26. );
  27. (window as any).store = store;
  28. (window as any).actions = Actions;
  29. (window as any).selectors = Selectors;
  30. // Force the page to reload after 12 hours
  31. setTimeout(() => {
  32. window.location.reload(true);
  33. }, 1000 * 60 * 60 * 12);
  34. interface RunConfig {
  35. roomName: string;
  36. root: HTMLElement;
  37. gridPlaceholder?: PlaceholderGenerator;
  38. haircheckHeaderPlaceholder?: PlaceholderGenerator;
  39. emptyRosterPlaceholder?: PlaceholderGenerator;
  40. homepagePlaceholder?: PlaceholderGenerator;
  41. }
  42. const run = ({
  43. roomName,
  44. root,
  45. gridPlaceholder,
  46. haircheckHeaderPlaceholder,
  47. emptyRosterPlaceholder,
  48. homepagePlaceholder
  49. }: RunConfig) => {
  50. if (CONFIG_URL.endsWith('YOUR_API_KEY')) {
  51. ReactDOM.render(
  52. <div className="container" style={{textAlign: 'left'}}>
  53. <h1>Configuration Setup Needed:</h1>
  54. <p>Edit <code>public/index.html</code> to add your API key to the configuration URL.</p>
  55. <p>Visit <a href="https://simplewebrtc.com">simplewebrtc.com</a> to sign up and get an API key.</p>
  56. <h2>How to set your API key:</h2>
  57. <p>See the meta tag section marked <code>IMPORTANT SETUP</code> in <code>public/index.html</code>:</p>
  58. <pre style={{ textAlign: 'left' }}>
  59. {'<!-- IMPORTANT SETUP -->'}<br />
  60. {'<!-- Change the YOUR_API_KEY section of the config URL to match your API key -->'}<br />
  61. {`<meta
  62. name="simplewebrtc-config-url"
  63. content="https://api.simplewebrtc.com/config/guest/YOUR_API_KEY"
  64. />`}
  65. </pre>
  66. </div>,
  67. root
  68. );
  69. return;
  70. }
  71. ReactDOM.render(
  72. <Provider store={store}>
  73. <App
  74. roomName={roomName}
  75. configUrl={CONFIG_URL}
  76. userData={USER_DATA}
  77. gridPlaceholder={gridPlaceholder ? gridPlaceholder : null}
  78. haircheckHeaderPlaceholder={
  79. haircheckHeaderPlaceholder ? haircheckHeaderPlaceholder : null
  80. }
  81. emptyRosterPlaceholder={
  82. emptyRosterPlaceholder ? emptyRosterPlaceholder : null
  83. }
  84. homepagePlaceholder={homepagePlaceholder ? homepagePlaceholder : null}
  85. />
  86. </Provider>,
  87. root
  88. );
  89. };
  90. const loadTemplate = (id: string): DocumentFragment | null => {
  91. const el = document.getElementById(id);
  92. if (el !== null && el.nodeName === 'TEMPLATE') {
  93. return document.importNode((el as HTMLTemplateElement).content, true);
  94. }
  95. return null;
  96. };
  97. export default {
  98. run,
  99. loadTemplate,
  100. randomRoomName
  101. };