Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for feature requests #32 and #299 #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 108 additions & 3 deletions src/routes/Search/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,72 @@ import * as API from 'types/api';


const SEARCH_PERIOD = 4000;
var urlSearchExecuted = 0;

interface SearchProps extends RouteComponentProps<{}> {

}

function getParameters(inputString: string) {
var params = {};

var tempString = inputString.trim();
while (tempString.startsWith('-')) {
var spaceIndex = tempString.search(' ');
var argString = '';
if (spaceIndex > 0) {
argString = tempString.substring(0, spaceIndex);
tempString = tempString.substring(spaceIndex).trim();
spaceIndex = tempString.search(' ');
if (spaceIndex > 0) {
var valueString = tempString.substring(0, spaceIndex);
tempString = tempString.substring(spaceIndex).trim();
params[argString] = valueString;
}
}
}

// If there are only arguments and no actual search term,
// just return complete input instead.
if (tempString.length === 0) {
alert('Parameter parsing failed. Only parameters in search string: ' + inputString);
return [{}, inputString];
}

return [params, tempString];
}

function sanitizeFileType(inputString: string) {
if (inputString === 'audio') { return 'audio'; }

if (inputString === 'archive') { return 'compressed'; }
if (inputString === 'compressed') { return 'compressed'; }

if (inputString === 'doc') { return 'document'; }
if (inputString === 'document') { return 'document'; }

if (inputString === 'exe') { return 'executable'; }
if (inputString === 'executable') { return 'executable'; }

if (inputString === 'img') { return 'picture'; }
if (inputString === 'picture') { return 'picture'; }

if (inputString === 'video') { return 'video'; }
if (inputString === 'filelist') { return 'filelist'; }
if (inputString === 'other') { return 'other'; }
if (inputString === 'any') { return 'any'; }
if (inputString === 'tth') { return 'tth'; }

if (inputString === 'dir') { return 'directory'; }
if (inputString === 'directory') { return 'directory'; }

if (inputString === 'file') { return 'file'; }

alert('Bad file type \"' + inputString + '\". Using \"any\" instead.');

return 'any';
}

class Search extends React.Component<SearchProps> {
state = {
searchString: '',
Expand Down Expand Up @@ -55,13 +116,30 @@ class Search extends React.Component<SearchProps> {

search = (searchString: string) => {
console.log('Searching');
var fileType = 'any';
var fileTypeSelector = document.getElementById('file-type-selector');
if (fileTypeSelector != null) {
fileType = (fileTypeSelector as HTMLInputElement).value;
}

//inputString = searchString.trim();
var params;
var queryString;
[params, queryString] = getParameters(searchString);
console.log('Search: ' + queryString);
if (params['-t'] != null) {
fileType = params['-t'];
fileType = sanitizeFileType(fileType);
console.log('Search file_type: ' + fileType);
}

clearTimeout(this._searchTimeout);

SocketService.post(SearchConstants.HUB_SEARCH_URL, {
query: {
pattern: searchString,
},
pattern: queryString,
file_type: fileType,
},
priority: API.PriorityEnum.HIGH,
})
.then(this.onSearchPosted)
Expand All @@ -88,18 +166,45 @@ class Search extends React.Component<SearchProps> {

render() {
const { searchString, running } = this.state;
var searchDefault = searchString;
var url = new URL(window.location.href);
var urlSearch = url.searchParams.get('query');
if (urlSearch != null) {
searchDefault = urlSearch;
if (urlSearchExecuted === 0) { // render() is called again when searching
urlSearchExecuted = 1; // This ensures search is only executed once.
console.log('Search term from url: ' + searchDefault);
// Execute search with timeout, when page is loaded.
setTimeout(
() => {
this.search(searchDefault);
},
100
);
}
}
return (
<OfflineHubMessageDecorator
offlineMessage="You must to be connected to at least one hub in order to perform searches"
>
<div className="search-layout">
<div className="search-container">
<div className="search-area">
<select id="file-type-selector" className="file-type-selector" >
<option value="any">Any</option>
<option value="audio">Audio</option>
<option value="compressed">Archive</option>
<option value="document">Document</option>
<option value="executable">Executable</option>
<option value="picture">Picture</option>
<option value="video">Video</option>
<option value="directory">Directory</option>
</select>
<HistoryInput
historyId={ HistoryStringEnum.SEARCH }
submitHandler={ this.search }
disabled={ running }
defaultValue={ searchString }
defaultValue={ searchDefault }
placeholder="Enter search string..."
button={
<Button
Expand Down
6 changes: 5 additions & 1 deletion src/routes/Search/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
display: inline-block !important;
}

.file-type-selector {
float:left;
}

.search-layout {
display: flex;
flex-direction: column;
Expand All @@ -49,4 +53,4 @@
.result.modal .download.dropdown {
max-width: 150px;
margin-top: 10px;
}
}