This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
helper.ts
139 lines (132 loc) · 3.74 KB
/
helper.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
UserCollectionMetadata,
UserTrackMetadata,
UserMetadata,
removeNullable
} from '@audius/common'
import * as adapter from './ResponseAdapter'
import { APIResponse, APISearch } from './types'
const SEARCH_MAX_SAVED_RESULTS = 10
const SEARCH_MAX_TOTAL_RESULTS = 50
const AUTOCOMPLETE_MAX_SAVED_RESULTS = 2
const AUTOCOMPLETE_TOTAL_RESULTS = 3
/**
* Combines two lists by concatting `maxSaved` results from the `savedList` onto the head of `normalList`,
* ensuring that no item is duplicated in the resulting list (deduped by `uniqueKey`). The final list length is capped
* at `maxTotal` items.
*/
const combineLists = (
savedList: Array<Record<string, any>>,
normalList: Array<Record<string, any>>,
uniqueKey: string,
maxSaved: number,
maxTotal: number
) => {
const truncatedSavedList = savedList.slice(
0,
Math.min(maxSaved, savedList.length)
)
const saveListsSet = new Set(truncatedSavedList.map((s) => s[uniqueKey]))
const filteredList = normalList.filter((n) => !saveListsSet.has(n[uniqueKey]))
const combinedLists = savedList.concat(filteredList)
return combinedLists.slice(0, Math.min(maxTotal, combinedLists.length))
}
type ProcessSearchResultsArgs = {
tracks?: UserTrackMetadata[]
albums?: UserCollectionMetadata[]
playlists?: UserCollectionMetadata[]
users?: UserMetadata[]
saved_tracks?: UserTrackMetadata[]
saved_albums?: UserCollectionMetadata[]
saved_playlists?: UserCollectionMetadata[]
followed_users?: UserMetadata[]
searchText?: string | null
isAutocomplete?: boolean
}
export const adaptSearchResponse = (searchResponse: APIResponse<APISearch>) => {
return {
tracks:
searchResponse.data.tracks
?.map(adapter.makeTrack)
.filter(removeNullable) ?? undefined,
saved_tracks:
searchResponse.data.saved_tracks
?.map(adapter.makeTrack)
.filter(removeNullable) ?? undefined,
users:
searchResponse.data.users?.map(adapter.makeUser).filter(removeNullable) ??
undefined,
followed_users:
searchResponse.data.followed_users
?.map(adapter.makeUser)
.filter(removeNullable) ?? undefined,
playlists:
searchResponse.data.playlists
?.map(adapter.makePlaylist)
.filter(removeNullable) ?? undefined,
saved_playlists:
searchResponse.data.saved_playlists
?.map(adapter.makePlaylist)
.filter(removeNullable) ?? undefined,
albums:
searchResponse.data.albums
?.map(adapter.makePlaylist)
.filter(removeNullable) ?? undefined,
saved_albums:
searchResponse.data.saved_albums
?.map(adapter.makePlaylist)
.filter(removeNullable) ?? undefined
}
}
export const processSearchResults = async ({
tracks = [],
albums = [],
playlists = [],
users = [],
saved_tracks: savedTracks = [],
saved_albums: savedAlbums = [],
saved_playlists: savedPlaylists = [],
followed_users: followedUsers = [],
isAutocomplete = false
}: ProcessSearchResultsArgs) => {
const maxSaved = isAutocomplete
? AUTOCOMPLETE_MAX_SAVED_RESULTS
: SEARCH_MAX_SAVED_RESULTS
const maxTotal = isAutocomplete
? AUTOCOMPLETE_TOTAL_RESULTS
: SEARCH_MAX_TOTAL_RESULTS
const combinedTracks = combineLists(
savedTracks,
tracks,
'track_id',
maxSaved,
maxTotal
)
const combinedAlbums = combineLists(
savedAlbums,
albums,
'playlist_id',
maxSaved,
maxTotal
)
const combinedPlaylists = combineLists(
savedPlaylists,
playlists,
'playlist_id',
maxSaved,
maxTotal
)
const combinedUsers = combineLists(
followedUsers,
users,
'user_id',
maxSaved,
maxTotal
)
return {
tracks: combinedTracks,
albums: combinedAlbums,
playlists: combinedPlaylists,
users: combinedUsers
}
}