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.
315 lines
9.4 KiB
315 lines
9.4 KiB
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import { Provider } from 'react-redux';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
import configureStore from '../store/configureStore';
|
||
8 years ago
|
import {
|
||
|
refreshTimelineSuccess,
|
||
|
updateTimeline,
|
||
|
deleteFromTimelines,
|
||
8 years ago
|
refreshTimeline,
|
||
|
connectTimeline,
|
||
|
disconnectTimeline
|
||
8 years ago
|
} from '../actions/timelines';
|
||
8 years ago
|
import { showOnboardingOnce } from '../actions/onboarding';
|
||
8 years ago
|
import { updateNotifications, refreshNotifications } from '../actions/notifications';
|
||
8 years ago
|
import createBrowserHistory from 'history/lib/createBrowserHistory';
|
||
8 years ago
|
import {
|
||
8 years ago
|
applyRouterMiddleware,
|
||
8 years ago
|
useRouterHistory,
|
||
8 years ago
|
Router,
|
||
|
Route,
|
||
8 years ago
|
IndexRedirect,
|
||
8 years ago
|
IndexRoute
|
||
8 years ago
|
} from 'react-router';
|
||
|
import { useScroll } from 'react-router-scroll';
|
||
|
import UI from '../features/ui';
|
||
|
import Status from '../features/status';
|
||
|
import GettingStarted from '../features/getting_started';
|
||
|
import PublicTimeline from '../features/public_timeline';
|
||
8 years ago
|
import CommunityTimeline from '../features/community_timeline';
|
||
8 years ago
|
import AccountTimeline from '../features/account_timeline';
|
||
|
import HomeTimeline from '../features/home_timeline';
|
||
|
import Compose from '../features/compose';
|
||
|
import Followers from '../features/followers';
|
||
|
import Following from '../features/following';
|
||
|
import Reblogs from '../features/reblogs';
|
||
|
import Favourites from '../features/favourites';
|
||
|
import HashtagTimeline from '../features/hashtag_timeline';
|
||
8 years ago
|
import Notifications from '../features/notifications';
|
||
8 years ago
|
import FollowRequests from '../features/follow_requests';
|
||
8 years ago
|
import GenericNotFound from '../features/generic_not_found';
|
||
8 years ago
|
import FavouritedStatuses from '../features/favourited_statuses';
|
||
8 years ago
|
import Blocks from '../features/blocks';
|
||
8 years ago
|
import Mutes from '../features/mutes';
|
||
8 years ago
|
import Report from '../features/report';
|
||
8 years ago
|
import { IntlProvider, addLocaleData } from 'react-intl';
|
||
8 years ago
|
import ar from 'react-intl/locale-data/ar';
|
||
8 years ago
|
import en from 'react-intl/locale-data/en';
|
||
8 years ago
|
import de from 'react-intl/locale-data/de';
|
||
8 years ago
|
import eo from 'react-intl/locale-data/eo';
|
||
8 years ago
|
import es from 'react-intl/locale-data/es';
|
||
8 years ago
|
import fa from 'react-intl/locale-data/fa';
|
||
8 years ago
|
import fi from 'react-intl/locale-data/fi';
|
||
8 years ago
|
import fr from 'react-intl/locale-data/fr';
|
||
8 years ago
|
import he from 'react-intl/locale-data/he';
|
||
8 years ago
|
import hu from 'react-intl/locale-data/hu';
|
||
8 years ago
|
import it from 'react-intl/locale-data/it';
|
||
8 years ago
|
import ja from 'react-intl/locale-data/ja';
|
||
8 years ago
|
import pt from 'react-intl/locale-data/pt';
|
||
8 years ago
|
import nl from 'react-intl/locale-data/nl';
|
||
8 years ago
|
import no from 'react-intl/locale-data/no';
|
||
8 years ago
|
import ru from 'react-intl/locale-data/ru';
|
||
|
import uk from 'react-intl/locale-data/uk';
|
||
|
import zh from 'react-intl/locale-data/zh';
|
||
8 years ago
|
import bg from 'react-intl/locale-data/bg';
|
||
8 years ago
|
import id from 'react-intl/locale-data/id';
|
||
8 years ago
|
import getMessagesForLocale from '../locales';
|
||
8 years ago
|
import { hydrateStore } from '../actions/store';
|
||
8 years ago
|
import createStream from '../stream';
|
||
8 years ago
|
|
||
8 years ago
|
const store = configureStore();
|
||
8 years ago
|
const initialState = JSON.parse(document.getElementById("initial-state").textContent);
|
||
|
store.dispatch(hydrateStore(initialState));
|
||
8 years ago
|
|
||
8 years ago
|
const browserHistory = useRouterHistory(createBrowserHistory)({
|
||
|
basename: '/web'
|
||
|
});
|
||
|
|
||
8 years ago
|
addLocaleData([
|
||
|
...en,
|
||
8 years ago
|
...ar,
|
||
8 years ago
|
...de,
|
||
|
...eo,
|
||
|
...es,
|
||
8 years ago
|
...fa,
|
||
8 years ago
|
...fi,
|
||
|
...fr,
|
||
8 years ago
|
...he,
|
||
8 years ago
|
...hu,
|
||
8 years ago
|
...it,
|
||
8 years ago
|
...ja,
|
||
|
...pt,
|
||
8 years ago
|
...nl,
|
||
8 years ago
|
...no,
|
||
8 years ago
|
...ru,
|
||
|
...uk,
|
||
|
...zh,
|
||
8 years ago
|
...bg,
|
||
8 years ago
|
...id,
|
||
8 years ago
|
]);
|
||
8 years ago
|
|
||
8 years ago
|
const getTopWhenReplacing = (previous, { location }) => location && location.action === 'REPLACE' && [0, 0];
|
||
|
|
||
|
const hiddenColumnContainerStyle = {
|
||
|
position: 'absolute',
|
||
|
left: '0',
|
||
|
top: '0',
|
||
|
visibility: 'hidden'
|
||
|
};
|
||
|
|
||
|
class Container extends React.PureComponent {
|
||
|
|
||
|
constructor(props) {
|
||
|
super(props);
|
||
|
|
||
|
this.state = {
|
||
|
renderedPersistents: [],
|
||
|
unrenderedPersistents: [],
|
||
|
};
|
||
|
}
|
||
|
|
||
|
componentWillMount () {
|
||
|
this.unlistenHistory = null;
|
||
|
|
||
|
this.setState(() => {
|
||
|
return {
|
||
|
mountImpersistent: false,
|
||
|
renderedPersistents: [],
|
||
|
unrenderedPersistents: [
|
||
|
{pathname: '/timelines/home', component: HomeTimeline},
|
||
|
{pathname: '/timelines/public', component: PublicTimeline},
|
||
|
{pathname: '/timelines/public/local', component: CommunityTimeline},
|
||
|
|
||
|
{pathname: '/notifications', component: Notifications},
|
||
|
{pathname: '/favourites', component: FavouritedStatuses}
|
||
|
],
|
||
|
};
|
||
|
}, () => {
|
||
|
if (this.unlistenHistory) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.unlistenHistory = browserHistory.listen(location => {
|
||
|
const pathname = location.pathname.replace(/\/$/, '').toLowerCase();
|
||
|
|
||
|
this.setState(oldState => {
|
||
|
let persistentMatched = false;
|
||
|
|
||
|
const newState = {
|
||
|
renderedPersistents: oldState.renderedPersistents.map(persistent => {
|
||
|
const givenMatched = persistent.pathname === pathname;
|
||
|
|
||
|
if (givenMatched) {
|
||
|
persistentMatched = true;
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
hidden: !givenMatched,
|
||
|
pathname: persistent.pathname,
|
||
|
component: persistent.component
|
||
|
};
|
||
|
}),
|
||
|
};
|
||
|
|
||
|
if (!persistentMatched) {
|
||
|
newState.unrenderedPersistents = [];
|
||
|
|
||
|
oldState.unrenderedPersistents.forEach(persistent => {
|
||
|
if (persistent.pathname === pathname) {
|
||
|
persistentMatched = true;
|
||
|
|
||
|
newState.renderedPersistents.push({
|
||
|
hidden: false,
|
||
|
pathname: persistent.pathname,
|
||
|
component: persistent.component
|
||
|
});
|
||
|
} else {
|
||
|
newState.unrenderedPersistents.push(persistent);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
newState.mountImpersistent = !persistentMatched;
|
||
|
|
||
|
return newState;
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
componentWillUnmount () {
|
||
|
if (this.unlistenHistory) {
|
||
|
this.unlistenHistory();
|
||
|
}
|
||
|
|
||
|
this.unlistenHistory = "done";
|
||
|
}
|
||
|
|
||
|
render () {
|
||
|
// Hide some components rather than unmounting them to allow to show again
|
||
|
// quickly and keep the view state such as the scrolled offset.
|
||
|
const persistentsView = this.state.renderedPersistents.map((persistent) =>
|
||
|
<div aria-hidden={persistent.hidden} key={persistent.pathname} className='mastodon-column-container' style={persistent.hidden ? hiddenColumnContainerStyle : null}>
|
||
|
<persistent.component shouldUpdateScroll={persistent.hidden ? Function.prototype : getTopWhenReplacing} />
|
||
|
</div>
|
||
|
);
|
||
|
|
||
|
return (
|
||
|
<UI>
|
||
|
{this.state.mountImpersistent && this.props.children}
|
||
|
{persistentsView}
|
||
|
</UI>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Container.propTypes = {
|
||
|
children: PropTypes.node,
|
||
|
};
|
||
|
|
||
8 years ago
|
class Mastodon extends React.Component {
|
||
8 years ago
|
|
||
8 years ago
|
componentDidMount() {
|
||
|
const { locale } = this.props;
|
||
8 years ago
|
const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
|
||
8 years ago
|
const accessToken = store.getState().getIn(['meta', 'access_token']);
|
||
8 years ago
|
|
||
8 years ago
|
this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
|
||
8 years ago
|
|
||
8 years ago
|
connected () {
|
||
|
store.dispatch(connectTimeline('home'));
|
||
|
},
|
||
|
|
||
|
disconnected () {
|
||
|
store.dispatch(disconnectTimeline('home'));
|
||
|
},
|
||
|
|
||
8 years ago
|
received (data) {
|
||
|
switch(data.event) {
|
||
|
case 'update':
|
||
|
store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
|
||
|
break;
|
||
|
case 'delete':
|
||
|
store.dispatch(deleteFromTimelines(data.payload));
|
||
|
break;
|
||
|
case 'notification':
|
||
|
store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
|
||
|
break;
|
||
8 years ago
|
}
|
||
8 years ago
|
},
|
||
|
|
||
|
reconnected () {
|
||
8 years ago
|
store.dispatch(connectTimeline('home'));
|
||
8 years ago
|
store.dispatch(refreshTimeline('home'));
|
||
|
store.dispatch(refreshNotifications());
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
|
// Desktop notifications
|
||
8 years ago
|
if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
|
||
8 years ago
|
Notification.requestPermission();
|
||
|
}
|
||
8 years ago
|
|
||
|
store.dispatch(showOnboardingOnce());
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
componentWillUnmount () {
|
||
|
if (typeof this.subscription !== 'undefined') {
|
||
8 years ago
|
this.subscription.close();
|
||
|
this.subscription = null;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
render () {
|
||
8 years ago
|
const { locale } = this.props;
|
||
|
|
||
8 years ago
|
return (
|
||
8 years ago
|
<IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
|
||
8 years ago
|
<Provider store={store}>
|
||
|
<Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
|
||
8 years ago
|
<Route path='/' component={Container}>
|
||
8 years ago
|
<IndexRedirect to='/getting-started' />
|
||
8 years ago
|
<Route path='getting-started' component={GettingStarted} />
|
||
8 years ago
|
<Route path='timelines/tag/:id' component={HashtagTimeline} />
|
||
|
|
||
|
<Route path='statuses/new' component={Compose} />
|
||
|
<Route path='statuses/:statusId' component={Status} />
|
||
|
<Route path='statuses/:statusId/reblogs' component={Reblogs} />
|
||
|
<Route path='statuses/:statusId/favourites' component={Favourites} />
|
||
|
|
||
8 years ago
|
<Route path='accounts/:accountId' component={AccountTimeline} />
|
||
|
<Route path='accounts/:accountId/followers' component={Followers} />
|
||
|
<Route path='accounts/:accountId/following' component={Following} />
|
||
8 years ago
|
|
||
|
<Route path='follow_requests' component={FollowRequests} />
|
||
8 years ago
|
<Route path='blocks' component={Blocks} />
|
||
8 years ago
|
<Route path='mutes' component={Mutes} />
|
||
8 years ago
|
<Route path='report' component={Report} />
|
||
8 years ago
|
|
||
8 years ago
|
<Route path='*' component={GenericNotFound} />
|
||
8 years ago
|
</Route>
|
||
8 years ago
|
</Router>
|
||
|
</Provider>
|
||
|
</IntlProvider>
|
||
8 years ago
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|
||
|
|
||
|
Mastodon.propTypes = {
|
||
|
locale: PropTypes.string.isRequired
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
export default Mastodon;
|