ente/src/components/SearchBar.tsx

248 lines
7.5 KiB
TypeScript
Raw Normal View History

2021-05-18 14:20:15 +00:00
import { SetCollections, SetFiles } from 'pages/gallery';
2021-05-20 09:04:48 +00:00
import React, { useEffect, useState, useRef } from 'react';
2021-05-18 04:32:37 +00:00
import styled from 'styled-components';
import AsyncSelect from 'react-select/async';
import { components } from 'react-select';
2021-05-24 12:04:37 +00:00
import debounce from 'debounce-promise';
2021-05-20 08:00:02 +00:00
import { File, getLocalFiles } from 'services/fileService';
import {
Collection,
getLocalCollections,
getNonEmptyCollections,
} from 'services/collectionService';
import { Bbox, parseHumanDate, searchLocation } from 'services/searchService';
import {
getFilesWithCreationDay,
getFilesInsideBbox,
2021-05-24 12:19:18 +00:00
getFormattedDate,
} from 'utils/search';
import constants from 'utils/strings/constants';
import LocationIcon from './LocationIcon';
import DateIcon from './DateIcon';
2021-05-21 10:53:18 +00:00
import CrossIcon from './CrossIcon';
2021-05-18 04:32:37 +00:00
const Wrapper = styled.div<{ open: boolean }>`
background-color: #111;
color: #fff;
min-height: 64px;
align-items: center;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.7);
margin-bottom: 10px;
position: fixed;
top: 0;
width: 100%;
2021-05-18 07:47:03 +00:00
display: flex;
align-items: center;
justify-content: center;
2021-05-18 06:52:11 +00:00
z-index: 200;
2021-05-18 07:47:03 +00:00
padding: 0 20%;
display: ${(props) => (props.open ? 'flex' : 'none')};
2021-05-18 04:32:37 +00:00
`;
2021-05-24 12:19:18 +00:00
enum SuggestionType {
DATE,
LOCATION,
}
2021-05-24 12:19:18 +00:00
interface Suggestion {
type: SuggestionType;
label: string;
value: Bbox | Date;
2021-05-18 07:47:03 +00:00
}
interface Props {
2021-05-18 14:20:15 +00:00
isOpen: boolean;
2021-05-18 07:47:03 +00:00
setOpen: (value) => void;
2021-05-18 14:20:15 +00:00
loadingBar: any;
setFiles: SetFiles;
setCollections: SetCollections;
2021-05-18 07:47:03 +00:00
}
export default function SearchBar(props: Props) {
2021-05-20 08:00:02 +00:00
const [allFiles, setAllFiles] = useState<File[]>([]);
const [allCollections, setAllCollections] = useState<Collection[]>([]);
useEffect(() => {
const main = async () => {
setAllFiles(await getLocalFiles());
setAllCollections(await getLocalCollections());
};
main();
2021-05-24 13:54:14 +00:00
}, [props.isOpen]);
2021-05-18 14:20:15 +00:00
2021-05-20 09:04:48 +00:00
const searchBarRef = useRef(null);
useEffect(() => {
if (props.isOpen) {
setTimeout(() => {
searchBarRef.current?.focus();
}, 200);
}
}, [props.isOpen]);
const getAutoCompleteSuggestion = async (searchPhrase: string) => {
2021-05-24 12:19:18 +00:00
let option = new Array<Suggestion>();
2021-05-24 12:22:05 +00:00
if (!searchPhrase?.length) {
return option;
}
const searchedDate = parseHumanDate(searchPhrase);
if (searchedDate != null) {
option.push({
2021-05-24 12:19:18 +00:00
type: SuggestionType.DATE,
value: searchedDate,
2021-05-24 12:19:18 +00:00
label: getFormattedDate(searchedDate),
});
}
const searchResults = await searchLocation(searchPhrase);
option.push(
...searchResults.map(
(searchResult) =>
({
2021-05-24 12:19:18 +00:00
type: SuggestionType.LOCATION,
value: searchResult.bbox,
label: searchResult.placeName,
2021-05-24 12:19:18 +00:00
} as Suggestion)
)
);
return option;
};
2021-05-20 08:19:37 +00:00
2021-05-24 13:54:14 +00:00
const getOptions = debounce(getAutoCompleteSuggestion, 250);
2021-05-24 12:04:37 +00:00
2021-05-24 12:19:18 +00:00
const filterFiles = (selectedOption: Suggestion) => {
if (!selectedOption) {
return;
}
2021-05-20 08:00:02 +00:00
let resultFiles: File[] = [];
2021-05-20 08:19:37 +00:00
switch (selectedOption.type) {
2021-05-24 12:19:18 +00:00
case SuggestionType.DATE:
const searchedDate = selectedOption.value as Date;
const filesWithSameDate = getFilesWithCreationDay(
allFiles,
searchedDate
);
resultFiles = filesWithSameDate;
break;
2021-05-24 12:19:18 +00:00
case SuggestionType.LOCATION:
const bbox = selectedOption.value as Bbox;
2021-05-20 08:19:37 +00:00
const filesTakenAtLocation = getFilesInsideBbox(allFiles, bbox);
resultFiles = filesTakenAtLocation;
2021-05-20 08:00:02 +00:00
}
2021-05-20 08:19:37 +00:00
2021-05-20 08:00:02 +00:00
props.setFiles(resultFiles);
props.setCollections(
getNonEmptyCollections(allCollections, resultFiles)
);
2021-05-18 14:20:15 +00:00
};
2021-05-20 08:00:02 +00:00
const closeSearchBar = ({ resetForm }) => {
2021-05-18 14:20:15 +00:00
props.setOpen(false);
2021-05-20 08:00:02 +00:00
props.setFiles(allFiles);
props.setCollections(allCollections);
2021-05-18 14:20:15 +00:00
resetForm();
};
2021-05-24 12:19:18 +00:00
const getIconByType = (type: SuggestionType) =>
type === SuggestionType.DATE ? <DateIcon /> : <LocationIcon />;
2021-05-21 13:29:06 +00:00
2021-05-24 12:19:18 +00:00
const LabelWithIcon = (props: { type: SuggestionType; label: string }) => (
2021-05-21 13:29:06 +00:00
<div style={{ display: 'flex', alignItems: 'center' }}>
<span style={{ marginRight: '10px' }}>
{getIconByType(props.type)}
</span>
<span>{props.label}</span>
</div>
);
const { Option, SingleValue } = components;
const SingleValueWithIcon = (props) => (
<SingleValue {...props}>
2021-05-21 13:29:06 +00:00
<LabelWithIcon type={props.data.type} label={props.data.label} />
</SingleValue>
);
const OptionWithIcon = (props) => (
<Option {...props}>
2021-05-21 13:29:06 +00:00
<LabelWithIcon type={props.data.type} label={props.data.label} />
</Option>
);
const customStyles = {
control: (provided, { isFocused }) => ({
...provided,
backgroundColor: '#282828',
color: '#d1d1d1',
borderColor: isFocused && '#2dc262',
boxShadow: isFocused && '0 0 3px #2dc262',
':hover': {
borderColor: '#2dc262',
},
}),
input: (provided) => ({
...provided,
color: '#d1d1d1',
}),
menu: (provided) => ({
...provided,
2021-05-21 13:29:06 +00:00
marginTop: '10px',
backgroundColor: '#282828',
}),
option: (provided, { isFocused }) => ({
...provided,
backgroundColor: isFocused && '#343434',
}),
dropdownIndicator: (provided) => ({
...provided,
display: 'none',
}),
indicatorSeparator: (provided) => ({
...provided,
display: 'none',
}),
clearIndicator: (provided) => ({
...provided,
':hover': { color: '#d2d2d2' },
}),
singleValue: (provided, state) => ({
...provided,
backgroundColor: '#282828',
color: '#d1d1d1',
}),
};
2021-05-21 09:55:19 +00:00
2021-05-18 07:47:03 +00:00
return (
2021-05-18 14:20:15 +00:00
<Wrapper open={props.isOpen}>
<>
<div
style={{
flex: 1,
maxWidth: '600px',
margin: '10px',
}}
>
<AsyncSelect
components={{
Option: OptionWithIcon,
SingleValue: SingleValueWithIcon,
}}
ref={searchBarRef}
placeholder={constants.SEARCH_HINT}
loadOptions={getOptions}
onChange={filterFiles}
isClearable
escapeClearsValue
styles={customStyles}
noOptionsMessage={() => null}
/>
</div>
<div
style={{
margin: '0',
display: 'flex',
alignItems: 'center',
cursor: 'pointer',
}}
onClick={() => closeSearchBar({ resetForm: () => null })}
>
2021-05-21 10:53:18 +00:00
<CrossIcon />
</div>
</>
2021-05-18 07:47:03 +00:00
</Wrapper>
);
2021-05-18 04:32:37 +00:00
}