diff --git a/packages/common/src/api/reducer.ts b/packages/common/src/api/reducer.ts index 7071f34371c..b2f6c906533 100644 --- a/packages/common/src/api/reducer.ts +++ b/packages/common/src/api/reducer.ts @@ -5,7 +5,6 @@ import { authorizedAppsApiReducer } from './authorizedApps' import { developerAppsApiReducer } from './developerApps' import { favoritesApiReducer } from './favorites' import { libraryApiReducer } from './library' -import { searchApiReducer } from './search' import { signUpReducer } from './signUp' import { trendingApiReducer } from './trending' import { userApiReducer } from './user' @@ -16,7 +15,6 @@ export default combineReducers({ developerAppsApi: developerAppsApiReducer, favoritesApi: favoritesApiReducer, libraryApi: libraryApiReducer, - searchApi: searchApiReducer, signUpApi: signUpReducer, trendingApi: trendingApiReducer, userApi: userApiReducer diff --git a/packages/common/src/api/search.ts b/packages/common/src/api/search.ts deleted file mode 100644 index a8a62bae3fa..00000000000 --- a/packages/common/src/api/search.ts +++ /dev/null @@ -1,157 +0,0 @@ -import { Mood, OptionalId } from '@audius/sdk' -import { isEmpty } from 'lodash' - -import { searchResultsFromSDK } from '~/adapters' -import { createApi } from '~/audius-query' -import { Name, SearchSource, UserTrackMetadata } from '~/models' -import { ID } from '~/models/Identifiers' -import { FeatureFlags } from '~/services' -import { SearchKind, SearchSortMethod } from '~/store' -import { Genre, formatMusicalKey } from '~/utils' - -export type SearchCategory = 'all' | 'tracks' | 'albums' | 'playlists' | 'users' - -export type SearchFilters = { - genre?: Genre - mood?: Mood - bpm?: string - key?: string - isVerified?: boolean - hasDownloads?: boolean - isPremium?: boolean -} - -export type SearchFilter = keyof SearchFilters - -type GetSearchArgs = { - currentUserId: ID | null - query: string - category?: SearchCategory - limit?: number - offset?: number - source?: SearchSource - sortMethod?: SearchSortMethod - disableAnalytics?: boolean -} & SearchFilters - -const getMinMaxFromBpm = (bpm?: string) => { - const bpmParts = bpm ? bpm.split('-') : [undefined, undefined] - const bpmMin = bpmParts[0] ? parseFloat(bpmParts[0]) : undefined - const bpmMax = bpmParts[1] ? parseFloat(bpmParts[1]) : bpmMin - - // Because we round the bpm display to the nearest whole number, we need to add a small buffer - const bufferedBpmMin = bpmMin ? Math.round(bpmMin) - 0.5 : undefined - const bufferedBpmMax = bpmMax ? Math.round(bpmMax) + 0.5 : undefined - - return [bufferedBpmMin, bufferedBpmMax] -} - -const searchApi = createApi({ - reducerPath: 'searchApi', - endpoints: { - getSearchResults: { - fetch: async ( - args: GetSearchArgs, - { audiusSdk, getFeatureEnabled, analytics } - ) => { - const { - category, - currentUserId, - query, - limit, - offset, - source = 'search results page', - disableAnalytics, - ...filters - } = args - - const isUSDCEnabled = await getFeatureEnabled( - FeatureFlags.USDC_PURCHASES - ) - - const kind = category as SearchKind - if (!query && isEmpty(filters)) { - return { - tracks: [], - users: [], - albums: [], - playlists: [] - } - } - const sdk = await audiusSdk() - - const [bpmMin, bpmMax] = getMinMaxFromBpm(filters.bpm) - - const key = formatMusicalKey(filters.key) - - const isTagsSearch = query?.[0] === '#' - - const searchParams = { - kind, - userId: OptionalId.parse(currentUserId), - query: isTagsSearch ? query.slice(1) : query, - limit: limit || 50, - offset: offset || 0, - includePurchaseable: isUSDCEnabled, - bpmMin, - bpmMax, - key: key ? [key] : undefined, - genre: filters.genre ? [filters.genre] : undefined, - mood: filters.mood ? [filters.mood] : undefined, - sortMethod: filters.sortMethod, - isVerified: filters.isVerified, - hasDownloads: filters.hasDownloads, - isPurchaseable: filters.isPremium - } - - // Fire analytics only for the first page of results - if (offset === 0 && !disableAnalytics) { - analytics.track( - analytics.make( - isTagsSearch - ? { - eventName: Name.SEARCH_TAG_SEARCH, - tag: query, - source, - ...searchParams - } - : { - eventName: Name.SEARCH_SEARCH, - term: query, - source, - ...searchParams - } - ) - ) - } - - const { data } = isTagsSearch - ? await sdk.full.search.searchTags(searchParams) - : await sdk.full.search.search(searchParams) - const { tracks, playlists, albums, users } = searchResultsFromSDK(data) - const results = { tracks, playlists, albums, users } - - const formattedResults = { - ...results, - tracks: results.tracks.map((track) => { - return { - ...track, - user: { - ...((track as UserTrackMetadata).user ?? {}), - user_id: track.owner_id - } - } - }) - } - - return formattedResults - }, - options: {} - } - } -}) - -export const { useGetSearchResults } = searchApi.hooks -export const searchApiFetch = searchApi.fetch -export const searchApiFetchSaga = searchApi.fetchSaga -export const searchApiReducer = searchApi.reducer diff --git a/packages/common/src/models/Search.ts b/packages/common/src/models/Search.ts index 851e52c7c19..30654533b2d 100644 --- a/packages/common/src/models/Search.ts +++ b/packages/common/src/models/Search.ts @@ -1,3 +1,6 @@ +import { Mood } from '@audius/sdk' + +import { Genre } from '~/utils' import { Nullable } from '~/utils/typeUtils' import { CollectionImage } from './Collection' @@ -124,3 +127,15 @@ export type SearchPlaylist = CollectionImage & { stream_conditions: null ddex_app: null } + +export type SearchCategory = 'all' | 'tracks' | 'albums' | 'playlists' | 'users' + +export type SearchFilters = { + genre?: Genre + mood?: Mood + bpm?: string + key?: string + isVerified?: boolean + hasDownloads?: boolean + isPremium?: boolean +} diff --git a/packages/common/src/utils/route.ts b/packages/common/src/utils/route.ts index 56fed193981..967060f1623 100644 --- a/packages/common/src/utils/route.ts +++ b/packages/common/src/utils/route.ts @@ -1,8 +1,7 @@ import qs from 'query-string' import { matchPath, generatePath } from 'react-router' -import { SearchCategory, SearchFilters } from '~/api/search' -import { ID } from '~/models' +import { ID, SearchCategory, SearchFilters } from '~/models' import { encodeUrlName } from './formatUtil' import { convertGenreLabelToValue, Genre } from './genres'