You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/components/SearchBar.tsx

119 lines
3.6 KiB
TypeScript

import { useEffect, useState, useRef } from "react";
import { useTranslation } from "react-i18next";
import useDebounce from "../hooks/useDebounce";
import { useLocationStore, useDialogStore } from "../store/module";
4 years ago
import { memoSpecialTypes } from "../helpers/filter";
import Icon from "./Icon";
import "../less/search-bar.less";
4 years ago
const SearchBar = () => {
const { t } = useTranslation();
const locationStore = useLocationStore();
const dialogStore = useDialogStore();
const memoType = locationStore.state.query.type;
const [queryText, setQueryText] = useState("");
const [isFocus, setIsFocus] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (!inputRef.current) {
return;
}
if (dialogStore.getState().dialogStack.length) {
return;
}
const isMetaKey = event.ctrlKey || event.metaKey;
if (isMetaKey && event.key === "f") {
event.preventDefault();
inputRef.current.focus();
return;
}
};
document.body.addEventListener("keydown", handleKeyDown);
return () => {
document.body.removeEventListener("keydown", handleKeyDown);
};
}, []);
useEffect(() => {
const text = locationStore.getState().query.text;
setQueryText(text === undefined ? "" : text);
}, [locationStore.state.query.text]);
4 years ago
useDebounce(
() => {
locationStore.setTextQuery(queryText.length === 0 ? undefined : queryText);
},
200,
[queryText]
);
const handleMemoTypeItemClick = (type: MemoSpecType | undefined) => {
const { type: prevType } = locationStore.getState().query ?? {};
4 years ago
if (type === prevType) {
type = undefined;
4 years ago
}
locationStore.setMemoTypeQuery(type);
4 years ago
};
const handleTextQueryInput = (event: React.FormEvent<HTMLInputElement>) => {
const text = event.currentTarget.value;
setQueryText(text);
4 years ago
};
const handleFocus = () => {
setIsFocus(true);
};
const handleBlur = () => {
setIsFocus(false);
};
4 years ago
return (
<div className={`search-bar-container ${isFocus ? "is-focus" : ""}`}>
4 years ago
<div className="search-bar-inputer">
<Icon.Search className="icon-img" />
<input
className="text-input"
autoComplete="new-password"
type="text"
placeholder=""
ref={inputRef}
value={queryText}
onChange={handleTextQueryInput}
onFocus={handleFocus}
onBlur={handleBlur}
/>
4 years ago
</div>
<div className="quickly-action-wrapper !hidden">
4 years ago
<div className="quickly-action-container">
<p className="title-text">{t("search.quickly-filter").toUpperCase()}</p>
4 years ago
<div className="section-container types-container">
<span className="section-text">{t("common.type").toUpperCase()}:</span>
4 years ago
<div className="values-container">
{memoSpecialTypes.map((type, idx) => {
4 years ago
return (
<div key={type.value}>
4 years ago
<span
className={`type-item ${memoType === type.value ? "selected" : ""}`}
4 years ago
onClick={() => {
handleMemoTypeItemClick(type.value as MemoSpecType);
4 years ago
}}
>
{t(type.text)}
4 years ago
</span>
{idx + 1 < memoSpecialTypes.length ? <span className="split-text">/</span> : null}
</div>
);
})}
</div>
</div>
</div>
</div>
</div>
);
};
export default SearchBar;