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 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into gated-content-updates
* origin/main: (26 commits) [C-1608 C-2750] Fix edit profile/cover photo (#3735) [C-2711] Remove app-store link in force-upgrade for saga (#3737) Pin lerna version (#3738) Update announcement notification style and hover behavior retry (#3736) [CON-765] Default v2 signup and fix local EM (#3730) Update announcement notification style and hover behavior (#3733) Update announcement notification style and hover behavior (#3733) [C-2844] Ensure all tracks fetched on collection page (#3734) Add dev storage bootstrap nodes (#3731) [QA-565] Fix notification overflow bug (#3732) [C-2547] Lineup pagination fixes (#3728) [C-2679] Track Availability Modal Form (#3720) [C-2785] Update collection screen focus effect to fetch lineup (#3722) Make embed not fetch metadata from CN (#3710) [C-2809] Remove user from image hooks (#3723) [C-2834] Remove replica set usage in web (#3721) [C-2829] Finalize SuggestedTracks (#3706) [PAY-1569] Update blog post link (#3719) Call the DN selection callback in AudiusBackend if using a cached DN (#3718) DMs: Add space to learn more text (#3714) ...
- Loading branch information
Showing
127 changed files
with
3,464 additions
and
1,098 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,132 @@ | ||
import { useMemo } from 'react' | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
import { sampleSize } from 'lodash' | ||
import { useSelector } from 'react-redux' | ||
import { difference, shuffle } from 'lodash' | ||
import { useSelector, useDispatch } from 'react-redux' | ||
|
||
import { usePaginatedQuery } from 'audius-query' | ||
import { ID } from 'models/Identifiers' | ||
import { Status } from 'models/Status' | ||
import { TimeRange } from 'models/TimeRange' | ||
import { getUserId } from 'store/account/selectors' | ||
import { addTrackToPlaylist } from 'store/cache/collections/actions' | ||
import { getCollection } from 'store/cache/collections/selectors' | ||
import { getTrack } from 'store/cache/tracks/selectors' | ||
import { CommonState } from 'store/index' | ||
|
||
import { useGetFavoritedTrackList } from './favorites' | ||
import { useGetTracksByIds } from './track' | ||
import { useGetTrending } from './trending' | ||
|
||
export const useGetSuggestedTracks = () => { | ||
const suggestedTrackCount = 5 | ||
|
||
const selectSuggestedTracks = (state: CommonState, ids: ID[]) => { | ||
return ids.map((id) => { | ||
const track = getTrack(state, { id }) | ||
if (!track) return { id, isLoading: true as const } | ||
return { id, track, isLoading: false as const } | ||
}) | ||
} | ||
|
||
const selectCollectionTrackIds = (state: CommonState, collectionId: ID) => { | ||
const collection = getCollection(state, { id: collectionId }) | ||
if (!collection) return [] | ||
return collection?.playlist_contents.track_ids.map((trackId) => trackId.track) | ||
} | ||
|
||
export const useGetSuggestedTracks = (collectionId: ID) => { | ||
const currentUserId = useSelector(getUserId) | ||
const { data: favoritedTracks } = useGetFavoritedTrackList( | ||
{ currentUserId }, | ||
{ disabled: !currentUserId } | ||
const dispatch = useDispatch() | ||
const [suggestedTrackIds, setSuggestedTrackIds] = useState<ID[]>([]) | ||
|
||
const collectionTrackIds = useSelector((state: CommonState) => | ||
selectCollectionTrackIds(state, collectionId) | ||
) | ||
|
||
const favoritedTracksSample = useMemo(() => { | ||
return sampleSize(favoritedTracks, 5) | ||
const { data: favoritedTracks, status: favoritedStatus } = | ||
useGetFavoritedTrackList({ currentUserId }, { disabled: !currentUserId }) | ||
|
||
useEffect(() => { | ||
if (favoritedTracks) { | ||
const suggestedTrackIds = difference( | ||
shuffle(favoritedTracks).map((track) => track.save_item_id), | ||
collectionTrackIds | ||
) | ||
setSuggestedTrackIds(suggestedTrackIds) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [favoritedTracks]) | ||
|
||
return useGetTracksByIds( | ||
const { | ||
data: trendingTracks, | ||
status: trendingStatus, | ||
loadMore | ||
} = usePaginatedQuery( | ||
useGetTrending, | ||
{ | ||
timeRange: TimeRange.WEEK, | ||
currentUserId, | ||
genre: null | ||
}, | ||
{ | ||
pageSize: 10, | ||
disabled: favoritedStatus !== Status.SUCCESS | ||
} | ||
) | ||
|
||
useEffect(() => { | ||
if (trendingStatus === Status.SUCCESS) { | ||
const trendingTrackIds = difference( | ||
trendingTracks.map((track) => track.track_id), | ||
collectionTrackIds | ||
) | ||
setSuggestedTrackIds([...suggestedTrackIds, ...trendingTrackIds]) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [trendingStatus]) | ||
|
||
useEffect(() => { | ||
if (suggestedTrackIds.length < 5) { | ||
loadMore() | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [suggestedTrackIds.length]) | ||
|
||
const suggestedTracks = useSelector((state: CommonState) => | ||
selectSuggestedTracks( | ||
state, | ||
suggestedTrackIds.slice(0, suggestedTrackCount) | ||
) | ||
) | ||
|
||
useGetTracksByIds( | ||
{ | ||
currentUserId, | ||
ids: favoritedTracksSample?.map((favorite) => favorite.save_item_id) | ||
ids: suggestedTracks | ||
.filter((suggestedTrack) => suggestedTrack.isLoading) | ||
.map((suggestedTrack) => suggestedTrack.id) | ||
}, | ||
{ | ||
disabled: !currentUserId || favoritedTracksSample.length === 0 | ||
disabled: !currentUserId || suggestedTrackIds.length === 0 | ||
} | ||
) | ||
|
||
const handleAddTrack = useCallback( | ||
(trackId: ID, collectionId: ID) => { | ||
dispatch(addTrackToPlaylist(trackId, collectionId)) | ||
const trackIndexToRemove = suggestedTrackIds.indexOf(trackId) | ||
suggestedTrackIds.splice(trackIndexToRemove, 1) | ||
setSuggestedTrackIds(suggestedTrackIds) | ||
}, | ||
[dispatch, suggestedTrackIds] | ||
) | ||
|
||
const handleRefresh = useCallback(() => { | ||
setSuggestedTrackIds(suggestedTrackIds.slice(suggestedTrackCount)) | ||
}, [suggestedTrackIds]) | ||
|
||
return { | ||
suggestedTracks, | ||
onRefresh: handleRefresh, | ||
onAddTrack: handleAddTrack | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createApi } from 'audius-query' | ||
import { ID } from 'models/Identifiers' | ||
import { Kind } from 'models/Kind' | ||
import { TimeRange } from 'models/TimeRange' | ||
import { Genre } from 'utils/genres' | ||
import { Nullable } from 'utils/typeUtils' | ||
|
||
type GetTrendingArgs = { | ||
timeRange: TimeRange | ||
genre: Nullable<Genre> | ||
offset: number | ||
limit: number | ||
currentUserId: Nullable<ID> | ||
} | ||
|
||
const trendingApi = createApi({ | ||
reducerPath: 'trendingApi', | ||
endpoints: { | ||
getTrending: { | ||
fetch: async (args: GetTrendingArgs, { apiClient }) => { | ||
return await apiClient.getTrending(args) | ||
}, | ||
options: { | ||
kind: Kind.TRACKS, | ||
schemaKey: 'tracks' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
export const { useGetTrending } = trendingApi.hooks | ||
export const trendingApiReducer = trendingApi.reducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.