addded searchStats

This commit is contained in:
Abhinav-grd 2021-05-26 12:40:36 +05:30
parent 8581843736
commit 25c17c687e
2 changed files with 82 additions and 36 deletions

View file

@ -52,6 +52,25 @@ const SearchButton = styled.div`
align-items: center; align-items: center;
justify-content: center; justify-content: center;
`; `;
const SearchStats = styled.div`
@media (min-width: 1000px) {
width: 1000px;
}
@media (min-width: 450px) and (max-width: 1000px) {
width: 600px;
}
@media (max-width: 450px) {
width: 100%;
}
margin: auto;
color: #979797;
display: flex;
align-items: center;
`;
enum SuggestionType { enum SuggestionType {
DATE, DATE,
LOCATION, LOCATION,
@ -68,11 +87,15 @@ interface Props {
setFiles: SetFiles; setFiles: SetFiles;
setCollections: SetCollections; setCollections: SetCollections;
} }
interface Stats {
resultCount: number;
timeTaken: number;
}
export default function SearchBar(props: Props) { export default function SearchBar(props: Props) {
const [allFiles, setAllFiles] = useState<File[]>([]); const [allFiles, setAllFiles] = useState<File[]>([]);
const [allCollections, setAllCollections] = useState<Collection[]>([]); const [allCollections, setAllCollections] = useState<Collection[]>([]);
const [windowWidth, setWindowWidth] = useState(window.innerWidth); const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const [stats, setStats] = useState<Stats>(null);
const selectRef = useRef(null); const selectRef = useRef(null);
useEffect(() => { useEffect(() => {
if (!props.isOpen && allFiles?.length > 0) { if (!props.isOpen && allFiles?.length > 0) {
@ -126,6 +149,7 @@ export default function SearchBar(props: Props) {
if (!selectedOption) { if (!selectedOption) {
return; return;
} }
const startTime = Date.now();
props.setOpen(true); props.setOpen(true);
let resultFiles: File[] = []; let resultFiles: File[] = [];
@ -148,6 +172,11 @@ export default function SearchBar(props: Props) {
props.setCollections( props.setCollections(
getNonEmptyCollections(allCollections, resultFiles) getNonEmptyCollections(allCollections, resultFiles)
); );
const timeTaken = (Date.now() - startTime) / 1000;
setStats({
timeTaken,
resultCount: resultFiles.length,
});
}; };
const resetSearch = () => { const resetSearch = () => {
if (props.isOpen) { if (props.isOpen) {
@ -253,7 +282,9 @@ export default function SearchBar(props: Props) {
}), }),
}; };
return windowWidth > 450 || props.isOpen ? ( return (
<>
{windowWidth > 450 || props.isOpen ? (
<Wrapper> <Wrapper>
<div <div
style={{ style={{
@ -279,7 +310,10 @@ export default function SearchBar(props: Props) {
</div> </div>
<div style={{ width: '24px' }}> <div style={{ width: '24px' }}>
{props.isOpen && ( {props.isOpen && (
<div style={{ cursor: 'pointer' }} onClick={resetSearch}> <div
style={{ cursor: 'pointer' }}
onClick={resetSearch}
>
<CrossIcon /> <CrossIcon />
</div> </div>
)} )}
@ -289,5 +323,10 @@ export default function SearchBar(props: Props) {
<SearchButton onClick={() => props.setOpen(true)}> <SearchButton onClick={() => props.setOpen(true)}>
<SearchIcon /> <SearchIcon />
</SearchButton> </SearchButton>
)}
{props.isOpen && (
<SearchStats>{constants.SEARCH_STATS(stats)}</SearchStats>
)}
</>
); );
} }

View file

@ -324,6 +324,13 @@ const englishConstants = {
with ente with ente
</p> </p>
), ),
SEARCH_STATS: ({ resultCount, timeTaken }) => (
<span>
found <span style={{ color: '#2dc262' }}>{resultCount}</span>{' '}
memories ( <span style={{ color: '#2dc262' }}> {timeTaken}</span>{' '}
seconds )
</span>
),
}; };
export default englishConstants; export default englishConstants;