import './search.css';
import { useEffect, useRef, useState } from 'preact/hooks';
import { useParams, useSearchParams } from 'react-router-dom';
import AccountBlock from '../components/account-block';
import Icon from '../components/icon';
import Link from '../components/link';
import Loader from '../components/loader';
import Menu from '../components/menu';
import Status from '../components/status';
import { api } from '../utils/api';
import useTitle from '../utils/useTitle';
function Search() {
const params = useParams();
const { masto, instance, authenticated } = api({
instance: params.instance,
});
const [uiState, setUiState] = useState('default');
const [searchParams, setSearchParams] = useSearchParams();
const searchFieldRef = useRef();
const q = searchParams.get('q');
useTitle(q ? `Search: ${q}` : 'Search', `/search`);
const [statusResults, setStatusResults] = useState([]);
const [accountResults, setAccountResults] = useState([]);
const [hashtagResults, setHashtagResults] = useState([]);
useEffect(() => {
searchFieldRef.current?.focus?.();
if (q) {
searchFieldRef.current.value = q;
setUiState('loading');
(async () => {
const results = await masto.v2.search({
q,
limit: 20,
resolve: authenticated,
});
console.log(results);
setStatusResults(results.statuses);
setAccountResults(results.accounts);
setHashtagResults(results.hashtags);
setUiState('default');
})();
}
}, [q, instance]);
return (
{!!q && uiState !== 'loading' ? (
<>
{accountResults.length > 0 ? (
{accountResults.map((account) => (
-
))}
) : (
No accounts found.
)}
{hashtagResults.length > 0 ? (
{hashtagResults.map((hashtag) => (
-
{hashtag.name}
))}
) : (
No hashtags found.
)}
{statusResults.length > 0 ? (
{statusResults.map((status) => (
-
))}
) : (
No posts found.
)}
>
) : uiState === 'loading' ? (
) : (
Enter your search term or paste a URL above to get started.
)}
);
}
export default Search;