Skip to content

Commit

Permalink
optimize search
Browse files Browse the repository at this point in the history
  • Loading branch information
huxulm committed Feb 24, 2024
1 parent 5a55863 commit 2560213
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
19 changes: 16 additions & 3 deletions src/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ const LC_HOST = `https://leetcode.cn`;

export default function Search() {
const [filter, setFilter] = useState("");
const { solutions, isPending: solLoading } = useSolutions(filter);
const { solutions: _solutions, isPending: solLoading } = useSolutions(filter);
const { tags: qtags, isPending: tgLoading } = useQuestionTags(filter);
const { tags } = useTags();
const onSearchTextChange = (e: React.ChangeEvent) => {
// @ts-ignore
setFilter(e.target.value);
}

const solutions = useMemo(() => {
let result: any = {};
Object.keys(_solutions).forEach(id => {
let v = _solutions[id];
let key: string = v[6];
if (filter === "" || v[3].indexOf(filter) != -1 || v[0].indexOf(filter) != -1 || v[4].indexOf(filter) != -1) {
result[key] = v; // title_slug_hash => question
}
});
return result;
}, [filter]);

const [lang, setLang] = useState("zh");
const onChangeLang = () => {
setLang(() => lang === "en" ? "zh" : "en")
Expand Down Expand Up @@ -50,7 +63,7 @@ export default function Search() {
if (Object.keys(selectedTags).filter(id => !!selectedTags[id]).length == 0 || tags.filter((id: string) => true === selectedTags[id]).length > 0) { return true; }
return false;
// return true;
}), [selectedTags, solLoading]);
}), [selectedTags, solLoading, solutions]);

function backToTop() {
document.body.scrollTop = 0;
Expand Down Expand Up @@ -97,7 +110,7 @@ export default function Search() {
<Row as="div" className="justify-content-center p-3 position-sticky top-0 z-3 bg-white" style={{ zIndex: 1000 }}>
<Row md={12} sm={12} lg={12} className="justify-content-center" style={{ gap: '.5rem' }}>
<Col md={5} sm={12} lg={5} className="position-relative">
<input className="form-control fw-light" placeholder="题目 或 题解标题(模糊匹配)" onChange={onSearchTextChange}></input>
<input className="form-control fw-light" placeholder="题号、标目、题解标题(模糊匹配)" onChange={onSearchTextChange}></input>
<span className="qtot">总数:{filteredSolutions.length}</span>
</Col>
<Col md={2} sm={12} lg={2}>
Expand Down
12 changes: 2 additions & 10 deletions src/hooks/useSolutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@ export function useSolutions(filter: any) {
.then((res) => res.json())
.then((result: Solutions) => {
startTransition(() => {
let _solutions: any = {};
Object.keys(result).forEach(id => {
let v = result[id];
let key: string = v[6];
if (filter === "" || v[0].indexOf(filter) != -1 || v[4].indexOf(filter) != -1) {
_solutions[key] = v; // title_slug_hash => question
}
});
setSolutions(_solutions);
setSolutions(result);
});
});
}, [filter]);

return {solutions, isPending};
return { solutions, isPending };
}

0 comments on commit 2560213

Please sign in to comment.