forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.ts
73 lines (63 loc) · 1.63 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import MeiliSearch from 'meilisearch'
import { IndexerDumpState, WithChainId } from '@dao-dao/types'
import {
INACTIVE_DAO_NAMES,
SEARCH_API_KEY,
SEARCH_HOST,
getSupportedChainConfig,
} from '@dao-dao/utils'
let _client: MeiliSearch | undefined
export const loadMeilisearchClient = async (): Promise<MeiliSearch> => {
if (!_client) {
_client = new MeiliSearch({
host: SEARCH_HOST,
apiKey: SEARCH_API_KEY,
})
}
return _client
}
export type DaoSearchResult = {
chainId: string
contractAddress: string
codeId: number
blockHeight: number
blockTimeUnixMicro: number
value: IndexerDumpState
}
export type SearchDaosOptions = WithChainId<{
query: string
limit?: number
exclude?: string[]
}>
export const searchDaos = async ({
chainId,
query,
limit,
exclude,
}: SearchDaosOptions): Promise<DaoSearchResult[]> => {
const client = await loadMeilisearchClient()
const config = getSupportedChainConfig(chainId)
if (!config) {
return []
}
const index = client.index(config.indexes.search)
const results = await index.search<Omit<DaoSearchResult, 'chainId'>>(query, {
limit,
filter: [
// Exclude inactive DAOs.
`NOT value.config.name IN ["${INACTIVE_DAO_NAMES.join('", "')}"]`,
...(exclude?.length
? // Exclude DAOs that are in the exclude list.
[`NOT contractAddress IN ["${exclude.join('", "')}"]`]
: []),
]
.map((filter) => `(${filter})`)
.join(' AND '),
// Most recent at the top.
sort: ['blockHeight:desc', 'value.proposalCount:desc'],
})
return results.hits.map((hit) => ({
chainId,
...hit,
}))
}