File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import { hot } from 'react-hot-loader/root';
1313import { Switch , Route , Redirect , useLocation } from 'react-router-dom' ;
1414import CritterpediaPage from 'pages/Critterpedia/loadable' ;
1515import NotFoundPage from 'pages/404' ;
16+ import DisqusRedirect from 'pages/DisqusRedirect' ;
1617import { useInjectReducer } from 'redux-injectors' ;
1718import { CacheProvider } from '@emotion/core' ;
1819import createCache from '@emotion/cache' ;
@@ -57,6 +58,7 @@ const App = () => {
5758 path = { `/:mode(${ MODE_COLLECTION } |${ MODE_DISCOVERY } |${ MODE_ALL } )/:category(${ CATEGORY_FISH } |${ CATEGORY_INSECTS } |${ CATEGORY_SEA } )` }
5859 component = { CritterpediaPage }
5960 />
61+ < Route path = { `/comment-:commentId(\\d+)` } component = { DisqusRedirect } />
6062 < Route component = { NotFoundPage } />
6163 </ Switch >
6264 { /* the global modals */ }
Original file line number Diff line number Diff line change 1+ import React , { useEffect } from 'react' ;
2+ import { useHistory } from 'react-router-dom' ;
3+ import { waitAndScrollByElement } from './utils' ;
4+
5+ /**
6+ * Redirect to disqus comment when it's ready /comment-5621046783
7+ */
8+ const DisqusRedirect = ( ) => {
9+ // TODO: redirect to comment id
10+ // const { commentId } = useParams();
11+ const history = useHistory ( ) ;
12+ useEffect ( ( ) => {
13+ history . push ( '/' ) ;
14+ return ( ) => {
15+ waitAndScrollByElement ( '#disqus_thread' ) . catch ( ( ) => { } ) ;
16+ } ;
17+ } , [ ] ) ;
18+
19+ return < > </ > ;
20+ } ;
21+
22+ DisqusRedirect . propTypes = { } ;
23+
24+ export default DisqusRedirect ;
Original file line number Diff line number Diff line change 1+ export const waitAndScrollByElement = ( selector , interval = 1000 , retry = 10 ) =>
2+ new Promise ( ( resolve , reject ) => {
3+ let remainingRetry = retry ;
4+ const waitingForElement = ( ) => {
5+ remainingRetry -= 1 ;
6+ const element = document . querySelector ( selector ) ;
7+ if ( element ) {
8+ element . scrollIntoView ( ) ;
9+ resolve ( element ) ;
10+ } else if ( remainingRetry > 0 ) {
11+ setTimeout ( waitingForElement , interval ) ;
12+ } else {
13+ reject ( new Error ( 'element not found' ) ) ;
14+ }
15+ } ;
16+ waitingForElement ( ) ;
17+ } ) ;
You can’t perform that action at this time.
0 commit comments