main.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { createRoot } from 'react-dom/client';
  2. import { setupBrowserNotifications } from 'mastodon/actions/notifications';
  3. import Mastodon from 'mastodon/containers/mastodon';
  4. import { me } from 'mastodon/initial_state';
  5. import * as perf from 'mastodon/performance';
  6. import ready from 'mastodon/ready';
  7. import { store } from 'mastodon/store';
  8. import { isProduction } from './utils/environment';
  9. /**
  10. * @returns {Promise<void>}
  11. */
  12. function main() {
  13. perf.start('main()');
  14. return ready(async () => {
  15. const mountNode = document.getElementById('mastodon');
  16. const props = JSON.parse(mountNode.getAttribute('data-props'));
  17. const root = createRoot(mountNode);
  18. root.render(<Mastodon {...props} />);
  19. store.dispatch(setupBrowserNotifications());
  20. if (isProduction() && me && 'serviceWorker' in navigator) {
  21. const { Workbox } = await import('workbox-window');
  22. const wb = new Workbox('/sw.js');
  23. /** @type {ServiceWorkerRegistration} */
  24. let registration;
  25. try {
  26. registration = await wb.register();
  27. } catch (err) {
  28. console.error(err);
  29. }
  30. if (registration && 'Notification' in window && Notification.permission === 'granted') {
  31. const registerPushNotifications = await import('mastodon/actions/push_notifications');
  32. store.dispatch(registerPushNotifications.register());
  33. }
  34. }
  35. perf.stop('main()');
  36. });
  37. }
  38. export default main;