Skip to content

Commit

Permalink
add source videos count in channel home
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaz9 committed Jul 24, 2024
1 parent fa621ab commit ce3ff00
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/components/profile/ProfileIndexPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface ProfileIndexPreviewProps {
const ProfileIndexPreview: React.FC<ProfileIndexPreviewProps> = ({ address, url }) => {
const hostname = urlHostname(url)!
const [hasSomeVideos, setHasSomeVideos] = useState<boolean>()
const [total, setTotal] = useState<number>()

return (
<ProfileSource>
Expand All @@ -54,7 +55,8 @@ const ProfileIndexPreview: React.FC<ProfileIndexPreviewProps> = ({ address, url
<span>Index {hostname}</span>
</ProfileSourceTitle>
<ProfileSourceDescription>
Videos list provided by the {hostname} index
<span>Videos list provided by the {hostname} index</span>
{total != null && <span> - {total} videos</span>}
</ProfileSourceDescription>
</ProfileSourceHeaderSection>
</ProfileSourceHeader>
Expand All @@ -64,7 +66,10 @@ const ProfileIndexPreview: React.FC<ProfileIndexPreviewProps> = ({ address, url
source={{ type: "index", url }}
variant="preview"
rows={2}
onVideosFetched={videos => setHasSomeVideos(videos.length > 0)}
onVideosFetched={(videos, total) => {
setHasSomeVideos(videos.length > 0)
setTotal(total)
}}
/>

{hasSomeVideos === false && (
Expand Down
3 changes: 3 additions & 0 deletions src/components/profile/ProfilePlaylistPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const ProfilePlaylistPreview: React.FC<ProfilePlaylistPreviewProps> = ({
})()}
</>
)}
{playlistQuery.isSuccess && (
<span> - {playlistQuery.data.details.videos.length} videos</span>
)}
</ProfileSourceDescription>
</ProfileSourceHeaderSection>
{"rootManifest" in identification && (
Expand Down
9 changes: 5 additions & 4 deletions src/components/profile/ProfileSourceVideos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface ProfileSourceVideosProps {
isLoading?: boolean
variant?: "default" | "preview"
rows?: number
onVideosFetched?: (videos: Video[]) => void
onVideosFetched?: (videos: Video[], total: number) => void
}

const ProfileSourceVideos: React.FC<ProfileSourceVideosProps> = ({
Expand Down Expand Up @@ -68,15 +68,16 @@ const ProfileSourceVideos: React.FC<ProfileSourceVideosProps> = ({
sequentialFetchCount: 12,
})

const videos = (videosQuery.data?.pages.flat() ?? []).flatMap(({ videos }) => videos)

useEffect(() => {
if (videosQuery.isSuccess) {
onVideosFetched?.(videosQuery.data?.pages.flat() ?? [])
const total = videosQuery.data?.pages[0]?.total ?? 0
onVideosFetched?.(videos, total)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [videosQuery.data])

const videos = videosQuery.data?.pages.flat() ?? []

return (
<div>
{error && !(error instanceof FundsMissingError) && (
Expand Down
14 changes: 9 additions & 5 deletions src/queries/channel-videos-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ export const useChannelVideosQuery = (opts: ChannelVideosQueryOptions) => {
const ownerAddress = await getOwnerAddress()
const source = opts.source

let videos: VideoWithOwner[] = []

if (source?.type === "index") {
const client = new IndexClient(source.url)
const resp = await client.users.fetchVideos(ownerAddress, page, limit)

total.current = resp.totalElements

return resp.elements
videos = resp.elements
.filter(vid => vid.lastValidManifest)
.map(indexVideo => {
try {
Expand Down Expand Up @@ -112,19 +114,21 @@ export const useChannelVideosQuery = (opts: ChannelVideosQueryOptions) => {
})
.filter(Boolean) as VideoWithOwner[]
} else if (source?.type === "playlist") {
total.current = source.data.details.videos.length

const from = input.pageParam
? firstFetchPagesCount + (input.pageParam - 1) * sequentialFetchCount
: 0
const to = from + limit
const vids = source.data.details.videos.slice(from, to) ?? []
const videos = await Promise.all(
const playlistVideos = await Promise.all(
vids.map(async playlistVid => {
return await queryClient.fetchQuery(
useVideoPreviewQuery.getQueryConfig({ reference: playlistVid.reference })
)
})
)
const videosIndexes = videos.map<VideoWithOwner>((video, i) => ({
videos = playlistVideos.map<VideoWithOwner>((video, i) => ({
reference: video?.reference ?? (vids[i]!.reference as Reference),
preview: video?.preview ?? {
reference: "" as Reference,
Expand All @@ -140,11 +144,11 @@ export const useChannelVideosQuery = (opts: ChannelVideosQueryOptions) => {
indexesStatus: {},
owner: profile,
}))

return videosIndexes
} else {
throw new Error("Invalid source")
}

return { videos, total: total.current }
},
initialPageParam: 0,
getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
Expand Down

0 comments on commit ce3ff00

Please sign in to comment.