From 581fa92428ec49f63421119a7bfcfef4e46df80a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 2 May 2022 22:36:10 -0500 Subject: fix routing --- src/web/src/components/App.js | 20 +++++++++++--------- src/web/src/components/Search/List/SearchListRow.js | 7 ++++++- src/web/src/components/Search/Searches.js | 8 ++++++++ src/web/src/config.js | 17 ++++++++++++++--- src/web/src/lib/api.js | 6 +++--- 5 files changed, 42 insertions(+), 16 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 8fa6bcaa..75db94b2 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -1,5 +1,7 @@ import React, { Component } from 'react'; -import { Route, Link, Switch } from "react-router-dom"; +import { Route, Link, Switch, Redirect } from "react-router-dom"; + +import { reactRouterBaseUrl } from '../config'; import * as session from '../lib/session'; import { connect, disconnect } from '../lib/server'; @@ -151,37 +153,37 @@ class App extends Component { {version.isCanary && Canary } - + Search - + Downloads - + Uploads - + Rooms - + Chat - + Users - + Browse @@ -251,7 +253,7 @@ class App extends Component { this.withTokenCheck()}/> this.withTokenCheck()}/> this.withTokenCheck()}/> - this.withTokenCheck()}/> + diff --git a/src/web/src/components/Search/List/SearchListRow.js b/src/web/src/components/Search/List/SearchListRow.js index 24d24082..efac68a7 100644 --- a/src/web/src/components/Search/List/SearchListRow.js +++ b/src/web/src/components/Search/List/SearchListRow.js @@ -1,4 +1,8 @@ import React, { useState } from 'react'; +import { + Link, + useRouteMatch, +} from "react-router-dom"; import SearchStatusIcon from './SearchStatusIcon'; import SearchActionIcon from './SearchActionIcon'; @@ -10,6 +14,7 @@ import { const SearchListRow = ({ search, onRemove, onStop }) => { const [working, setWorking] = useState(false); + const match = useRouteMatch(); const invoke = async (func) => { setWorking(true); @@ -27,7 +32,7 @@ const SearchListRow = ({ search, onRemove, onStop }) => { state={search.state} /> - {search.searchText} + {search.searchText} {search.fileCount} {search.lockedFileCount} {search.responseCount} diff --git a/src/web/src/components/Search/Searches.js b/src/web/src/components/Search/Searches.js index 8748aade..e770b829 100644 --- a/src/web/src/components/Search/Searches.js +++ b/src/web/src/components/Search/Searches.js @@ -1,4 +1,11 @@ import React, { useState, useEffect, useRef } from 'react'; +import { + BrowserRouter as Router, + Switch, + Route, + Link, + useParams +} from "react-router-dom"; import { v4 as uuidv4 } from 'uuid'; @@ -19,6 +26,7 @@ const Searches = () => { const [{ creating, createError }, setCreating] = useState({ creating: false, createError: false }); const [error, setError] = useState(undefined); const [searches, setSearches] = useState({}); + const params = useParams(); const inputRef = useRef(); const onConnecting = () => setConnected({ connected: false, connecting: true, connectError: false }) diff --git a/src/web/src/config.js b/src/web/src/config.js index ea71e9dd..4e0781f5 100644 --- a/src/web/src/config.js +++ b/src/web/src/config.js @@ -1,17 +1,28 @@ const rootUrl = process.env.NODE_ENV === 'production' ? '' : 'http://localhost:5000/'; -const baseUrl = `${rootUrl}api/v0`; +const apiBaseUrl = `${rootUrl}api/v0`; const tokenKey = 'slskd-token'; const tokenPassthroughValue = 'n/a'; const activeChatKey = 'slskd-active-chat'; const activeRoomKey = 'slskd-active-room'; const activeUserInfoKey = 'slskd-active-user'; +// url base is the value of the url base from config; +// the route used when behind a reverse proxy. we +// need this value so we can be explicit with react-router. +let reactRouterBaseUrl; + +const setReactRouterBaseUrl = (url) => { + reactRouterBaseUrl = (url === '/' ? '' : url); +}; + export { rootUrl, - baseUrl, + apiBaseUrl, + reactRouterBaseUrl, tokenKey, tokenPassthroughValue, activeChatKey, activeRoomKey, - activeUserInfoKey + activeUserInfoKey, + setReactRouterBaseUrl, }; diff --git a/src/web/src/lib/api.js b/src/web/src/lib/api.js index ef1fd31a..02fe2eca 100644 --- a/src/web/src/lib/api.js +++ b/src/web/src/lib/api.js @@ -1,8 +1,8 @@ import axios from 'axios'; -import { baseUrl, setUrlBase } from '../config'; +import { apiBaseUrl, setReactRouterBaseUrl } from '../config'; import * as session from './session'; -axios.defaults.baseURL = baseUrl; +axios.defaults.baseURL = apiBaseUrl; const api = axios.create({ withCredentials: true @@ -24,7 +24,7 @@ api.interceptors.response.use(response => { // when behind a reverse proxy, we need the configured url base // so that we can be explicit with react-router. every API response // should contain this header, and it should be the same. - setUrlBase(response.headers['x-url-base']); + setReactRouterBaseUrl(response.headers['x-url-base']); return response; }, error => { if (error.response.status === 401 && !['/session', '/server', '/application'].includes(error.response.config.url)) { -- cgit v1.2.3