|
1 | 1 | import { useLocalSearchParams } from "expo-router"; |
2 | 2 | import { RootStackParams } from "../../app/_layout"; |
3 | | -import { Query, useInfiniteQuery, useMutation, useQuery } from "@tanstack/react-query"; |
4 | | -import { GetVideosVideo } from "../models"; |
| 3 | +import { Query, useInfiniteQuery, useMutation, useQueries, useQuery } from "@tanstack/react-query"; |
| 4 | +import { GetVideosVideo, OwnTubeError } from "../models"; |
5 | 5 | import { ApiServiceImpl } from "../peertubeVideosApi"; |
6 | | -import { VideosCommonQuery, Video } from "@peertube/peertube-types"; |
| 6 | +import { VideosCommonQuery, Video, VideoCaption } from "@peertube/peertube-types"; |
7 | 7 | import { SOURCES } from "../../types"; |
8 | 8 | import { getLocalData, retry } from "../helpers"; |
9 | 9 |
|
@@ -142,3 +142,48 @@ export const useGetVideoCaptionsQuery = (id?: string, enabled = true) => { |
142 | 142 | retry, |
143 | 143 | }); |
144 | 144 | }; |
| 145 | + |
| 146 | +export const useGetVideoCaptionsCollectionQuery = (videoIds: string[] = [], queryKey: string) => { |
| 147 | + const { backend } = useLocalSearchParams<RootStackParams["index"]>(); |
| 148 | + |
| 149 | + return useQueries({ |
| 150 | + queries: videoIds.map((videoId) => ({ |
| 151 | + queryKey: [queryKey, videoId, "captions"], |
| 152 | + queryFn: async () => { |
| 153 | + try { |
| 154 | + return await ApiServiceImpl.getVideoCaptions(backend!, videoId!); |
| 155 | + } catch (e) { |
| 156 | + throw new OwnTubeError({ message: (e as unknown as { message: string }).message }); |
| 157 | + } |
| 158 | + }, |
| 159 | + retry, |
| 160 | + enabled: !!backend && videoIds.length > 0, |
| 161 | + })), |
| 162 | + combine: (result) => { |
| 163 | + return result.filter(({ data }) => !!data).map(({ data }) => data || ([] as VideoCaption[])); |
| 164 | + }, |
| 165 | + }); |
| 166 | +}; |
| 167 | + |
| 168 | +export const useGetVideoFullInfoCollectionQuery = (videoIds: string[] = [], queryKey: string) => { |
| 169 | + const { backend } = useLocalSearchParams<RootStackParams["index"]>(); |
| 170 | + |
| 171 | + return useQueries({ |
| 172 | + queries: videoIds.map((videoId) => ({ |
| 173 | + queryKey: [queryKey, videoId], |
| 174 | + queryFn: async () => { |
| 175 | + try { |
| 176 | + const res = await ApiServiceImpl.getVideo(backend!, videoId!); |
| 177 | + return { ...res, previewPath: `https://${backend}${res?.previewPath}` }; |
| 178 | + } catch (e) { |
| 179 | + throw new OwnTubeError({ message: (e as unknown as { message: string }).message }); |
| 180 | + } |
| 181 | + }, |
| 182 | + retry, |
| 183 | + enabled: !!backend && videoIds.length > 0, |
| 184 | + })), |
| 185 | + combine: (result) => { |
| 186 | + return result.filter(({ data }) => !!data).map(({ data }) => data || ({} as Video)); |
| 187 | + }, |
| 188 | + }); |
| 189 | +}; |
0 commit comments