2021-03-21 10:55:27 +01:00
|
|
|
import React, { useEffect, useState } from "react";
|
2020-07-08 19:36:36 +02:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
2021-03-21 18:05:24 +01:00
|
|
|
import selectedTheme from "../lib/theme";
|
2020-07-08 19:36:36 +02:00
|
|
|
|
2021-03-21 10:55:27 +01:00
|
|
|
import { Button } from "./elements";
|
|
|
|
|
|
|
|
const Search = styled.form`
|
|
|
|
width: 100%;
|
|
|
|
height: 2rem;
|
|
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
|
|
padding-top: 0.25rem;
|
|
|
|
`;
|
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
const SearchInput = styled.input`
|
|
|
|
width: 100%;
|
2021-06-14 11:29:03 +02:00
|
|
|
margin: 0px;
|
2021-03-21 10:55:27 +01:00
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
font-size: 1rem;
|
2021-03-21 10:55:27 +01:00
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
border: none;
|
|
|
|
border-bottom: 1px solid ${selectedTheme.accentColor};
|
2021-06-14 11:29:03 +02:00
|
|
|
border-radius: 0;
|
2021-03-21 10:55:27 +01:00
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
background: none;
|
|
|
|
color: ${selectedTheme.mainColor};
|
2021-03-11 13:54:38 +01:00
|
|
|
|
|
|
|
:focus {
|
|
|
|
outline: none;
|
|
|
|
}
|
2020-07-08 19:36:36 +02:00
|
|
|
`;
|
|
|
|
|
2021-03-21 10:55:27 +01:00
|
|
|
const SearchButton = styled(Button)`
|
|
|
|
margin: 0px 2px;
|
|
|
|
min-height: 0;
|
|
|
|
`;
|
|
|
|
|
2021-03-05 22:00:32 +01:00
|
|
|
export interface ISearchProviderProps {
|
|
|
|
name: string;
|
|
|
|
url: string;
|
|
|
|
prefix: string;
|
|
|
|
}
|
2020-07-08 19:36:36 +02:00
|
|
|
|
2021-03-05 22:00:32 +01:00
|
|
|
interface ISearchBarProps {
|
|
|
|
providers: Array<ISearchProviderProps> | undefined;
|
|
|
|
}
|
2020-07-08 19:36:36 +02:00
|
|
|
|
2021-03-21 19:59:18 +01:00
|
|
|
/**
|
|
|
|
* Renders a search bar
|
2021-06-14 11:29:03 +02:00
|
|
|
* @param {ISearchBarProps} props - The search providers for the search bar to use
|
2021-03-21 19:59:18 +01:00
|
|
|
*/
|
2021-03-05 22:00:32 +01:00
|
|
|
const SearchBar = ({ providers }: ISearchBarProps) => {
|
2021-03-21 10:55:27 +01:00
|
|
|
let [input, setInput] = useState<string>("");
|
|
|
|
let [buttonsHidden, setButtonsHidden] = useState<boolean>(true);
|
|
|
|
|
2021-03-23 16:26:14 +01:00
|
|
|
useEffect(() => setButtonsHidden(input === ""), [input]);
|
2020-07-08 19:36:36 +02:00
|
|
|
|
|
|
|
const handleSearchQuery = (e: React.FormEvent) => {
|
2021-03-21 10:55:27 +01:00
|
|
|
var query: string = input || "";
|
2020-07-08 19:36:36 +02:00
|
|
|
|
|
|
|
if (query.split(" ")[0].includes("/")) {
|
|
|
|
handleQueryWithProvider(query);
|
|
|
|
} else {
|
|
|
|
window.location.href = "https://google.com/search?q=" + query;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleQueryWithProvider = (query: string) => {
|
2021-03-21 10:55:27 +01:00
|
|
|
let queryArray: Array<string> = query.split(" ");
|
|
|
|
let prefix: string = queryArray[0];
|
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
queryArray.shift();
|
|
|
|
|
2021-03-21 10:55:27 +01:00
|
|
|
let searchQuery: string = queryArray.join(" ");
|
2020-07-08 19:36:36 +02:00
|
|
|
|
2021-03-21 10:55:27 +01:00
|
|
|
let providerFound: boolean = false;
|
2021-03-05 22:00:32 +01:00
|
|
|
if (providers) {
|
|
|
|
providers.forEach((provider: ISearchProviderProps) => {
|
|
|
|
if (provider.prefix === prefix) {
|
|
|
|
providerFound = true;
|
|
|
|
window.location.href = provider.url + searchQuery;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-08 19:36:36 +02:00
|
|
|
|
|
|
|
if (!providerFound)
|
|
|
|
window.location.href = "https://google.com/search?q=" + query;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-03-21 10:55:27 +01:00
|
|
|
<Search onSubmit={(e) => handleSearchQuery(e)}>
|
2020-07-08 19:36:36 +02:00
|
|
|
<SearchInput
|
|
|
|
type="text"
|
2021-03-21 10:55:27 +01:00
|
|
|
value={input}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
|
|
|
setInput(e.target.value)
|
|
|
|
}
|
2020-07-08 19:36:36 +02:00
|
|
|
></SearchInput>
|
2021-03-21 10:55:27 +01:00
|
|
|
<SearchButton
|
|
|
|
type="button"
|
|
|
|
onClick={() => setInput("")}
|
|
|
|
hidden={buttonsHidden}
|
|
|
|
>
|
|
|
|
Clear
|
|
|
|
</SearchButton>
|
|
|
|
<SearchButton type="submit" hidden={buttonsHidden}>
|
|
|
|
Search
|
|
|
|
</SearchButton>
|
|
|
|
</Search>
|
2020-07-08 19:36:36 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchBar;
|