2017-05-12 00:23:56 +00:00
|
|
|
// @flow
|
2016-02-27 21:53:11 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { render } from 'react-dom';
|
2016-06-26 06:31:22 +00:00
|
|
|
import { Provider } from 'mobx-react';
|
2017-05-17 07:11:13 +00:00
|
|
|
import {
|
|
|
|
BrowserRouter as Router,
|
|
|
|
Switch,
|
|
|
|
Route,
|
|
|
|
Redirect,
|
|
|
|
} from 'react-router-dom';
|
|
|
|
import { Flex } from 'reflexbox';
|
2016-02-27 21:53:11 +00:00
|
|
|
|
2016-06-26 06:31:22 +00:00
|
|
|
import stores from 'stores';
|
2017-05-26 19:56:10 +00:00
|
|
|
import CollectionsStore from 'stores/CollectionsStore';
|
2016-02-27 21:53:11 +00:00
|
|
|
|
2016-05-04 06:43:43 +00:00
|
|
|
import 'normalize.css/normalize.css';
|
2016-07-15 06:03:04 +00:00
|
|
|
import 'styles/base.scss';
|
|
|
|
import 'styles/fonts.css';
|
2016-10-09 22:17:22 +00:00
|
|
|
import 'styles/transitions.scss';
|
2017-05-18 02:36:31 +00:00
|
|
|
import 'styles/prism-tomorrow.scss';
|
2016-07-15 06:03:04 +00:00
|
|
|
import 'styles/hljs-github-gist.scss';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
import Home from 'scenes/Home';
|
|
|
|
import Dashboard from 'scenes/Dashboard';
|
2017-05-26 19:56:10 +00:00
|
|
|
import Collection from 'scenes/Collection';
|
2017-05-18 02:36:31 +00:00
|
|
|
import Document from 'scenes/Document';
|
2016-07-18 03:59:32 +00:00
|
|
|
import Search from 'scenes/Search';
|
2016-08-24 03:39:31 +00:00
|
|
|
import Settings from 'scenes/Settings';
|
2016-04-29 05:25:37 +00:00
|
|
|
import SlackAuth from 'scenes/SlackAuth';
|
2016-09-15 03:50:59 +00:00
|
|
|
import Flatpage from 'scenes/Flatpage';
|
2016-08-28 17:59:34 +00:00
|
|
|
import ErrorAuth from 'scenes/ErrorAuth';
|
2017-02-10 03:57:35 +00:00
|
|
|
import Error404 from 'scenes/Error404';
|
2016-08-01 16:18:27 +00:00
|
|
|
|
2016-09-15 03:50:59 +00:00
|
|
|
import flatpages from 'static/flatpages';
|
|
|
|
|
2016-06-26 05:43:28 +00:00
|
|
|
let DevTools;
|
|
|
|
if (__DEV__) {
|
2016-08-03 12:00:17 +00:00
|
|
|
DevTools = require('mobx-react-devtools').default; // eslint-disable-line global-require
|
2016-06-26 05:43:28 +00:00
|
|
|
}
|
2016-06-05 21:38:14 +00:00
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
let authenticatedStores;
|
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
type AuthProps = {
|
|
|
|
children?: React.Element<any>,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Auth = ({ children }: AuthProps) => {
|
2017-05-26 19:56:10 +00:00
|
|
|
if (stores.user.authenticated && stores.user.team) {
|
|
|
|
// Only initialize stores once. Kept in global scope
|
|
|
|
// because otherwise they will get overriden on route
|
|
|
|
// change
|
|
|
|
if (!authenticatedStores) {
|
|
|
|
// Stores for authenticated user
|
|
|
|
authenticatedStores = {
|
|
|
|
collections: new CollectionsStore({
|
|
|
|
teamId: stores.user.team.id,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
authenticatedStores.collections.fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex auto>
|
|
|
|
<Provider {...authenticatedStores}>
|
|
|
|
{children}
|
|
|
|
</Provider>
|
|
|
|
</Flex>
|
|
|
|
);
|
2017-05-17 07:11:13 +00:00
|
|
|
} else {
|
|
|
|
return <Redirect to="/" />;
|
2016-02-27 21:53:11 +00:00
|
|
|
}
|
2017-05-17 07:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const notFoundSearch = () => <Search notFound />;
|
|
|
|
const KeyboardShortcuts = () => (
|
|
|
|
<Flatpage title="Keyboard shortcuts" content={flatpages.keyboard} />
|
|
|
|
);
|
|
|
|
const Api = () => <Flatpage title="API" content={flatpages.api} />;
|
|
|
|
const DocumentNew = () => <Document newDocument />;
|
|
|
|
const DocumentNewChild = () => <Document newChildDocument />;
|
2016-06-05 01:28:14 +00:00
|
|
|
|
2017-04-28 04:48:13 +00:00
|
|
|
render(
|
2016-08-11 11:32:56 +00:00
|
|
|
<div style={{ display: 'flex', flex: 1, height: '100%' }}>
|
2017-04-28 04:48:13 +00:00
|
|
|
<Provider {...stores}>
|
2017-05-17 07:11:13 +00:00
|
|
|
<Router>
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/" component={Home} />
|
2016-06-26 06:31:22 +00:00
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
<Route exact path="/auth/slack" component={SlackAuth} />
|
|
|
|
<Route exact path="/auth/slack/commands" component={SlackAuth} />
|
|
|
|
<Route exact path="/auth/error" component={ErrorAuth} />
|
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
<Auth>
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/dashboard" component={Dashboard} />
|
2017-05-26 19:56:10 +00:00
|
|
|
<Route exact path="/collections/:id" component={Collection} />
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route exact path="/d/:id" component={Document} />
|
2017-05-27 04:58:16 +00:00
|
|
|
<Route exact path="/d/:id/:edit" component={Document} />
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path="/collections/:id/new"
|
|
|
|
component={DocumentNew}
|
|
|
|
/>
|
|
|
|
<Route exact path="/d/:id/new" component={DocumentNewChild} />
|
2016-07-18 03:59:32 +00:00
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route exact path="/search" component={Search} />
|
2017-05-22 06:02:53 +00:00
|
|
|
<Route exact path="/search/:query" component={Search} />
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route exact path="/settings" component={Settings} />
|
2016-08-03 11:55:56 +00:00
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path="/keyboard-shortcuts"
|
|
|
|
component={KeyboardShortcuts}
|
|
|
|
/>
|
|
|
|
<Route exact path="/developers" component={Api} />
|
2016-09-15 03:50:59 +00:00
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
<Route path="/404" component={Error404} />
|
|
|
|
<Route component={notFoundSearch} />
|
|
|
|
</Switch>
|
|
|
|
</Auth>
|
|
|
|
</Switch>
|
2017-04-29 21:14:11 +00:00
|
|
|
</Router>
|
2016-06-26 06:31:22 +00:00
|
|
|
</Provider>
|
2017-05-12 00:23:56 +00:00
|
|
|
{DevTools && <DevTools position={{ bottom: 0, right: 0 }} />}
|
2017-04-28 04:48:13 +00:00
|
|
|
</div>,
|
|
|
|
document.getElementById('root')
|
|
|
|
);
|