import React from "react" import * as antd from "antd" import classnames from "classnames" import { UserPreview } from "components" import { Icons, createIconRender } from "components/Icons" import FeedModel from "models/feed" import "./index.less" const ResultRenders = { users: (props) => { const { item, onClick } = props return
} } const ResultsTypeDecorators = { users: { icon: "Users", label: "Users" } } const Results = (props) => { let { results } = props if (!results) { return } if (typeof results !== "object") { return } let keys = Object.keys(results) if (keys.length === 0) { return } // check if all keys are valid, if not replace as "others" keys = keys.map((type) => { if (ResultRenders[type]) { return type } return "others" }) const handleOnClick = (type, value) => { if (typeof props.onClick !== "function") { console.warn("Searcher: onClick is not a function") return } return props.onClick(type, value) } return keys.map((type) => { const decorator = ResultsTypeDecorators[type] ?? { label: keys, icon: } return

{createIconRender(decorator.icon)}{decorator.label}

{ results[type].map((item) => { return React.createElement(ResultRenders[type], { item, onClick: (...props) => handleOnClick(type, ...props) }) }) }
}) } export default (props) => { const [loading, setLoading] = React.useState(false) const [searchResult, setSearchResult] = React.useState(null) const [searchValue, setSearchValue] = React.useState("") const makeSearch = async (value) => { if (value === "") { return setSearchResult(null) } const result = await FeedModel.search(value) return setSearchResult(result) } const handleOnSearch = (e) => { // not allow to input space as first character if (e.target.value[0] === " ") { return } setSearchValue(e.target.value) } const handleResultClick = (type, value) => { switch (type) { case "users": { app.navigation.goToAccount(value.username) break } case "posts": { app.navigation.goToPost(value) break } default: { console.warn("Searcher: cannot handle clicks on result of type :", type) break } } if (typeof props.close === "function") { props.close() } } React.useEffect(() => { const timer = setTimeout(async () => { setLoading(true) await makeSearch(searchValue) setLoading(false) }, 400) if (searchValue === "") { if (typeof props.onEmpty === "function") { props.onEmpty() } } else { if (typeof props.onFilled === "function") { props.onFilled() } } return () => clearTimeout(timer) }, [searchValue]) return
} autoFocus={props.autoFocus ?? false} onFocus={props.onFocus} onBlur={props.onUnfocus} /> {searchResult &&
{loading && } { !loading && }
}
}