this repo has no description
1import { useAutoAnimate } from '@formkit/auto-animate/preact';
2import { Trans, useLingui } from '@lingui/react/macro';
3import { useReducer } from 'preact/hooks';
4
5import { api } from '../utils/api';
6import {
7 addToSearchHistory,
8 clearAllSearchHistory,
9 getSearchHistory,
10 removeFromSearchHistory,
11} from '../utils/search-history';
12import showToast from '../utils/show-toast';
13
14import Icon from './icon';
15import Link from './link';
16import { generateSearchItemData } from './search-form';
17
18export default function RecentSearches({ onItemClick }) {
19 const { t } = useLingui();
20 const { instance } = api();
21 const [, reload] = useReducer((c) => c + 1, 0);
22 const history = getSearchHistory();
23
24 const handleClearAll = () => {
25 clearAllSearchHistory();
26 showToast({
27 text: t`Cleared recent searches`,
28 delay: 1000,
29 });
30 reload();
31 };
32
33 const handleRemoveItem = (query, queryType) => {
34 removeFromSearchHistory(query, queryType);
35 reload();
36 };
37
38 const [listRef] = useAutoAnimate();
39
40 if (history.length === 0) {
41 return null;
42 }
43
44 return (
45 <div class="recent-searches">
46 <div class="ui-state insignificant recent-searches-header">
47 <Icon icon="history" />{' '}
48 <span>
49 <Trans>Recent searches</Trans>
50 </span>
51 <span class="spacer" />
52 <button
53 type="button"
54 class="plain4 small"
55 onClick={handleClearAll}
56 disabled={history.length <= 0}
57 >
58 <span>
59 <Trans>Clear all</Trans>
60 </span>
61 </button>
62 </div>
63 <ul class="link-list recent-searches-list" ref={listRef}>
64 {history.map((historyItem) => {
65 const { label, to, icon } = generateSearchItemData(
66 historyItem.query,
67 historyItem.queryType,
68 instance,
69 );
70
71 return (
72 <li
73 key={`${historyItem.query}-${historyItem.queryType}-${historyItem.timestamp}`}
74 class="recent-searches-item"
75 >
76 <Link
77 to={to}
78 class="recent-searches-link"
79 onClick={(e) => {
80 addToSearchHistory(historyItem.query, historyItem.queryType);
81 onItemClick?.(e);
82 }}
83 >
84 <Icon icon={icon} class="more-insignificant" />
85 <span class="recent-searches-label">{label}</span>
86 </Link>
87 <button
88 type="button"
89 class="plain4 small"
90 onClick={() =>
91 handleRemoveItem(historyItem.query, historyItem.queryType)
92 }
93 >
94 <Icon icon="trash" alt={t`Clear`} />
95 </button>
96 </li>
97 );
98 })}
99 </ul>
100 </div>
101 );
102}