Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 0 additions & 117 deletions packages/common/src/api/collection.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// TODO: migrate all of these to tan-query
export * from './account'
export * from './authorizedApps'
export * from './collection'
export * from './developerApps'
export * from './library'
export * from './purchases'
Expand All @@ -22,6 +21,7 @@ export * from './tan-query/comments'

// Collection
export * from './tan-query/collection/useCollection'
export * from './tan-query/collection/useCollections'
export * from './tan-query/collection/useCollectionByPermalink'
export * from './tan-query/collection/useCollectionFavorites'
export * from './tan-query/collection/useCollectionReposts'
Expand Down
2 changes: 0 additions & 2 deletions packages/common/src/api/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { combineReducers } from 'redux'

import { accountApiReducer } from './account'
import { authorizedAppsApiReducer } from './authorizedApps'
import { collectionApiReducer } from './collection'
import { developerAppsApiReducer } from './developerApps'
import { favoritesApiReducer } from './favorites'
import { libraryApiReducer } from './library'
Expand All @@ -17,7 +16,6 @@ import { userApiReducer } from './user'
export default combineReducers({
accountApi: accountApiReducer,
authorizedAppsApi: authorizedAppsApiReducer,
collectionApi: collectionApiReducer,
developerAppsApi: developerAppsApiReducer,
favoritesApi: favoritesApiReducer,
libraryApi: libraryApiReducer,
Expand Down
10 changes: 2 additions & 8 deletions packages/common/src/api/tan-query/collection/useCollections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ import { CommonState } from '~/store'

import { getCollectionsBatcher } from '../batchers/getCollectionsBatcher'
import { TQCollection } from '../models'
import { QUERY_KEYS } from '../queryKeys'
import { QueryKey, QueryOptions } from '../types'
import { QueryOptions } from '../types'
import { useCurrentUserId } from '../users/account/useCurrentUserId'
import { combineQueryResults } from '../utils/combineQueryResults'
import { useQueries } from '../utils/useQueries'

export const getCollectionQueryKey = (collectionId: ID | null | undefined) => {
return [
QUERY_KEYS.collection,
collectionId
] as unknown as QueryKey<TQCollection>
}
import { getCollectionQueryKey } from './useCollection'

export const useCollections = (
collectionIds: ID[] | null | undefined,
Expand Down
14 changes: 6 additions & 8 deletions packages/common/src/hooks/chats/useChatBlastAudienceContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useMemo } from 'react'
import { ChatBlast, ChatBlastAudience, OptionalHashId } from '@audius/sdk'

import {
useCollection,
useGetCurrentUser,
useGetCurrentUserId,
useGetPlaylistById,
useGetPurchasersCount,
useRemixersCount,
useTrack
Expand Down Expand Up @@ -34,12 +34,10 @@ export const useChatBlastAudienceContent = ({ chat }: { chat: ChatBlast }) => {
enabled: !!decodedContentId && audienceContentType === 'track',
select: (track) => track.title
})
const { data: album } = useGetPlaylistById(
{
playlistId: decodedContentId!
},
{ disabled: !decodedContentId || audienceContentType !== 'album' }
)
const { data: albumTitle } = useCollection(decodedContentId!, {
enabled: audienceContentType === 'album',
select: (collection) => collection.playlist_name
})

const { data: purchasersCount } = useGetPurchasersCount(
{
Expand Down Expand Up @@ -81,7 +79,7 @@ export const useChatBlastAudienceContent = ({ chat }: { chat: ChatBlast }) => {
const contentTitle = audienceContentId
? audienceContentType === 'track'
? trackTitle
: album?.playlist_name
: albumTitle
: undefined

const chatBlastTitle = getChatBlastTitle(audience)
Expand Down
9 changes: 4 additions & 5 deletions packages/common/src/hooks/chats/usePurchasersAudience.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useMemo } from 'react'
import { keyBy } from 'lodash'

import {
useCollections,
useGetCurrentUserId,
useGetPlaylistsByIds,
useGetPurchasersCount,
useGetSalesAggegrate,
useTracks
Expand Down Expand Up @@ -35,10 +35,9 @@ export const usePurchasersAudience = ({
const { data: tracks } = useTracks(
trackAggregates?.map((sale) => sale.contentId) ?? []
)
const { data: albums } = useGetPlaylistsByIds({
ids: albumAggregates?.map((sale) => sale.contentId) ?? [],
currentUserId
})
const { data: albums } = useCollections(
albumAggregates?.map((sale) => sale.contentId) ?? []
)
const tracksById = useMemo(() => keyBy(tracks, 'track_id'), [tracks])
const albumsById = useMemo(() => keyBy(albums, 'playlist_id'), [albums])

Expand Down
26 changes: 16 additions & 10 deletions packages/common/src/hooks/useCollectionMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pick } from 'lodash'
import { useSelector } from 'react-redux'

import { useGetCurrentUserId } from '~/api/account'
import { useGetPlaylistById } from '~/api/collection'
import { useCollection } from '~/api'
import { ID } from '~/models/Identifiers'
import { getCollectionDuration } from '~/store/cache/collections/selectors'
import { CommonState } from '~/store/commonStore'
Expand Down Expand Up @@ -32,24 +32,30 @@ type CollectionMetadataProps = {
export const useCollectionMetadata = ({
collectionId
}: CollectionMetadataProps): CollectionMetadataInfo[] => {
const { data: currentUserId } = useGetCurrentUserId({})
const { data: collection } = useGetPlaylistById({
playlistId: collectionId,
currentUserId
const { data: collectionMetadata } = useCollection(collectionId, {
select: (playlist) =>
pick(playlist, [
'is_private',
'updated_at',
'release_date',
'created_at',
'playlist_contents'
])
})
const duration = useSelector((state: CommonState) =>
getCollectionDuration(state, { id: collectionId })
)

if (!collection) return []
if (!collectionMetadata) return []

const {
is_private: isPrivate,
updated_at: updatedAt,
release_date: releaseDate,
created_at: createdAt
} = collection
const numTracks = collection.playlist_contents?.track_ids?.length ?? 0
created_at: createdAt,
playlist_contents: playlistContents
} = collectionMetadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't fully understand where createdAt, playlistContents were initialized.. but ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialized? not sure I follow - these are on the metadata of the collection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my b plz ignore

const numTracks = playlistContents?.track_ids?.length ?? 0

const metadataItems = [
{
Expand Down
43 changes: 23 additions & 20 deletions packages/common/src/hooks/useGatedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
} from '~/store/cache'
import { gatedContentSelectors } from '~/store/gated-content'
import { CommonState } from '~/store/reducers'
import { isContentCollection, isContentTrack } from '~/utils/contentTypeUtils'
import {
isContentPartialCollection,
isContentPartialTrack
} from '~/utils/contentTypeUtils'
import { Nullable, removeNullable } from '~/utils/typeUtils'

import { useFeatureFlag } from './useFeatureFlag'
Expand Down Expand Up @@ -64,25 +67,24 @@ export const useGatedCollectionAccess = (collectionId: ID) => {
return { hasStreamAccess }
}

type PartialTrack = Pick<
Track,
| 'track_id'
| 'is_stream_gated'
| 'is_download_gated'
| 'access'
| 'stream_conditions'
| 'download_conditions'
>

type PartialCollection = Pick<
Collection,
'playlist_id' | 'is_stream_gated' | 'access' | 'stream_conditions'
>

// Returns whether user has access to given track.
export const useGatedContentAccess = (
content:
| Nullable<
| Pick<
Track,
| 'track_id'
| 'is_stream_gated'
| 'is_download_gated'
| 'access'
| 'stream_conditions'
| 'download_conditions'
>
| Pick<
Collection,
'playlist_id' | 'is_stream_gated' | 'access' | 'stream_conditions'
>
>
| undefined
content: Nullable<PartialTrack> | Nullable<PartialCollection> | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

) => {
const nftAccessSignatureMap = useSelector(getNftAccessSignatureMap)
const hasAccount = useSelector(getHasAccount)
Expand All @@ -97,8 +99,9 @@ export const useGatedContentAccess = (
}
}

const isTrack = isContentTrack(content)
const isCollection = isContentCollection(content)
const isTrack = isContentPartialTrack<PartialTrack>(content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why we couldn't just typeguard content to a full track? but this is the old code so nbd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm intentionally only selecting whatever args are needed for the track to avoid unnecessary rerenders from the query hook.
I re-did this because typecasting to a full Track is incorrect here since only parts of the track metadata exist here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its an optimization pattern with tan-query that we're going to be doing a lot more of so this isPartialXYZ is something I'll likely use a lot more 😄

const isCollection =
isContentPartialCollection<PartialCollection>(content)
const trackId = isTrack
? content.track_id
: isCollection
Expand Down
10 changes: 10 additions & 0 deletions packages/common/src/utils/contentTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { EntityType } from '~/store'

import { Nullable } from './typeUtils'

/** Typeguard for determining if content is a Track but can return as a subset of the Track type */
export const isContentPartialTrack = <T extends Partial<Track>>(
content?: Nullable<Partial<Track | Collection>>
): content is T => !!content && 'track_id' in content

/** Typeguard for determining if content is a Collection but can return as a subset of the Collection type */
export const isContentPartialCollection = <T extends Partial<Collection>>(
content?: Nullable<Partial<Track | Collection>>
): content is T => !!content && 'playlist_id' in content

/** Typeguard for determiniing if content is a Track */
export const isContentTrack = (
content?: Nullable<Partial<Track | Collection>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback, type ReactNode, useEffect } from 'react'

import {
useCollection,
useCurrentUserId,
useGetPlaylistById,
useGetUserById,
useTrack
} from '@audius/common/api'
Expand Down Expand Up @@ -453,10 +453,9 @@ export const PremiumContentPurchaseDrawer = () => {
const isAlbum = contentType === PurchaseableContentType.ALBUM
const { data: currentUserId } = useCurrentUserId()
const { data: track, isPending: isTrackPending } = useTrack(contentId)
const { data: album } = useGetPlaylistById(
{ playlistId: contentId!, currentUserId },
{ disabled: !isAlbum || !contentId }
)
const { data: album } = useCollection(contentId, {
enabled: isAlbum
})
const { data: user } = useGetUserById(
{
id: track?.owner_id ?? album?.playlist_owner_id ?? 0,
Expand Down
Loading