闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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