import './search.css';
import { useEffect, useRef, useState } from 'preact/hooks';
import { useSearchParams } from 'react-router-dom';
import Avatar from '../components/avatar';
import Link from '../components/link';
import Menu from '../components/menu';
import NameText from '../components/name-text';
import Status from '../components/status';
import { api } from '../utils/api';
function Search() {
const { masto, instance, authenticated } = api();
const [uiState, setUiState] = useState('default');
const [searchParams, setSearchParams] = useSearchParams();
const searchFieldRef = useRef();
const q = searchParams.get('q');
const [statusResults, setStatusResults] = useState([]);
const [accountResults, setAccountResults] = useState([]);
const [hashtagResults, setHashtagResults] = useState([]);
useEffect(() => {
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]);
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.
)}
>
) : (
Enter your search term above to get started.
)}
);
}
export default Search;