import React from "react";
import { useTranslation } from "react-i18next";
import { Container, Row } from "react-bootstrap";
import { useHistory, useLocation } from "react-router";
import { Link } from "react-router-dom";
import { HttpNetworkError } from "@/http/common";
import { getHttpSearchClient } from "@/http/search";
import { HttpTimelineInfo } from "@/http/timeline";
import SearchInput from "../common/SearchInput";
import UserAvatar from "../common/user/UserAvatar";
const TimelineSearchResultItemView: React.FC<{
timeline: HttpTimelineInfo;
}> = ({ timeline }) => {
const link = timeline.name.startsWith("@")
? `users/${timeline.owner.username}`
: `timelines/${timeline.name}`;
return (
{timeline.title}
{timeline.name}
{timeline.owner.nickname}
@{timeline.owner.username}
);
};
const SearchPage: React.FC = () => {
const { t } = useTranslation();
const history = useHistory();
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const queryParam = searchParams.get("q");
const [searchText, setSearchText] = React.useState("");
const [state, setState] = React.useState<
HttpTimelineInfo[] | "init" | "loading" | "network-error" | "error"
>("init");
const [forceResearchKey, setForceResearchKey] = React.useState(0);
React.useEffect(() => {
setState("init");
if (queryParam != null && queryParam.length > 0) {
setSearchText(queryParam);
setState("loading");
void getHttpSearchClient()
.searchTimelines(queryParam)
.then(
(ts) => {
setState(ts);
},
(e) => {
if (e instanceof HttpNetworkError) {
setState("network-error");
} else {
setState("error");
}
}
);
}
}, [queryParam, forceResearchKey]);
return (
{
if (queryParam === searchText) {
setForceResearchKey((old) => old + 1);
} else {
history.push(`/search?q=${searchText}`);
}
}}
/>
{(() => {
switch (state) {
case "init": {
if (queryParam == null || queryParam.length === 0) {
return {t("searchPage.input")}
;
}
break;
}
case "loading": {
return {t("searchPage.loading")}
;
}
case "network-error": {
return {t("error.network")}
;
}
case "error": {
return {t("error.unknown")}
;
}
default: {
if (state.length === 0) {
return {t("searchPage.noResult")}
;
}
return state.map((t) => (
));
}
}
})()}
);
};
export default SearchPage;