Skip to content

Commit b1a7d16

Browse files
committed
Merge branch 'main' of github.com:AudiusProject/audius-protocol into jd/dep-aquery-track-tracksbyids
# Conflicts: # packages/common/src/api/track.ts
2 parents cca6e56 + a7866ce commit b1a7d16

File tree

13 files changed

+150
-167
lines changed

13 files changed

+150
-167
lines changed

packages/common/src/api/track.ts

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,6 @@ import { SDKRequest } from './types'
1010
const trackApi = createApi({
1111
reducerPath: 'trackApi',
1212
endpoints: {
13-
getTrackById: {
14-
fetch: async (
15-
{
16-
id,
17-
currentUserId
18-
}: { id: ID | null | undefined; currentUserId?: Nullable<ID> },
19-
{ audiusSdk }
20-
) => {
21-
if (!id || id === -1) return null
22-
const sdk = await audiusSdk()
23-
const { data } = await sdk.full.tracks.getTrack({
24-
trackId: Id.parse(id),
25-
userId: OptionalId.parse(currentUserId)
26-
})
27-
return data ? userTrackMetadataFromSDK(data) : null
28-
},
29-
fetchBatch: async (
30-
{ ids, currentUserId }: { ids: ID[]; currentUserId?: Nullable<ID> },
31-
{ audiusSdk }
32-
) => {
33-
const id = ids.filter((id) => id && id !== -1).map((id) => Id.parse(id))
34-
if (id.length === 0) return []
35-
36-
const sdk = await audiusSdk()
37-
const { data = [] } = await sdk.full.tracks.getBulkTracks({
38-
id,
39-
userId: OptionalId.parse(currentUserId)
40-
})
41-
return transformAndCleanList(data, userTrackMetadataFromSDK)
42-
},
43-
options: {
44-
idArgKey: 'id',
45-
kind: Kind.TRACKS,
46-
schemaKey: 'track'
47-
}
48-
},
49-
getTrackByPermalink: {
50-
fetch: async (
51-
{
52-
permalink,
53-
currentUserId
54-
}: { permalink: Nullable<string>; currentUserId: Nullable<ID> },
55-
{ audiusSdk }
56-
) => {
57-
if (!permalink) {
58-
console.error('Attempting to get track but permalink is null...')
59-
return
60-
}
61-
const sdk = await audiusSdk()
62-
const { data } = await sdk.full.tracks.getBulkTracks({
63-
permalink: [permalink],
64-
userId: OptionalId.parse(currentUserId)
65-
})
66-
return data && data.length > 0
67-
? userTrackMetadataFromSDK(data[0])
68-
: null
69-
},
70-
options: {
71-
permalinkArgKey: 'permalink',
72-
kind: Kind.TRACKS,
73-
schemaKey: 'track'
74-
}
75-
},
7613
// TODO: Remove after purchases.ts is deprecated
7714
getTracksByIds: {
7815
fetch: async (
@@ -124,8 +61,7 @@ const trackApi = createApi({
12461
}
12562
})
12663

127-
export const { useGetTrackByPermalink, useGetUserTracksByHandle } =
128-
trackApi.hooks
64+
export const { useGetTracksByIds, useGetUserTracksByHandle } = trackApi.hooks
12965
export const trackApiFetch = trackApi.fetch
13066
export const trackApiReducer = trackApi.reducer
13167
export const trackApiActions = trackApi.actions

packages/common/src/hooks/useGatedContent.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,23 @@ export const useGatedCollectionAccess = (collectionId: ID) => {
6666

6767
// Returns whether user has access to given track.
6868
export const useGatedContentAccess = (
69-
content: Nullable<Partial<Track | Collection>> | undefined
69+
content:
70+
| Nullable<
71+
| Pick<
72+
Track,
73+
| 'track_id'
74+
| 'is_stream_gated'
75+
| 'is_download_gated'
76+
| 'access'
77+
| 'stream_conditions'
78+
| 'download_conditions'
79+
>
80+
| Pick<
81+
Collection,
82+
'playlist_id' | 'is_stream_gated' | 'access' | 'stream_conditions'
83+
>
84+
>
85+
| undefined
7086
) => {
7187
const nftAccessSignatureMap = useSelector(getNftAccessSignatureMap)
7288
const hasAccount = useSelector(getHasAccount)

packages/common/src/hooks/useTrackMetadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Mood } from '@audius/sdk'
22
import { pick } from 'lodash'
33

4-
import { useTrack } from '~/api/tan-query/useTrack'
4+
import { useTrack } from '~/api'
55
import { ID } from '~/models'
66
import { parseMusicalKey } from '~/utils/musicalKeys'
77
import { searchPage } from '~/utils/route'

packages/mobile/src/screens/chat-screen/ChatMessageTrack.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
import { useCallback, useEffect, useMemo } from 'react'
22

3-
import { useGetTrackByPermalink } from '@audius/common/api'
3+
import { useTrackByPermalink, useUser } from '@audius/common/api'
44
import { useGatedContentAccess, useToggleTrack } from '@audius/common/hooks'
55
import type { TrackPlayback } from '@audius/common/hooks'
66
import { Name, PlaybackSource, Kind } from '@audius/common/models'
77
import type { ID } from '@audius/common/models'
8-
import { accountSelectors, QueueSource } from '@audius/common/store'
8+
import { QueueSource } from '@audius/common/store'
99
import type { ChatMessageTileProps } from '@audius/common/store'
1010
import { getPathFromTrackUrl, makeUid } from '@audius/common/utils'
11-
import { useSelector } from 'react-redux'
11+
import { pick } from 'lodash'
1212

1313
import { TrackTile } from 'app/components/lineup-tile'
1414
import { LineupTileSource } from 'app/components/lineup-tile/types'
1515
import { make, track as trackEvent } from 'app/services/analytics'
1616

17-
const { getUserId } = accountSelectors
18-
1917
export const ChatMessageTrack = ({
2018
link,
2119
onEmpty,
2220
onSuccess,
2321
styles
2422
}: ChatMessageTileProps) => {
25-
const currentUserId = useSelector(getUserId)
26-
2723
const permalink = getPathFromTrackUrl(link)
28-
const { data: track } = useGetTrackByPermalink(
29-
{
30-
permalink,
31-
currentUserId
32-
},
33-
{ disabled: !permalink }
34-
)
35-
const { hasStreamAccess } = useGatedContentAccess(track ?? null)
36-
const isPreview =
37-
!!track?.is_stream_gated && !!track?.preview_cid && !hasStreamAccess
38-
39-
const user = useMemo(() => (track ? { ...track.user } : null), [track])
24+
const { data: partialTrack } = useTrackByPermalink(permalink, {
25+
select: (track) =>
26+
pick(track, [
27+
'track_id',
28+
'owner_id',
29+
'preview_cid',
30+
'access',
31+
'is_download_gated',
32+
'is_stream_gated',
33+
'download_conditions',
34+
'stream_conditions'
35+
])
36+
})
37+
const trackExists = !!partialTrack
38+
const { hasStreamAccess } = useGatedContentAccess(partialTrack)
39+
const { track_id, is_stream_gated, preview_cid, owner_id } =
40+
partialTrack ?? {}
41+
const { data: user } = useUser(owner_id)
42+
const isPreview = !!is_stream_gated && !!preview_cid && !hasStreamAccess
4043

41-
const trackId = track?.track_id
4244
const uid = useMemo(() => {
43-
return trackId ? makeUid(Kind.TRACKS, trackId) : null
44-
}, [trackId])
45+
return track_id ? makeUid(Kind.TRACKS, track_id) : null
46+
}, [track_id])
4547

4648
const recordAnalytics = useCallback(
4749
({ name, id }: { name: TrackPlayback; id: ID }) => {
48-
if (!track) return
50+
if (!trackExists) return
4951
trackEvent(
5052
make({
5153
eventName: name,
@@ -54,19 +56,19 @@ export const ChatMessageTrack = ({
5456
})
5557
)
5658
},
57-
[track]
59+
[trackExists]
5860
)
5961

6062
const { togglePlay } = useToggleTrack({
61-
id: track?.track_id,
63+
id: track_id,
6264
uid,
6365
isPreview,
6466
source: QueueSource.CHAT_TRACKS,
6567
recordAnalytics
6668
})
6769

6870
useEffect(() => {
69-
if (track && user && uid) {
71+
if (trackExists && user && uid) {
7072
trackEvent(
7173
make({
7274
eventName: Name.MESSAGE_UNFURL_TRACK
@@ -76,11 +78,11 @@ export const ChatMessageTrack = ({
7678
} else {
7779
onEmpty?.()
7880
}
79-
}, [track, user, uid, onSuccess, onEmpty])
81+
}, [trackExists, user, uid, onSuccess, onEmpty])
8082

81-
return track && user && uid ? (
83+
return track_id && user && uid ? (
8284
<TrackTile
83-
id={track.track_id}
85+
id={track_id}
8486
index={0}
8587
togglePlay={togglePlay}
8688
uid={uid}

packages/mobile/src/screens/track-screen/RemixContestSection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ export const RemixContestSection = ({
7878
const { data: track } = useTrack(trackId)
7979
const { data: currentUserId } = useCurrentUserId()
8080
const isOwner = track?.owner_id === currentUserId
81+
const hasPrizeInfo = !!remixContest?.eventData?.prizeInfo
8182

8283
const [index, setIndex] = useState(0)
8384
const [routes] = useState<Route[]>([
8485
{ key: 'details', title: 'Details' },
85-
{ key: 'prizes', title: 'Prizes' },
86+
...(hasPrizeInfo ? [{ key: 'prizes', title: 'Prizes' }] : []),
8687
{ key: 'submissions', title: 'Submissions' }
8788
])
8889
const [heights, setHeights] = useState({})

packages/web/src/components/edit/fields/RemixSettingsField/RemixSettingsMenuFields.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { useEffect } from 'react'
22

3-
import { useGetTrackByPermalink } from '@audius/common/api'
3+
import { useTrackByPermalink } from '@audius/common/api'
44
import { useGatedContentAccess } from '@audius/common/hooks'
5-
import { accountSelectors } from '@audius/common/store'
65
import { getPathFromTrackUrl } from '@audius/common/utils'
76
import { useField } from 'formik'
8-
import { useSelector } from 'react-redux'
7+
import { pick } from 'lodash'
98
import { useThrottle } from 'react-use'
109

1110
import { Divider } from 'components/divider'
@@ -16,7 +15,6 @@ import { SwitchRowField } from '../SwitchRowField'
1615
import styles from './RemixSettingsField.module.css'
1716
import { TrackInfo } from './TrackInfo'
1817
import { CAN_REMIX_PARENT, IS_REMIX, REMIX_LINK, SHOW_REMIXES } from './types'
19-
const { getUserId } = accountSelectors
2018

2119
const messages = {
2220
hideRemix: {
@@ -36,24 +34,29 @@ export const RemixSettingsMenuFields = () => {
3634
const [{ value: trackUrl }] = useField(REMIX_LINK)
3735
const [, , { setValue: setCanRemixParent }] = useField(CAN_REMIX_PARENT)
3836
const permalink = useThrottle(getPathFromTrackUrl(trackUrl), 1000)
39-
const currentUserId = useSelector(getUserId)
4037

41-
const { data: track } = useGetTrackByPermalink(
42-
{ permalink, currentUserId },
43-
{ disabled: !permalink }
44-
)
38+
const { data: partialTrack } = useTrackByPermalink(permalink, {
39+
select: (track) =>
40+
pick(track, [
41+
'track_id',
42+
'is_stream_gated',
43+
'stream_conditions',
44+
'is_download_gated',
45+
'download_conditions',
46+
'access'
47+
])
48+
})
4549

46-
const trackId = track?.track_id
47-
const { hasStreamAccess: canRemixParent } = useGatedContentAccess(
48-
track ?? null
49-
)
50+
const { track_id } = partialTrack ?? {}
51+
const { hasStreamAccess: canRemixParent } =
52+
useGatedContentAccess(partialTrack)
5053

5154
const [, , { setValue: setParentTrackId }] = useField('parentTrackId')
5255

5356
useEffect(() => {
54-
setParentTrackId(trackId)
57+
setParentTrackId(track_id)
5558
setCanRemixParent(canRemixParent)
56-
}, [trackId, setParentTrackId, canRemixParent, setCanRemixParent])
59+
}, [track_id, setParentTrackId, canRemixParent, setCanRemixParent])
5760

5861
return (
5962
<div className={styles.fields}>
@@ -63,7 +66,7 @@ export const RemixSettingsMenuFields = () => {
6366
description={messages.remixOf.description}
6467
>
6568
<TextField name={REMIX_LINK} label={messages.remixOf.linkLabel} />
66-
{track ? <TrackInfo trackId={track.track_id} /> : null}
69+
{track_id ? <TrackInfo trackId={track_id} /> : null}
6770
</SwitchRowField>
6871

6972
<Divider />

packages/web/src/components/now-playing/components/ActionsBar.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ const ActionsBar = ({
4040
const { data: partialTrack } = useTrack(trackId, {
4141
select: (track) =>
4242
pick(track, [
43+
'track_id',
4344
'is_unlisted',
4445
'owner_id',
4546
'has_current_user_reposted',
46-
'has_current_user_saved'
47+
'has_current_user_saved',
48+
'access',
49+
'is_stream_gated',
50+
'stream_conditions',
51+
'is_download_gated',
52+
'download_conditions'
4753
])
4854
})
4955
const { data: currentUserId } = useCurrentUserId()
@@ -54,7 +60,7 @@ const ActionsBar = ({
5460
is_unlisted: isUnlisted
5561
} = partialTrack ?? {}
5662
const isOwner = ownerId === currentUserId
57-
const { hasStreamAccess } = useGatedContentAccess(partialTrack ?? {})
63+
const { hasStreamAccess } = useGatedContentAccess(partialTrack)
5864
const shouldShowActions = hasStreamAccess && !isUnlisted
5965

6066
if (!shouldShowActions) return null

packages/web/src/components/track/helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export const getTrackWithFallback = (track: Track | null | undefined) => {
4545
ddex_app: null,
4646
comment_count: 0,
4747
comments_disabled: false,
48-
album_backlink: undefined
48+
album_backlink: undefined,
49+
access: {
50+
stream: false,
51+
download: false
52+
}
4953
}
5054
)
5155
}

0 commit comments

Comments
 (0)