The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

321 lines
9.6 KiB

  1. import { Provider } from 'react-redux';
  2. import PropTypes from 'prop-types';
  3. import configureStore from '../store/configureStore';
  4. import {
  5. refreshTimelineSuccess,
  6. updateTimeline,
  7. deleteFromTimelines,
  8. refreshTimeline,
  9. connectTimeline,
  10. disconnectTimeline
  11. } from '../actions/timelines';
  12. import { showOnboardingOnce } from '../actions/onboarding';
  13. import { updateNotifications, refreshNotifications } from '../actions/notifications';
  14. import createBrowserHistory from 'history/lib/createBrowserHistory';
  15. import {
  16. applyRouterMiddleware,
  17. useRouterHistory,
  18. Router,
  19. Route,
  20. IndexRedirect,
  21. IndexRoute
  22. } from 'react-router';
  23. import { useScroll } from 'react-router-scroll';
  24. import UI from '../features/ui';
  25. import Status from '../features/status';
  26. import GettingStarted from '../features/getting_started';
  27. import PublicTimeline from '../features/public_timeline';
  28. import CommunityTimeline from '../features/community_timeline';
  29. import AccountTimeline from '../features/account_timeline';
  30. import HomeTimeline from '../features/home_timeline';
  31. import Compose from '../features/compose';
  32. import Followers from '../features/followers';
  33. import Following from '../features/following';
  34. import Reblogs from '../features/reblogs';
  35. import Favourites from '../features/favourites';
  36. import HashtagTimeline from '../features/hashtag_timeline';
  37. import Notifications from '../features/notifications';
  38. import FollowRequests from '../features/follow_requests';
  39. import GenericNotFound from '../features/generic_not_found';
  40. import FavouritedStatuses from '../features/favourited_statuses';
  41. import Blocks from '../features/blocks';
  42. import Mutes from '../features/mutes';
  43. import Report from '../features/report';
  44. import { IntlProvider, addLocaleData } from 'react-intl';
  45. import ar from 'react-intl/locale-data/ar';
  46. import en from 'react-intl/locale-data/en';
  47. import de from 'react-intl/locale-data/de';
  48. import eo from 'react-intl/locale-data/eo';
  49. import es from 'react-intl/locale-data/es';
  50. import fa from 'react-intl/locale-data/fa';
  51. import fi from 'react-intl/locale-data/fi';
  52. import fr from 'react-intl/locale-data/fr';
  53. import he from 'react-intl/locale-data/he';
  54. import hu from 'react-intl/locale-data/hu';
  55. import it from 'react-intl/locale-data/it';
  56. import ja from 'react-intl/locale-data/ja';
  57. import pt from 'react-intl/locale-data/pt';
  58. import nl from 'react-intl/locale-data/nl';
  59. import no from 'react-intl/locale-data/no';
  60. import ru from 'react-intl/locale-data/ru';
  61. import uk from 'react-intl/locale-data/uk';
  62. import zh from 'react-intl/locale-data/zh';
  63. import bg from 'react-intl/locale-data/bg';
  64. import id from 'react-intl/locale-data/id';
  65. import { localeData as zh_hk } from '../locales/zh-hk';
  66. import { localeData as zh_cn } from '../locales/zh-cn';
  67. import pt_br from '../locales/pt-br';
  68. import getMessagesForLocale from '../locales';
  69. import { hydrateStore } from '../actions/store';
  70. import createStream from '../stream';
  71. const store = configureStore();
  72. const initialState = JSON.parse(document.getElementById("initial-state").textContent);
  73. store.dispatch(hydrateStore(initialState));
  74. const browserHistory = useRouterHistory(createBrowserHistory)({
  75. basename: '/web'
  76. });
  77. addLocaleData([
  78. ...en,
  79. ...ar,
  80. ...de,
  81. ...eo,
  82. ...es,
  83. ...fa,
  84. ...fi,
  85. ...fr,
  86. ...he,
  87. ...hu,
  88. ...it,
  89. ...ja,
  90. ...pt,
  91. ...pt_br,
  92. ...nl,
  93. ...no,
  94. ...ru,
  95. ...uk,
  96. ...zh,
  97. ...zh_hk,
  98. ...zh_cn,
  99. ...bg,
  100. ...id,
  101. ]);
  102. const getTopWhenReplacing = (previous, { location }) => location && location.action === 'REPLACE' && [0, 0];
  103. const hiddenColumnContainerStyle = {
  104. position: 'absolute',
  105. left: '0',
  106. top: '0',
  107. visibility: 'hidden'
  108. };
  109. class Container extends React.PureComponent {
  110. constructor(props) {
  111. super(props);
  112. this.state = {
  113. renderedPersistents: [],
  114. unrenderedPersistents: [],
  115. };
  116. }
  117. componentWillMount () {
  118. this.unlistenHistory = null;
  119. this.setState(() => {
  120. return {
  121. mountImpersistent: false,
  122. renderedPersistents: [],
  123. unrenderedPersistents: [
  124. {pathname: '/timelines/home', component: HomeTimeline},
  125. {pathname: '/timelines/public', component: PublicTimeline},
  126. {pathname: '/timelines/public/local', component: CommunityTimeline},
  127. {pathname: '/notifications', component: Notifications},
  128. {pathname: '/favourites', component: FavouritedStatuses}
  129. ],
  130. };
  131. }, () => {
  132. if (this.unlistenHistory) {
  133. return;
  134. }
  135. this.unlistenHistory = browserHistory.listen(location => {
  136. const pathname = location.pathname.replace(/\/$/, '').toLowerCase();
  137. this.setState(oldState => {
  138. let persistentMatched = false;
  139. const newState = {
  140. renderedPersistents: oldState.renderedPersistents.map(persistent => {
  141. const givenMatched = persistent.pathname === pathname;
  142. if (givenMatched) {
  143. persistentMatched = true;
  144. }
  145. return {
  146. hidden: !givenMatched,
  147. pathname: persistent.pathname,
  148. component: persistent.component
  149. };
  150. }),
  151. };
  152. if (!persistentMatched) {
  153. newState.unrenderedPersistents = [];
  154. oldState.unrenderedPersistents.forEach(persistent => {
  155. if (persistent.pathname === pathname) {
  156. persistentMatched = true;
  157. newState.renderedPersistents.push({
  158. hidden: false,
  159. pathname: persistent.pathname,
  160. component: persistent.component
  161. });
  162. } else {
  163. newState.unrenderedPersistents.push(persistent);
  164. }
  165. });
  166. }
  167. newState.mountImpersistent = !persistentMatched;
  168. return newState;
  169. });
  170. });
  171. });
  172. }
  173. componentWillUnmount () {
  174. if (this.unlistenHistory) {
  175. this.unlistenHistory();
  176. }
  177. this.unlistenHistory = "done";
  178. }
  179. render () {
  180. // Hide some components rather than unmounting them to allow to show again
  181. // quickly and keep the view state such as the scrolled offset.
  182. const persistentsView = this.state.renderedPersistents.map((persistent) =>
  183. <div aria-hidden={persistent.hidden} key={persistent.pathname} className='mastodon-column-container' style={persistent.hidden ? hiddenColumnContainerStyle : null}>
  184. <persistent.component shouldUpdateScroll={persistent.hidden ? Function.prototype : getTopWhenReplacing} />
  185. </div>
  186. );
  187. return (
  188. <UI>
  189. {this.state.mountImpersistent && this.props.children}
  190. {persistentsView}
  191. </UI>
  192. );
  193. }
  194. }
  195. Container.propTypes = {
  196. children: PropTypes.node,
  197. };
  198. class Mastodon extends React.Component {
  199. componentDidMount() {
  200. const { locale } = this.props;
  201. const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
  202. const accessToken = store.getState().getIn(['meta', 'access_token']);
  203. this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
  204. connected () {
  205. store.dispatch(connectTimeline('home'));
  206. },
  207. disconnected () {
  208. store.dispatch(disconnectTimeline('home'));
  209. },
  210. received (data) {
  211. switch(data.event) {
  212. case 'update':
  213. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  214. break;
  215. case 'delete':
  216. store.dispatch(deleteFromTimelines(data.payload));
  217. break;
  218. case 'notification':
  219. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  220. break;
  221. }
  222. },
  223. reconnected () {
  224. store.dispatch(connectTimeline('home'));
  225. store.dispatch(refreshTimeline('home'));
  226. store.dispatch(refreshNotifications());
  227. }
  228. });
  229. // Desktop notifications
  230. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  231. Notification.requestPermission();
  232. }
  233. store.dispatch(showOnboardingOnce());
  234. }
  235. componentWillUnmount () {
  236. if (typeof this.subscription !== 'undefined') {
  237. this.subscription.close();
  238. this.subscription = null;
  239. }
  240. }
  241. render () {
  242. const { locale } = this.props;
  243. return (
  244. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  245. <Provider store={store}>
  246. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  247. <Route path='/' component={Container}>
  248. <IndexRedirect to="/getting-started" />
  249. <Route path='getting-started' component={GettingStarted} />
  250. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  251. <Route path='statuses/new' component={Compose} />
  252. <Route path='statuses/:statusId' component={Status} />
  253. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  254. <Route path='statuses/:statusId/favourites' component={Favourites} />
  255. <Route path='accounts/:accountId' component={AccountTimeline} />
  256. <Route path='accounts/:accountId/followers' component={Followers} />
  257. <Route path='accounts/:accountId/following' component={Following} />
  258. <Route path='follow_requests' component={FollowRequests} />
  259. <Route path='blocks' component={Blocks} />
  260. <Route path='mutes' component={Mutes} />
  261. <Route path='report' component={Report} />
  262. <Route path='*' component={GenericNotFound} />
  263. </Route>
  264. </Router>
  265. </Provider>
  266. </IntlProvider>
  267. );
  268. }
  269. }
  270. Mastodon.propTypes = {
  271. locale: PropTypes.string.isRequired
  272. };
  273. export default Mastodon;