2023-02-10 17:10:13 +03:00
|
|
|
|
import './search.css';
|
|
|
|
|
|
2023-09-04 09:49:39 +03:00
|
|
|
|
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
2023-06-28 18:36:37 +03:00
|
|
|
|
import { InView } from 'react-intersection-observer';
|
2023-02-12 14:36:55 +03:00
|
|
|
|
import { useParams, useSearchParams } from 'react-router-dom';
|
2023-02-10 17:10:13 +03:00
|
|
|
|
|
2023-02-12 14:29:03 +03:00
|
|
|
|
import AccountBlock from '../components/account-block';
|
2023-02-11 12:01:43 +03:00
|
|
|
|
import Icon from '../components/icon';
|
2023-02-10 17:10:13 +03:00
|
|
|
|
import Link from '../components/link';
|
2023-02-11 11:27:52 +03:00
|
|
|
|
import Loader from '../components/loader';
|
2023-04-26 08:09:44 +03:00
|
|
|
|
import NavMenu from '../components/nav-menu';
|
2023-09-04 09:49:39 +03:00
|
|
|
|
import SearchForm from '../components/search-form';
|
2023-02-10 17:10:13 +03:00
|
|
|
|
import Status from '../components/status';
|
|
|
|
|
import { api } from '../utils/api';
|
2023-02-11 12:57:26 +03:00
|
|
|
|
import useTitle from '../utils/useTitle';
|
2023-02-10 17:10:13 +03:00
|
|
|
|
|
2023-06-28 18:36:37 +03:00
|
|
|
|
const SHORT_LIMIT = 5;
|
|
|
|
|
const LIMIT = 40;
|
|
|
|
|
|
2023-02-18 15:48:24 +03:00
|
|
|
|
function Search(props) {
|
2023-02-12 14:36:55 +03:00
|
|
|
|
const params = useParams();
|
|
|
|
|
const { masto, instance, authenticated } = api({
|
|
|
|
|
instance: params.instance,
|
|
|
|
|
});
|
2023-08-14 06:22:42 +03:00
|
|
|
|
const [uiState, setUIState] = useState('default');
|
2023-04-29 15:59:51 +03:00
|
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
|
const searchFormRef = useRef();
|
2023-02-18 15:48:24 +03:00
|
|
|
|
const q = props?.query || searchParams.get('q');
|
2023-04-29 15:59:51 +03:00
|
|
|
|
const type = props?.type || searchParams.get('type');
|
|
|
|
|
useTitle(
|
|
|
|
|
q
|
|
|
|
|
? `Search: ${q}${
|
|
|
|
|
type
|
|
|
|
|
? ` (${
|
|
|
|
|
{
|
|
|
|
|
statuses: 'Posts',
|
|
|
|
|
accounts: 'Accounts',
|
|
|
|
|
hashtags: 'Hashtags',
|
|
|
|
|
}[type]
|
|
|
|
|
})`
|
|
|
|
|
: ''
|
|
|
|
|
}`
|
|
|
|
|
: 'Search',
|
|
|
|
|
`/search`,
|
|
|
|
|
);
|
2023-02-11 12:57:26 +03:00
|
|
|
|
|
2023-06-28 18:36:37 +03:00
|
|
|
|
const [showMore, setShowMore] = useState(false);
|
|
|
|
|
const offsetRef = useRef(0);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
offsetRef.current = 0;
|
2023-07-14 08:16:41 +03:00
|
|
|
|
}, [q, type]);
|
2023-06-28 18:36:37 +03:00
|
|
|
|
|
|
|
|
|
const scrollableRef = useRef();
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
scrollableRef.current?.scrollTo?.(0, 0);
|
|
|
|
|
}, [q, type]);
|
|
|
|
|
|
2023-02-10 17:10:13 +03:00
|
|
|
|
const [statusResults, setStatusResults] = useState([]);
|
|
|
|
|
const [accountResults, setAccountResults] = useState([]);
|
2023-02-10 19:05:18 +03:00
|
|
|
|
const [hashtagResults, setHashtagResults] = useState([]);
|
2023-07-14 08:16:41 +03:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
setStatusResults([]);
|
|
|
|
|
setAccountResults([]);
|
|
|
|
|
setHashtagResults([]);
|
|
|
|
|
}, [q]);
|
|
|
|
|
const setTypeResultsFunc = {
|
|
|
|
|
statuses: setStatusResults,
|
|
|
|
|
accounts: setAccountResults,
|
|
|
|
|
hashtags: setHashtagResults,
|
|
|
|
|
};
|
2023-06-28 18:36:37 +03:00
|
|
|
|
|
|
|
|
|
function loadResults(firstLoad) {
|
2023-08-14 06:22:42 +03:00
|
|
|
|
setUIState('loading');
|
2023-06-28 18:36:37 +03:00
|
|
|
|
if (firstLoad && !type) {
|
|
|
|
|
setStatusResults(statusResults.slice(0, SHORT_LIMIT));
|
|
|
|
|
setAccountResults(accountResults.slice(0, SHORT_LIMIT));
|
|
|
|
|
setHashtagResults(hashtagResults.slice(0, SHORT_LIMIT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
const params = {
|
|
|
|
|
q,
|
|
|
|
|
resolve: authenticated,
|
|
|
|
|
limit: SHORT_LIMIT,
|
|
|
|
|
};
|
|
|
|
|
if (type) {
|
|
|
|
|
params.limit = LIMIT;
|
|
|
|
|
params.type = type;
|
|
|
|
|
params.offset = offsetRef.current;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const results = await masto.v2.search(params);
|
|
|
|
|
console.log(results);
|
2023-07-14 08:16:41 +03:00
|
|
|
|
if (type) {
|
|
|
|
|
if (firstLoad) {
|
|
|
|
|
setTypeResultsFunc[type](results[type]);
|
|
|
|
|
const length = results[type]?.length;
|
|
|
|
|
offsetRef.current = LIMIT;
|
|
|
|
|
setShowMore(!!length);
|
|
|
|
|
} else {
|
|
|
|
|
setTypeResultsFunc[type]((prev) => [...prev, ...results[type]]);
|
|
|
|
|
const length = results[type]?.length;
|
|
|
|
|
offsetRef.current = offsetRef.current + LIMIT;
|
|
|
|
|
setShowMore(!!length);
|
2023-06-28 18:36:37 +03:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setStatusResults(results.statuses);
|
|
|
|
|
setAccountResults(results.accounts);
|
|
|
|
|
setHashtagResults(results.hashtags);
|
2023-07-14 08:16:41 +03:00
|
|
|
|
offsetRef.current = 0;
|
|
|
|
|
setShowMore(false);
|
2023-06-28 18:36:37 +03:00
|
|
|
|
}
|
2023-08-14 06:22:42 +03:00
|
|
|
|
setUIState('default');
|
2023-06-28 18:36:37 +03:00
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
2023-08-14 06:22:42 +03:00
|
|
|
|
setUIState('error');
|
2023-06-28 18:36:37 +03:00
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 17:10:13 +03:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (q) {
|
2023-04-29 15:59:51 +03:00
|
|
|
|
searchFormRef.current?.setValue?.(q);
|
2023-06-28 18:36:37 +03:00
|
|
|
|
loadResults(true);
|
2023-09-04 09:49:39 +03:00
|
|
|
|
} else {
|
|
|
|
|
searchFormRef.current?.focus?.();
|
2023-02-10 17:10:13 +03:00
|
|
|
|
}
|
2023-04-29 15:59:51 +03:00
|
|
|
|
}, [q, type, instance]);
|
2023-02-10 17:10:13 +03:00
|
|
|
|
|
2023-09-04 09:49:39 +03:00
|
|
|
|
useHotkeys(
|
|
|
|
|
'/',
|
|
|
|
|
(e) => {
|
|
|
|
|
searchFormRef.current?.focus?.();
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
preventDefault: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-02-10 17:10:13 +03:00
|
|
|
|
return (
|
2023-06-28 18:36:37 +03:00
|
|
|
|
<div id="search-page" class="deck-container" ref={scrollableRef}>
|
2023-02-10 17:10:13 +03:00
|
|
|
|
<div class="timeline-deck deck">
|
|
|
|
|
<header>
|
|
|
|
|
<div class="header-grid">
|
|
|
|
|
<div class="header-side">
|
2023-04-26 08:09:44 +03:00
|
|
|
|
<NavMenu />
|
2023-02-10 17:10:13 +03:00
|
|
|
|
</div>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
<SearchForm ref={searchFormRef} />
|
|
|
|
|
<div class="header-side"> </div>
|
2023-02-10 17:10:13 +03:00
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
<main>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
{!!q && (
|
|
|
|
|
<div class="filter-bar">
|
2023-09-04 12:01:06 +03:00
|
|
|
|
{!!type && (
|
|
|
|
|
<Link to={`/search${q ? `?q=${encodeURIComponent(q)}` : ''}`}>
|
|
|
|
|
‹ All
|
|
|
|
|
</Link>
|
|
|
|
|
)}
|
2023-04-29 15:59:51 +03:00
|
|
|
|
{[
|
|
|
|
|
{
|
|
|
|
|
label: 'Accounts',
|
|
|
|
|
type: 'accounts',
|
2023-09-04 12:01:06 +03:00
|
|
|
|
to: `/search?q=${encodeURIComponent(q)}&type=accounts`,
|
2023-04-29 15:59:51 +03:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Hashtags',
|
|
|
|
|
type: 'hashtags',
|
2023-09-04 12:01:06 +03:00
|
|
|
|
to: `/search?q=${encodeURIComponent(q)}&type=hashtags`,
|
2023-04-29 15:59:51 +03:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Posts',
|
|
|
|
|
type: 'statuses',
|
2023-09-04 12:01:06 +03:00
|
|
|
|
to: `/search?q=${encodeURIComponent(q)}&type=statuses`,
|
2023-04-29 15:59:51 +03:00
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
.sort((a, b) => {
|
|
|
|
|
if (a.type === type) return -1;
|
|
|
|
|
if (b.type === type) return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
})
|
|
|
|
|
.map((link) => (
|
|
|
|
|
<Link to={link.to}>{link.label}</Link>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2023-06-28 18:36:37 +03:00
|
|
|
|
{!!q ? (
|
2023-02-10 19:05:18 +03:00
|
|
|
|
<>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
{(!type || type === 'accounts') && (
|
|
|
|
|
<>
|
|
|
|
|
{type !== 'accounts' && (
|
|
|
|
|
<h2 class="timeline-header">Accounts</h2>
|
|
|
|
|
)}
|
|
|
|
|
{accountResults.length > 0 ? (
|
|
|
|
|
<>
|
|
|
|
|
<ul class="timeline flat accounts-list">
|
|
|
|
|
{accountResults.map((account) => (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
<li key={account.id}>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
<AccountBlock
|
|
|
|
|
account={account}
|
|
|
|
|
instance={instance}
|
2023-07-02 13:02:30 +03:00
|
|
|
|
showStats
|
2023-04-29 15:59:51 +03:00
|
|
|
|
/>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
{type !== 'accounts' && (
|
|
|
|
|
<div class="ui-state">
|
|
|
|
|
<Link
|
|
|
|
|
class="plain button"
|
|
|
|
|
to={`/search?q=${q}&type=accounts`}
|
|
|
|
|
>
|
|
|
|
|
See more accounts <Icon icon="arrow-right" />
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
!type &&
|
|
|
|
|
(uiState === 'loading' ? (
|
|
|
|
|
<p class="ui-state">
|
|
|
|
|
<Loader abrupt />
|
|
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<p class="ui-state">No accounts found.</p>
|
|
|
|
|
))
|
2023-04-29 15:59:51 +03:00
|
|
|
|
)}
|
|
|
|
|
</>
|
2023-02-10 19:05:18 +03:00
|
|
|
|
)}
|
2023-04-29 15:59:51 +03:00
|
|
|
|
{(!type || type === 'hashtags') && (
|
|
|
|
|
<>
|
|
|
|
|
{type !== 'hashtags' && (
|
|
|
|
|
<h2 class="timeline-header">Hashtags</h2>
|
|
|
|
|
)}
|
|
|
|
|
{hashtagResults.length > 0 ? (
|
|
|
|
|
<>
|
|
|
|
|
<ul class="link-list hashtag-list">
|
|
|
|
|
{hashtagResults.map((hashtag) => (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
<li key={hashtag.name}>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
<Link
|
|
|
|
|
to={
|
|
|
|
|
instance
|
|
|
|
|
? `/${instance}/t/${hashtag.name}`
|
|
|
|
|
: `/t/${hashtag.name}`
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Icon icon="hashtag" />
|
|
|
|
|
<span>{hashtag.name}</span>
|
|
|
|
|
</Link>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
{type !== 'hashtags' && (
|
|
|
|
|
<div class="ui-state">
|
|
|
|
|
<Link
|
|
|
|
|
class="plain button"
|
|
|
|
|
to={`/search?q=${q}&type=hashtags`}
|
|
|
|
|
>
|
|
|
|
|
See more hashtags <Icon icon="arrow-right" />
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
!type &&
|
|
|
|
|
(uiState === 'loading' ? (
|
|
|
|
|
<p class="ui-state">
|
|
|
|
|
<Loader abrupt />
|
|
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<p class="ui-state">No hashtags found.</p>
|
|
|
|
|
))
|
2023-04-29 15:59:51 +03:00
|
|
|
|
)}
|
|
|
|
|
</>
|
2023-02-10 19:05:18 +03:00
|
|
|
|
)}
|
2023-04-29 15:59:51 +03:00
|
|
|
|
{(!type || type === 'statuses') && (
|
|
|
|
|
<>
|
|
|
|
|
{type !== 'statuses' && (
|
|
|
|
|
<h2 class="timeline-header">Posts</h2>
|
|
|
|
|
)}
|
|
|
|
|
{statusResults.length > 0 ? (
|
|
|
|
|
<>
|
|
|
|
|
<ul class="timeline">
|
|
|
|
|
{statusResults.map((status) => (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
<li key={status.id}>
|
2023-04-29 15:59:51 +03:00
|
|
|
|
<Link
|
|
|
|
|
class="status-link"
|
|
|
|
|
to={
|
|
|
|
|
instance
|
|
|
|
|
? `/${instance}/s/${status.id}`
|
|
|
|
|
: `/s/${status.id}`
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Status status={status} />
|
|
|
|
|
</Link>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
{type !== 'statuses' && (
|
|
|
|
|
<div class="ui-state">
|
|
|
|
|
<Link
|
|
|
|
|
class="plain button"
|
|
|
|
|
to={`/search?q=${q}&type=statuses`}
|
|
|
|
|
>
|
|
|
|
|
See more posts <Icon icon="arrow-right" />
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
2023-06-30 04:48:52 +03:00
|
|
|
|
!type &&
|
|
|
|
|
(uiState === 'loading' ? (
|
|
|
|
|
<p class="ui-state">
|
|
|
|
|
<Loader abrupt />
|
|
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<p class="ui-state">No posts found.</p>
|
|
|
|
|
))
|
2023-04-29 15:59:51 +03:00
|
|
|
|
)}
|
|
|
|
|
</>
|
2023-02-10 19:05:18 +03:00
|
|
|
|
)}
|
2023-06-28 18:36:37 +03:00
|
|
|
|
{!!type &&
|
|
|
|
|
(uiState === 'default' ? (
|
|
|
|
|
showMore ? (
|
|
|
|
|
<InView
|
|
|
|
|
onChange={(inView) => {
|
|
|
|
|
if (inView) {
|
|
|
|
|
loadResults();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
class="plain block"
|
|
|
|
|
onClick={() => loadResults()}
|
|
|
|
|
style={{ marginBlockEnd: '6em' }}
|
|
|
|
|
>
|
|
|
|
|
Show more…
|
|
|
|
|
</button>
|
|
|
|
|
</InView>
|
|
|
|
|
) : (
|
|
|
|
|
<p class="ui-state insignificant">The end.</p>
|
|
|
|
|
)
|
|
|
|
|
) : (
|
2023-07-14 08:16:41 +03:00
|
|
|
|
uiState === 'loading' && (
|
2023-06-28 18:36:37 +03:00
|
|
|
|
<p class="ui-state">
|
|
|
|
|
<Loader abrupt />
|
|
|
|
|
</p>
|
|
|
|
|
)
|
|
|
|
|
))}
|
2023-02-10 19:05:18 +03:00
|
|
|
|
</>
|
2023-02-11 11:27:52 +03:00
|
|
|
|
) : uiState === 'loading' ? (
|
|
|
|
|
<p class="ui-state">
|
|
|
|
|
<Loader abrupt />
|
|
|
|
|
</p>
|
2023-02-10 19:05:18 +03:00
|
|
|
|
) : (
|
2023-02-11 12:57:44 +03:00
|
|
|
|
<p class="ui-state">
|
|
|
|
|
Enter your search term or paste a URL above to get started.
|
|
|
|
|
</p>
|
2023-02-10 17:10:13 +03:00
|
|
|
|
)}
|
|
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Search;
|