Skip to content

Commit 0fd51f5

Browse files
authored
[PE-6073] Migrate useTrackByTrackId to useTrack (#11950)
1 parent aae9c9b commit 0fd51f5

File tree

23 files changed

+211
-151
lines changed

23 files changed

+211
-151
lines changed

packages/common/src/api/track.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ const trackApi = createApi({
124124
})
125125

126126
export const {
127-
useGetTrackById,
128127
useGetTrackByPermalink,
129128
useGetTracksByIds,
130129
useGetUserTracksByHandle

packages/common/src/context/comments/commentsContext.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@ import { useQueryClient } from '@tanstack/react-query'
1414
import { useDispatch, useSelector } from 'react-redux'
1515

1616
import {
17-
useGetTrackById,
1817
useTrackComments,
1918
QUERY_KEYS,
2019
useTrackCommentCount,
2120
resetPreviousCommentCount,
22-
useSupporters
21+
useSupporters,
22+
useTrack
2323
} from '~/api'
2424
import { useGatedContentAccess } from '~/hooks'
25-
import {
26-
ModalSource,
27-
ID,
28-
Comment,
29-
ReplyComment,
30-
UserTrackMetadata,
31-
Name
32-
} from '~/models'
25+
import { ModalSource, ID, Comment, ReplyComment, Name, Track } from '~/models'
3326
import { LineupBaseActions, playerActions } from '~/store'
3427
import { getUserId } from '~/store/account/selectors'
3528
import { seek } from '~/store/player/slice'
@@ -74,7 +67,7 @@ type CommentSectionContextType<NavigationProp> = {
7467
artistId: ID
7568
isEntityOwner: boolean
7669
commentCount: number | undefined
77-
track: UserTrackMetadata
70+
track: Track
7871
playTrack: (timestampSeconds?: number) => void
7972
commentSectionLoading: boolean
8073
commentIds: ID[]
@@ -106,7 +99,7 @@ export function CommentSectionProvider<NavigationProp>(
10699
uid: lineupUid,
107100
lineupActions
108101
} = props
109-
const { data: track } = useGetTrackById({ id: entityId })
102+
const { data: track } = useTrack(entityId)
110103
const trackOwnerId = track?.owner_id
111104

112105
// Prefetch the track owner's supporters

packages/common/src/hooks/chats/useChatBlastAudienceContent.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
useGetCurrentUserId,
88
useGetPlaylistById,
99
useGetPurchasersCount,
10-
useGetTrackById,
11-
useRemixersCount
10+
useRemixersCount,
11+
useTrack
1212
} from '~/api'
1313
import {
1414
getChatBlastAudienceDescription,
@@ -30,12 +30,10 @@ export const useChatBlastAudienceContent = ({ chat }: { chat: ChatBlast }) => {
3030

3131
const { data: currentUserId } = useGetCurrentUserId({})
3232
const { data: user } = useGetCurrentUser({})
33-
const { data: track } = useGetTrackById(
34-
{
35-
id: decodedContentId!
36-
},
37-
{ disabled: !decodedContentId || audienceContentType !== 'track' }
38-
)
33+
const { data: trackTitle } = useTrack(decodedContentId, {
34+
enabled: !!decodedContentId && audienceContentType === 'track',
35+
select: (track) => track.title
36+
})
3937
const { data: album } = useGetPlaylistById(
4038
{
4139
playlistId: decodedContentId!
@@ -82,7 +80,7 @@ export const useChatBlastAudienceContent = ({ chat }: { chat: ChatBlast }) => {
8280

8381
const contentTitle = audienceContentId
8482
? audienceContentType === 'track'
85-
? track?.title
83+
? trackTitle
8684
: album?.playlist_name
8785
: undefined
8886

packages/common/src/hooks/useTrackMetadata.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Mood } from '@audius/sdk'
2+
import { pick } from 'lodash'
23

3-
import { useGetTrackById } from '~/api/track'
4+
import { useTrack } from '~/api/tan-query/useTrack'
45
import { ID } from '~/models'
56
import { parseMusicalKey } from '~/utils/musicalKeys'
67
import { searchPage } from '~/utils/route'
@@ -33,9 +34,22 @@ export type TrackMetadataInfo = {
3334
export const useTrackMetadata = ({
3435
trackId
3536
}: TrackMetadataProps): TrackMetadataInfo[] => {
36-
const { data: track } = useGetTrackById({ id: trackId })
37+
const { data: trackMetadata } = useTrack(trackId, {
38+
select: (track) =>
39+
pick(track, [
40+
'duration',
41+
'genre',
42+
'release_date',
43+
'mood',
44+
'is_unlisted',
45+
'musical_key',
46+
'bpm',
47+
'is_custom_bpm',
48+
'album_backlink'
49+
])
50+
})
3751

38-
if (!track) return []
52+
if (!trackMetadata) return []
3953

4054
const {
4155
duration,
@@ -47,7 +61,7 @@ export const useTrackMetadata = ({
4761
bpm,
4862
is_custom_bpm: isCustomBpm,
4963
album_backlink
50-
} = track
64+
} = trackMetadata
5165

5266
const parsedBpm = bpm
5367
? parseFloat((bpm ?? 0).toFixed(isCustomBpm ? 2 : 0)).toString()
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TrackMetadata } from '~/models'
22

3-
import { Maybe, Nullable } from './typeUtils'
3+
import { Maybe } from './typeUtils'
44

5-
export const isLongFormContent = (track: Maybe<Nullable<TrackMetadata>>) =>
6-
track?.genre === 'podcast' || track?.genre === 'audiobook'
5+
export const isLongFormContent = (
6+
track: Maybe<Pick<TrackMetadata, 'genre'> | null>
7+
) => track?.genre === 'podcast' || track?.genre === 'audiobook'

packages/mobile/src/components/composer-input/ComposerInput.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useState
99
} from 'react'
1010

11-
import { useGetTrackById } from '@audius/common/api'
11+
import { useTrack } from '@audius/common/api'
1212
import { useAudiusLinkResolver } from '@audius/common/hooks'
1313
import type { ID, UserMetadata } from '@audius/common/models'
1414
import {
@@ -18,7 +18,7 @@ import {
1818
timestampRegex
1919
} from '@audius/common/utils'
2020
import { OptionalHashId } from '@audius/sdk'
21-
import { isEqual } from 'lodash'
21+
import { isEqual, pick } from 'lodash'
2222
import type { TextInput as RnTextInput } from 'react-native'
2323
import { Platform, TouchableOpacity } from 'react-native'
2424
import type {
@@ -133,7 +133,10 @@ export const ComposerInput = forwardRef(function ComposerInput(
133133
const latestValueRef = useRef(value)
134134
const messageIdRef = useRef(messageId)
135135
const lastKeyPressMsRef = useRef<number | null>(null)
136-
const { data: track } = useGetTrackById({ id: entityId ?? -1 })
136+
const { data: partialTrack } = useTrack(entityId, {
137+
select: (track) =>
138+
pick(track, ['duration', 'genre', 'release_date', 'access'])
139+
})
137140

138141
useEffect(() => {
139142
setUserMentions(
@@ -183,9 +186,9 @@ export const ComposerInput = forwardRef(function ComposerInput(
183186
const prevLinkEntities = usePrevious(linkEntities)
184187

185188
const timestamps = useMemo(() => {
186-
if (!track || !track.access.stream) return []
189+
if (!partialTrack || !partialTrack.access.stream) return []
187190

188-
const { duration } = track
191+
const { duration } = partialTrack
189192
return Array.from(value.matchAll(timestampRegex))
190193
.filter((match) => getDurationFromTimestampMatch(match) <= duration)
191194
.map((match) => ({
@@ -194,7 +197,7 @@ export const ComposerInput = forwardRef(function ComposerInput(
194197
index: match.index,
195198
link: ''
196199
}))
197-
}, [track, value])
200+
}, [partialTrack, value])
198201
const prevTimestamps = usePrevious(timestamps)
199202

200203
useEffect(() => {

packages/mobile/src/components/premium-content-purchase-drawer/PremiumContentPurchaseDrawer.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useCallback, type ReactNode, useEffect } from 'react'
22

33
import {
4-
useGetCurrentUserId,
4+
useCurrentUserId,
55
useGetPlaylistById,
6-
useGetTrackById,
7-
useGetUserById
6+
useGetUserById,
7+
useTrack
88
} from '@audius/common/api'
99
import type { PurchaseableContentMetadata } from '@audius/common/hooks'
1010
import {
@@ -22,12 +22,7 @@ import {
2222
PURCHASE_METHOD_MINT_ADDRESS
2323
} from '@audius/common/hooks'
2424
import type { ID, USDCPurchaseConditions } from '@audius/common/models'
25-
import {
26-
Name,
27-
PurchaseMethod,
28-
PurchaseVendor,
29-
statusIsNotFinalized
30-
} from '@audius/common/models'
25+
import { Name, PurchaseMethod, PurchaseVendor } from '@audius/common/models'
3126
import { IntKeys, FeatureFlags } from '@audius/common/services'
3227
import {
3328
usePremiumContentPurchaseModal,
@@ -456,11 +451,8 @@ export const PremiumContentPurchaseDrawer = () => {
456451
onClosed
457452
} = usePremiumContentPurchaseModal()
458453
const isAlbum = contentType === PurchaseableContentType.ALBUM
459-
const { data: currentUserId } = useGetCurrentUserId({})
460-
const { data: track, status: trackStatus } = useGetTrackById(
461-
{ id: contentId },
462-
{ disabled: !contentId }
463-
)
454+
const { data: currentUserId } = useCurrentUserId()
455+
const { data: track, isPending: isTrackPending } = useTrack(contentId)
464456
const { data: album } = useGetPlaylistById(
465457
{ playlistId: contentId!, currentUserId },
466458
{ disabled: !isAlbum || !contentId }
@@ -481,8 +473,6 @@ export const PremiumContentPurchaseDrawer = () => {
481473
const error = useSelector(getPurchaseContentError)
482474
const isUnlocking = !error && isContentPurchaseInProgress(stage)
483475

484-
const isLoading = statusIsNotFinalized(trackStatus)
485-
486476
const isValidStreamGatedTrack = !!metadata && isStreamPurchaseable(metadata)
487477
const isValidDownloadGatedTrack =
488478
!!metadata && isTrackDownloadPurchaseable(metadata)
@@ -528,7 +518,7 @@ export const PremiumContentPurchaseDrawer = () => {
528518
isFullscreen
529519
dismissKeyboardOnOpen
530520
>
531-
{isLoading ? (
521+
{isTrackPending ? (
532522
<View style={styles.spinnerContainer}>
533523
<LoadingSpinner />
534524
</View>

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ReactNode } from 'react'
22

3-
import { useGetPlaylistById, useGetTrackById } from '@audius/common/api'
3+
import { useGetPlaylistById, useTrack, useUser } from '@audius/common/api'
44
import type { ID, UserMetadata } from '@audius/common/models'
55
import { SquareSizes } from '@audius/common/models'
6+
import { pick } from 'lodash'
67

78
import { Flex, Text } from '@audius/harmony-native'
89
import { CollectionImage } from 'app/components/image/CollectionImage'
@@ -11,8 +12,8 @@ import UserBadges from 'app/components/user-badges'
1112

1213
type ComposePreviewInfoProps = {
1314
title: string
14-
name: string
15-
user: UserMetadata
15+
name?: string
16+
user?: UserMetadata
1617
image?: ReactNode
1718
}
1819

@@ -43,7 +44,7 @@ const ComposePreviewInfo = (props: ComposePreviewInfoProps) => {
4344
<Text variant='body' strength='strong'>
4445
{name}
4546
</Text>
46-
<UserBadges hideName user={user} badgeSize={14} />
47+
{user ? <UserBadges hideName user={user} badgeSize={14} /> : null}
4748
</Flex>
4849
</Flex>
4950
</Flex>
@@ -57,15 +58,18 @@ type ComposerTrackInfoProps = {
5758
export const ComposerTrackInfo = (props: ComposerTrackInfoProps) => {
5859
const { trackId } = props
5960

60-
const { data: track } = useGetTrackById({ id: trackId }, { force: true })
61+
const { data: partialTrack } = useTrack(trackId, {
62+
select: (track) => pick(track, ['title', 'owner_id'])
63+
})
64+
const { data: user } = useUser(partialTrack?.owner_id)
6165

62-
if (!track) return null
66+
if (!partialTrack) return null
6367

6468
return (
6569
<ComposePreviewInfo
66-
title={track.title}
67-
name={track.user.name}
68-
user={track.user}
70+
title={partialTrack.title}
71+
name={user?.name ?? ''}
72+
user={user}
6973
image={
7074
<TrackImage trackId={trackId} size={SquareSizes.SIZE_150_BY_150} />
7175
}

packages/mobile/src/screens/edit-track-screen/components/RemixTrackPill.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useGetTrackById } from '@audius/common/api'
1+
import { useTrack } from '@audius/common/api'
22
import type { ID } from '@audius/common/models'
33
import { SquareSizes } from '@audius/common/models'
4+
import { pick } from 'lodash'
45
import type { StyleProp, ViewStyle } from 'react-native'
56

67
import { Flex, Paper, Text } from '@audius/harmony-native'
@@ -44,12 +45,14 @@ const useStyles = makeStyles(({ spacing, palette, typography }) => ({
4445

4546
export const RemixTrackPill = (props: RemixTrackPillProps) => {
4647
const { trackId, style } = props
47-
const { data: track } = useGetTrackById({ id: trackId })
48+
const { data: track } = useTrack(trackId, {
49+
select: (track) => pick(track, ['title', 'owner_id'])
50+
})
4851
const styles = useStyles()
4952

5053
if (!track) return null
5154

52-
const { user, title } = track
55+
const { title, owner_id } = track
5356

5457
return (
5558
<Paper
@@ -76,7 +79,7 @@ export const RemixTrackPill = (props: RemixTrackPillProps) => {
7679
</Flex>
7780
<Flex row alignItems='center'>
7881
<Text color='subdued'>{messages.trackBy}</Text>
79-
<UserLink userId={user.user_id} size='s' disabled />
82+
<UserLink userId={owner_id} size='s' disabled />
8083
</Flex>
8184
</Paper>
8285
)

packages/mobile/src/screens/search-screen/SearchItem.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {
22
useGetPlaylistById,
3-
useGetTrackById,
4-
useGetUserById
3+
useGetUserById,
4+
useTrack
55
} from '@audius/common/api'
66
import { recentSearchMessages as messages } from '@audius/common/messages'
77
import { Kind, SquareSizes, Status } from '@audius/common/models'
88
import type { SearchItem as SearchItemType } from '@audius/common/store'
9+
import { pick } from 'lodash'
910
import { TouchableOpacity } from 'react-native-gesture-handler'
1011

1112
import type { IconComponent } from '@audius/harmony-native'
@@ -81,15 +82,17 @@ export const SearchItemSkeleton = () => (
8182
export const SearchItemTrack = (props: SearchItemProps) => {
8283
const { searchItem, onPress } = props
8384
const { id } = searchItem
84-
const { data: track, status } = useGetTrackById({ id })
85-
const { data: user } = useGetUserById({ id: track?.owner_id ?? 0 })
85+
const { data: partialTrack, isPending: isTrackPending } = useTrack(id, {
86+
select: (track) => pick(track, ['title', 'owner_id'])
87+
})
88+
const { data: user } = useGetUserById({ id: partialTrack?.owner_id ?? 0 })
8689
const { spacing } = useTheme()
8790
const navigation = useNavigation()
8891

89-
if (status === Status.LOADING) return <SearchItemSkeleton />
92+
if (isTrackPending) return <SearchItemSkeleton />
9093

91-
if (!track) return null
92-
const { title } = track
94+
if (!partialTrack) return null
95+
const { title } = partialTrack
9396

9497
if (!user) return null
9598

0 commit comments

Comments
 (0)