2024-10-19 21:10:59 +02:00
|
|
|
import React, {ChangeEventHandler, EventHandler, useEffect, useState} from 'react';
|
2024-10-18 02:10:58 +02:00
|
|
|
import {MangaConnector} from "./MangaConnector";
|
|
|
|
import IMangaConnector from "./interfaces/IMangaConnector";
|
|
|
|
import {isValidUri} from "../App";
|
2024-10-19 19:52:28 +02:00
|
|
|
import IManga, {SearchResult} from "./interfaces/IManga";
|
2024-10-18 19:44:15 +02:00
|
|
|
import '../styles/search.css';
|
2024-10-19 19:52:28 +02:00
|
|
|
import '../styles/MangaSearchResult.css'
|
2024-10-18 02:10:58 +02:00
|
|
|
|
2024-10-20 21:29:07 +02:00
|
|
|
export default function Search({apiUri, jobInterval, onJobsChanged, closeSearch} : {apiUri: string, jobInterval: Date, onJobsChanged: (internalId: string) => void, closeSearch(): void}) {
|
2024-10-18 02:10:58 +02:00
|
|
|
const [mangaConnectors, setConnectors] = useState<IMangaConnector[]>();
|
|
|
|
const [selectedConnector, setSelectedConnector] = useState<IMangaConnector>();
|
|
|
|
const [selectedLanguage, setSelectedLanguage] = useState<string>();
|
|
|
|
const [searchBoxValue, setSearchBoxValue] = useState("");
|
|
|
|
const [searchResults, setSearchResults] = useState<IManga[]>();
|
|
|
|
|
|
|
|
const pattern = /https:\/\/([a-z0-9.]+\.[a-z0-9]{2,})(?:\/.*)?/i
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if(mangaConnectors === undefined) {
|
|
|
|
MangaConnector.GetAllConnectors().then(setConnectors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, [mangaConnectors]);
|
|
|
|
|
|
|
|
const selectedConnectorChanged : ChangeEventHandler<HTMLSelectElement> = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
if(mangaConnectors === undefined)
|
|
|
|
return;
|
|
|
|
const selectedConnector = mangaConnectors.find((con: IMangaConnector) => con.name == event.target.value);
|
|
|
|
if(selectedConnector === undefined)
|
|
|
|
return;
|
|
|
|
setSelectedConnector(selectedConnector);
|
|
|
|
setSelectedLanguage(selectedConnector.SupportedLanguages[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const searchBoxValueChanged : ChangeEventHandler<HTMLInputElement> = (event) => {
|
2024-10-19 20:50:14 +02:00
|
|
|
event.currentTarget.style.width = event.target.value.length + "ch";
|
2024-10-18 02:10:58 +02:00
|
|
|
if(mangaConnectors === undefined)
|
|
|
|
return;
|
|
|
|
var str : string = event.target.value;
|
|
|
|
setSearchBoxValue(str);
|
|
|
|
if(isValidUri(str))
|
|
|
|
setSelectedConnector(undefined);
|
|
|
|
const match = str.match(pattern);
|
|
|
|
if(match === null)
|
|
|
|
return;
|
|
|
|
let baseUri = match[1];
|
|
|
|
const selectCon = mangaConnectors.find((con: IMangaConnector) => {
|
2024-10-19 21:10:59 +02:00
|
|
|
return con.BaseUris.find(uri => uri == baseUri);
|
2024-10-18 02:10:58 +02:00
|
|
|
});
|
|
|
|
if(selectCon != undefined){
|
|
|
|
setSelectedConnector(selectCon);
|
|
|
|
setSelectedLanguage(selectCon.SupportedLanguages[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 00:05:13 +02:00
|
|
|
const ExecuteSearch : EventHandler<any> = () => {
|
2024-10-18 02:10:58 +02:00
|
|
|
if(searchBoxValue.length < 1 || selectedConnector === undefined || selectedLanguage === ""){
|
|
|
|
console.error("Tried initiating search while not all fields where submitted.")
|
|
|
|
return;
|
|
|
|
}
|
2024-10-20 20:16:32 +02:00
|
|
|
//console.info(`Searching Name: ${searchBoxValue} Connector: ${selectedConnector.name} Language: ${selectedLanguage}`);
|
2024-10-18 02:10:58 +02:00
|
|
|
if(isValidUri(searchBoxValue) && !selectedConnector.BaseUris.find((uri: string) => {
|
|
|
|
const match = searchBoxValue.match(pattern);
|
|
|
|
if(match === null)
|
|
|
|
return false;
|
|
|
|
return match[1] == uri
|
|
|
|
}))
|
|
|
|
{
|
|
|
|
console.error("URL in Searchbox detected, but does not match selected connector.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!isValidUri(searchBoxValue)){
|
|
|
|
MangaConnector.GetMangaFromConnectorByTitle(selectedConnector, searchBoxValue)
|
|
|
|
.then((mangas: IManga[]) => {
|
|
|
|
setSearchResults(mangas);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
MangaConnector.GetMangaFromConnectorByUrl(selectedConnector, searchBoxValue)
|
|
|
|
.then((manga: IManga) => {
|
|
|
|
setSearchResults([manga]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const changeSelectedLanguage : ChangeEventHandler<HTMLSelectElement> = (event) => setSelectedLanguage(event.target.value);
|
|
|
|
|
2024-10-19 20:57:03 +02:00
|
|
|
return (<div id="Search">
|
2024-10-18 02:10:58 +02:00
|
|
|
<div id="SearchBox">
|
2024-10-19 21:02:00 +02:00
|
|
|
<input type="text" placeholder="Manganame" id="Searchbox-Manganame" onKeyDown={(e) => {if(e.key == "Enter") ExecuteSearch(null);}} onChange={searchBoxValueChanged}></input>
|
2024-10-19 19:52:28 +02:00
|
|
|
<select id="Searchbox-Connector" value={selectedConnector === undefined ? "" : selectedConnector.name} onChange={selectedConnectorChanged}>
|
2024-10-18 02:10:58 +02:00
|
|
|
<option value="" disabled hidden>Select</option>
|
|
|
|
{mangaConnectors === undefined
|
|
|
|
? <option value="Loading">Loading</option>
|
|
|
|
: mangaConnectors.map(con => <option value={con.name} key={con.name}>{con.name}</option>)}
|
|
|
|
</select>
|
2024-10-19 19:52:28 +02:00
|
|
|
<select id="Searchbox-language" onChange={changeSelectedLanguage} value={selectedLanguage === null ? "" : selectedLanguage}>
|
2024-10-18 02:10:58 +02:00
|
|
|
{selectedConnector === undefined
|
|
|
|
? <option value="" disabled hidden>Select Connector</option>
|
2024-10-20 17:27:02 +02:00
|
|
|
: selectedConnector.SupportedLanguages.map(language => <option value={language} key={language}>{language}</option>)}
|
2024-10-18 02:10:58 +02:00
|
|
|
</select>
|
2024-10-19 19:52:28 +02:00
|
|
|
<button id="Searchbox-button" type="submit" onClick={ExecuteSearch}>Search</button>
|
2024-10-18 02:10:58 +02:00
|
|
|
</div>
|
2024-10-19 21:10:59 +02:00
|
|
|
<img alt="Close Search" id="closeSearch" src="../media/close-x.svg" onClick={closeSearch} />
|
2024-10-19 19:52:28 +02:00
|
|
|
<div id="SearchResults">
|
2024-10-18 02:10:58 +02:00
|
|
|
{searchResults === undefined
|
2024-10-19 19:52:28 +02:00
|
|
|
? <p></p>
|
2024-10-20 21:29:07 +02:00
|
|
|
: searchResults.map(result => SearchResult(apiUri, result, jobInterval, onJobsChanged))}
|
2024-10-18 02:10:58 +02:00
|
|
|
</div>
|
|
|
|
</div>)
|
|
|
|
}
|