aboutsummaryrefslogtreecommitdiffstats
path: root/src/web
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-29 21:27:24 -0500
committerGitHub <noreply@github.com>2021-05-29 21:27:24 -0500
commit92a594f9157311089fbe7b436cdb129a4f7f712d (patch)
tree460aa5bbad06a2134ddcd1c4f0a596f106982419 /src/web
parentc72d7088ce6d39193ae1a80f2808e34d07e6078f (diff)
parent4533681fbfc20e1e608f7ddf8ce4a105e281d481 (diff)
downloadslskd-92a594f9157311089fbe7b436cdb129a4f7f712d.tar.gz
Merge pull request #81 from slskd/persist-search
Persist search results to disk
Diffstat (limited to 'src/web')
-rw-r--r--src/web/src/components/Search/Search.js77
-rw-r--r--src/web/src/lib/search.js8
2 files changed, 60 insertions, 25 deletions
diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js
index 48e05775..60720c1a 100644
--- a/src/web/src/components/Search/Search.js
+++ b/src/web/src/components/Search/Search.js
@@ -18,7 +18,7 @@ import {
const initialState = {
searchPhrase: '',
- searchId: '',
+ searchId: undefined,
searchState: 'idle',
searchStatus: {
responseCount: 0,
@@ -31,6 +31,7 @@ const initialState = {
hideNoFreeSlots: true,
hiddenResults: [],
hideLocked: true,
+ fetching: false,
};
const sortOptions = {
@@ -50,13 +51,9 @@ class Search extends Component {
const searchPhrase = this.inputtext.inputRef.current.value;
const searchId = uuidv4();
- this.setState({ searchPhrase, searchId, searchState: 'pending' }, () => {
- search.search({ id: searchId, searchText: searchPhrase })
- .then(response => this.setState({ results: response.data }))
- .then(() => this.setState({ searchState: 'complete' }, () => {
- this.saveState();
- this.setSearchText();
- }))
+ this.setState({ searchPhrase, searchId, searchState: 'pending' }, async () => {
+ this.saveState();
+ search.search({ id: searchId, searchText: searchPhrase });
});
}
@@ -76,19 +73,20 @@ class Search extends Component {
saveState = () => {
try {
- localStorage.setItem('soulseek-example-search-state', JSON.stringify(this.state));
+ localStorage.setItem('soulseek-example-search-state', JSON.stringify({ ...this.state, results: [] }));
} catch(error) {
console.log(error);
}
}
loadState = () => {
- this.setState(JSON.parse(localStorage.getItem('soulseek-example-search-state')) || initialState);
+ return new Promise((resolve) =>
+ this.setState(JSON.parse(localStorage.getItem('soulseek-example-search-state')) || initialState, resolve));
}
- componentDidMount = () => {
- this.fetchStatus();
- this.loadState();
+ componentDidMount = async () => {
+ await this.loadState();
+ this.fetchResults();
this.setState({
interval: window.setInterval(this.fetchStatus, 500)
}, () => this.setSearchText());
@@ -107,12 +105,44 @@ class Search extends Component {
document.removeEventListener("keyup", this.keyUp, false);
}
- fetchStatus = () => {
- if (this.state.searchState === 'pending') {
- search.getStatus({ id: this.state.searchId })
- .then(response => this.setState({
- searchStatus: response
- }));
+ fetchResults = async () => {
+ const { searchId } = this.state;
+
+ if (!!searchId) {
+ this.setState({ fetching: true }, async () => {
+ try {
+ const responses = await search.getResponses({ id: searchId });
+
+ this.setState({
+ results: responses,
+ fetching: false
+ }, this.saveState);
+ } catch (error) {
+ console.log(error);
+ this.clear();
+ }
+ });
+ }
+ }
+
+ fetchStatus = async () => {
+ const { searchState, searchId } = this.state;
+
+ if (searchState === 'pending') {
+ this.setState({ fetching: true }, async () => {
+ const response = await search.getStatus({ id: searchId });
+
+ if (response.isComplete) {
+ this.setState({
+ searchState: 'complete'
+ }, this.fetchResults);
+ } else {
+ this.setState({
+ searchStatus: response,
+ fetching: false
+ }, this.saveState);
+ }
+ });
}
}
@@ -121,7 +151,7 @@ class Search extends Component {
}
sortAndFilterResults = () => {
- const { results, hideNoFreeSlots, resultSort, hideLocked, hiddenResults } = this.state;
+ const { results = [], hideNoFreeSlots, resultSort, hideLocked, hiddenResults = [] } = this.state;
const { field, order } = sortOptions[resultSort];
return results
@@ -154,8 +184,8 @@ class Search extends Component {
}
render = () => {
- let { searchState, searchStatus, results, displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults } = this.state;
- let pending = searchState === 'pending';
+ let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching } = this.state;
+ let pending = fetching || searchState === 'pending';
const sortedAndFilteredResults = this.sortAndFilterResults();
@@ -185,7 +215,8 @@ class Search extends Component {
inline='centered'
size='big'
>
- Found {searchStatus.fileCount} files {searchStatus.lockedFileCount > 0 ? `(plus ${searchStatus.lockedFileCount} locked) ` : ''}from {searchStatus.responseCount} users
+ {searchState === 'pending' ? <span>Found {searchStatus.fileCount} files {searchStatus.lockedFileCount > 0 ? `(plus ${searchStatus.lockedFileCount} locked) ` : ''}from {searchStatus.responseCount} users</span>
+ : 'Loading results...'}
</Loader>
:
<div>
diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js
index c71dba96..2179dcd2 100644
--- a/src/web/src/lib/search.js
+++ b/src/web/src/lib/search.js
@@ -4,6 +4,10 @@ export const search = ({ id, searchText }) => {
return api.post(`/searches`, { id, searchText });
};
-export const getStatus = async ({ id }) => {
- return (await api.get(`/searches/${encodeURIComponent(id)}`)).data;
+export const getStatus = async ({ id, includeResponses = false }) => {
+ return (await api.get(`/searches/${encodeURIComponent(id)}?includeResponses=${includeResponses}`)).data;
+};
+
+export const getResponses = async ({ id }) => {
+ return (await api.get(`/searches/${encodeURIComponent(id)}/responses`)).data;
}; \ No newline at end of file