Skip to content

Commit b866ebe

Browse files
authored
feat: enable search in relayPool table (#495)
1 parent 3ecbfe6 commit b866ebe

4 files changed

Lines changed: 100 additions & 11 deletions

File tree

src/core/relay/pool/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,38 @@ export class RelayPool {
7272
return this.relays;
7373
}
7474

75+
async findRelays(keyword: string, alwaysFetch = false) {
76+
const k = keyword.toLowerCase();
77+
const find = (r: Relay) =>
78+
r.url.toLowerCase().includes(k) || r.about?.toLowerCase().includes(k);
79+
80+
let relays = this.db.loadAll().filter(find);
81+
82+
if (alwaysFetch) {
83+
const urls = relays.map(r => r.url);
84+
const newRelays = (await this.fetchOnlineRelays()).filter(find);
85+
for (const r of newRelays) {
86+
if (!urls.includes(r.url)) {
87+
this.db.save(r);
88+
relays.push(r);
89+
}
90+
}
91+
92+
return filterDuplicateRelays(relays);
93+
}
94+
95+
if (relays.length === 0) {
96+
relays = (await this.fetchOnlineRelays()).filter(find);
97+
for (const r of relays) {
98+
this.db.save(r);
99+
}
100+
}
101+
102+
return filterDuplicateRelays(relays).sort(
103+
(a, b) => RelayTracker.success_rate(a) - RelayTracker.success_rate(b),
104+
);
105+
}
106+
75107
static async benchmark(
76108
urls: string[],
77109
progressCb?: (rest: number) => any,

src/pages/relay-manager/components/RelayPool/index.tsx

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ import {
1414
TableCell,
1515
} from 'components/shared/ui/Table';
1616
import { Relay } from 'core/relay/type';
17-
import useAllRelaysQuery, {
18-
useGetAllRelaysCountQuery,
19-
} from 'pages/relay-manager/hooks/useGetAllRelaysQuery';
2017
import { FaCircle } from 'react-icons/fa6';
2118
import { cn } from 'utils/classnames';
2219
import { Pagination } from 'components/shared/Pagination';
23-
import { useState } from 'react';
20+
import { useMemo, useState } from 'react';
2421
import { Button } from 'components/shared/ui/Button';
2522
import { toast } from 'components/shared/ui/Toast/use-toast';
23+
import useAllRelaysQuery, {
24+
useGetAllRelaysCountQuery,
25+
} from 'pages/relay-manager/hooks/useGetAllRelaysQuery';
26+
import useFindRelaysByKeywordQuery from 'pages/relay-manager/hooks/useFindRelayByKeywordQuery';
2627
import CopyToGroupModal from '../CopyToGroupModal';
2728
import useCopyRelaysMutation from 'pages/relay-manager/hooks/useCopyRelaysMutation';
2829

@@ -110,14 +111,39 @@ export const columns: ColumnDef<Relay>[] = [
110111
},
111112
];
112113

113-
export default function RelayPool() {
114+
export interface RelayPoolProp {
115+
keyword?: string;
116+
}
117+
118+
export default function RelayPool({ keyword }: RelayPoolProp) {
114119
const [pageIndex, setPageIndex] = useState(1);
115-
const { data: relays = [] } = useAllRelaysQuery(pageIndex, 20);
116-
const { data: totalCount = 0 } = useGetAllRelaysCountQuery();
120+
121+
const limit = 20;
122+
const isSearching = useMemo(() => keyword && keyword.length > 0, [keyword]);
123+
124+
const { data: searchResult } = useFindRelaysByKeywordQuery(
125+
keyword,
126+
pageIndex,
127+
limit,
128+
);
129+
const { data: relays = [] } = useAllRelaysQuery(pageIndex, limit);
130+
const { data: getAllTotalCount = 0 } = useGetAllRelaysCountQuery();
131+
132+
const pageCount = useMemo(() => {
133+
const totalCount = isSearching
134+
? (searchResult?.[1] as number)
135+
: getAllTotalCount;
136+
return Math.ceil(totalCount / 20);
137+
}, [isSearching, searchResult, getAllTotalCount]);
138+
139+
const tableData = useMemo(() => {
140+
return isSearching ? (searchResult?.[0] as Relay[]) || [] : relays;
141+
}, [isSearching, searchResult, relays]);
142+
117143
const copyMutation = useCopyRelaysMutation();
118144

119145
const table = useReactTable({
120-
data: relays,
146+
data: tableData,
121147
columns,
122148
getCoreRowModel: getCoreRowModel(),
123149
});
@@ -190,7 +216,7 @@ export default function RelayPool() {
190216
<div className="flex mt-8 mb-20 justify-center">
191217
<Pagination
192218
pageIndex={pageIndex}
193-
pageCount={Math.ceil(totalCount / 20)}
219+
pageCount={pageCount}
194220
onPageChange={(page: number) => setPageIndex(page)}
195221
/>
196222
</div>

src/pages/relay-manager/explore.page.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { useRouter } from 'next/router';
44
import { FaSearch } from 'react-icons/fa';
55
import { FaArrowLeft } from 'react-icons/fa6';
66
import RelayPool from './components/RelayPool';
7+
import { useState } from 'react';
8+
import { Input } from 'components/shared/ui/Input';
79

810
export default function RelayExplorePage() {
911
const router = useRouter();
12+
const [keyword, setKeyword] = useState<string>();
1013

1114
return (
1215
<BaseLayout>
@@ -24,13 +27,15 @@ export default function RelayExplorePage() {
2427
</div>
2528
<div className="w-[200px] bg-surface-02 border border-border-01 border-solid flex items-center gap-1 px-3 py-2 rounded-full">
2629
<FaSearch className="h-4 w-4 text-text-secondary" />
27-
<input
30+
<Input
2831
className="flex-1 border-none outline-none body text-md"
2932
placeholder="Search Relay..."
33+
value={keyword}
34+
onChange={e => setKeyword(e.currentTarget.value)}
3035
/>
3136
</div>
3237
</div>
33-
<RelayPool />
38+
<RelayPool keyword={keyword} />
3439
</div>
3540
</Left>
3641
</BaseLayout>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useRelayPool } from 'hooks/relay/useRelayManagerContext';
2+
import { useQuery } from '@tanstack/react-query';
3+
4+
export default function useFindRelaysByKeywordQuery(
5+
keyword: string | undefined,
6+
page = 1,
7+
limit = 10,
8+
) {
9+
const relayPool = useRelayPool();
10+
11+
const queryResult = useQuery({
12+
enabled: typeof keyword === 'string' && keyword.length > 0,
13+
queryKey: ['allRelays', keyword, page, limit],
14+
queryFn: async () => {
15+
if (!keyword) return [[], 0];
16+
17+
const relays = await relayPool.findRelays(keyword);
18+
const start = (page - 1) * limit;
19+
const end = start + limit;
20+
const relaysInPage = relays.slice(start, end);
21+
return [relaysInPage, relays.length];
22+
},
23+
});
24+
25+
return queryResult;
26+
}

0 commit comments

Comments
 (0)