From 13c15825aa6b651439c043c75d9871e52c69cf9f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 28 Oct 2022 12:48:23 +0300 Subject: Upgrade to `react-router` v6 --- vnext/src/App.js | 58 ++++++++++++++------------------------------------ vnext/src/ui/Feeds.js | 49 ++++++++++++++++++++++++------------------ vnext/src/ui/Header.js | 8 +++---- vnext/src/ui/Login.js | 8 +++---- vnext/src/ui/Post.js | 6 +++--- 5 files changed, 55 insertions(+), 74 deletions(-) (limited to 'vnext/src') diff --git a/vnext/src/App.js b/vnext/src/App.js index b56e0300..79fde2d1 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -1,5 +1,5 @@ import { useState, useEffect, useRef } from 'react'; -import { Route, Link, Switch } from 'react-router-dom'; +import { Route, Link, Routes } from 'react-router-dom'; import { useScroll, useRafState } from 'react-use'; import qs from 'qs'; @@ -183,47 +183,21 @@ export default function App({ footer }) { }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> +
diff --git a/vnext/src/ui/Feeds.js b/vnext/src/ui/Feeds.js index d6813130..c9199fa6 100644 --- a/vnext/src/ui/Feeds.js +++ b/vnext/src/ui/Feeds.js @@ -1,5 +1,5 @@ -import { useState, useEffect } from 'react'; -import { Link, useLocation, useHistory, useParams } from 'react-router-dom'; +import { useState, useEffect, useLayoutEffect } from 'react'; +import { Link, useLocation, useParams, Navigate } from 'react-router-dom'; import qs from 'qs'; import moment from 'moment'; @@ -25,6 +25,19 @@ import { getMessages } from '../api'; * @property {import('../api').Message[]=} msgs */ +function RequireAuth({ visitor, children }) { + let location = useLocation(); + if (!visitor.hash) { + // Redirect them to the /login page, but save the current location they were + // trying to go to when they were redirected. This allows us to send them + // along to that page after they login, which is a nicer user experience + // than dropping them off on the home page. + return ; + } + + return children; +} + /** * @param {PageProps} props */ @@ -36,7 +49,7 @@ export function Discover({ visitor }) { search: search, pageParam: search.search ? 'page' : 'before_mid' }; - return (); + return (); } /** @@ -47,7 +60,7 @@ export function Discussions({ visitor }) { baseUrl: '/api/messages/discussions', pageParam: 'to' }; - return (); + return (); } /** @@ -70,7 +83,7 @@ export function Blog({ visitor }) {
- + ); } @@ -88,7 +101,7 @@ export function Tag({ visitor }) { }, pageParam: 'before_mid' }; - return (); + return (); } /** @@ -99,12 +112,15 @@ export function Home({ visitor }) { baseUrl: '/api/home', pageParam: 'before_mid' }; - return (); + return ( + + + + ); } /** * @typedef {Object} FeedState - * @property { boolean } authRequired * @property { import('../api').SecureUser } visitor * @property { import('../api').Message[]= } msgs * @property { Query} query @@ -113,11 +129,9 @@ export function Home({ visitor }) { /** * @param {FeedState} props */ -function Feed({ visitor, query, authRequired }) { +function Feed({ visitor, query }) { const location = useLocation(); - const history = useHistory(); const [state, setState] = useState({ - authRequired: authRequired, hash: visitor.hash, msgs: [], nextpage: null, @@ -125,7 +139,6 @@ function Feed({ visitor, query, authRequired }) { tag: '' }); const [loading, setLoading] = useState(true); - useEffect(() => { setLoading(true); const filter = location.search.substring(1); @@ -135,23 +148,17 @@ function Feed({ visitor, query, authRequired }) { newFilter[pageParam] = pageValue; return `?${qs.stringify(newFilter)}`; }; - document.body.scrollTop = 0; - document.documentElement.scrollTop = 0; const filterParams = qs.parse(filter); let params = Object.assign({}, filterParams || {}, query.search || {}); let url = query.baseUrl; - if (state.hash) { - params.hash = state.hash; - } - if (!params.hash && state.authRequired) { - history.push('/'); - } getMessages(url, params) .then(response => { const { data } = response; const { pageParam } = query; const lastMessage = data.slice(-1)[0] || {}; const nextpage = getPageParam(pageParam, lastMessage, filterParams); + document.body.scrollTop = 0; + document.documentElement.scrollTop = 0; setState((prevState) => { return { ...prevState, @@ -169,7 +176,7 @@ function Feed({ visitor, query, authRequired }) { }; }); }); - }, [location.search, state.hash, state.authRequired, history, query]); + }, [location.search, query]); return (state.msgs.length > 0 ? (
{ diff --git a/vnext/src/ui/Header.js b/vnext/src/ui/Header.js index 1574a489..3162c9ea 100644 --- a/vnext/src/ui/Header.js +++ b/vnext/src/ui/Header.js @@ -1,12 +1,12 @@ import { memo, useCallback } from 'react'; -import { Link, useHistory } from 'react-router-dom'; +import { Link, useNavigate } from 'react-router-dom'; import Icon from './Icon'; import { UserLink } from './UserInfo'; import SearchBox from './SearchBox'; function Header({ visitor, className }) { - const history = useHistory(); + const navigate = useNavigate(); /** * @param {string} searchString */ @@ -14,8 +14,8 @@ function Header({ visitor, className }) { let location = {}; location.pathname = '/discover'; location.search = `?search=${searchString}`; - history.push(location); - }, [history]); + navigate(location); + }, [navigate]); return (