test
2 months ago
useCompleteOnboarding.js
2 months ago
useDateRangePicker.js
3 months ago
useEmail.ts
3 months ago
useEngagementChartData.js
3 months ago
useLicenseSettings.js
3 months ago
useLink.js
3 months ago
useMediaDetail.js
3 months ago
useMediaLibrary.js
3 months ago
useMediaList.ts
2 months ago
usePerformanceSettings.js
3 months ago
useRegisterActivePage.js
3 months ago
useSettingOption.js
3 months ago
useSimpleSettingsPage.js
3 months ago
useTopPerforming.js
3 months ago
useTopVideosPaginated.js
3 months ago
useUpgradeCTA.js
3 months ago
useUserDetail.js
3 months ago
useTopVideosPaginated.js
116 lines
| 1 | import { useState, useEffect } from "@wordpress/element"; |
| 2 | import apiFetch from "@wordpress/api-fetch"; |
| 3 | import { addQueryArgs } from "@wordpress/url"; |
| 4 | import { transformMediaItem } from "../utils/transformers"; |
| 5 | |
| 6 | /** |
| 7 | * Paginated fetch of /analytics/top-videos with X-WP-Total / |
| 8 | * X-WP-TotalPages support. Caller controls scope via `userId` (filter) |
| 9 | * and `dateParams` ({ start, end } ISO datetime strings) — the hook |
| 10 | * deliberately does no date defaulting so each consumer (Analytics |
| 11 | * "all-time" vs User Detail "last 30 days") keeps its own policy. |
| 12 | * |
| 13 | * Page automatically resets to 1 when the scope changes (userId or |
| 14 | * date range), since the previous page index may not exist in the new |
| 15 | * result set. |
| 16 | * |
| 17 | * @param {Object} options |
| 18 | * @param {number|string|null} [options.userId] |
| 19 | * @param {{start: string, end: string}} options.dateParams |
| 20 | * @param {number} [options.perPage=10] |
| 21 | * @param {boolean} [options.enabled=true] |
| 22 | * |
| 23 | * @returns {{ |
| 24 | * topMedia: Array, |
| 25 | * isLoading: boolean, |
| 26 | * error: ?string, |
| 27 | * page: number, |
| 28 | * setPage: Function, |
| 29 | * pagination: { totalItems: number, totalPages: number }, |
| 30 | * perPage: number, |
| 31 | * }} |
| 32 | */ |
| 33 | const useTopVideosPaginated = ({ |
| 34 | userId, |
| 35 | dateParams, |
| 36 | perPage = 10, |
| 37 | enabled = true, |
| 38 | } = {}) => { |
| 39 | const [topMedia, setTopMedia] = useState([]); |
| 40 | const [page, setPage] = useState(1); |
| 41 | const [pagination, setPagination] = useState({ |
| 42 | totalItems: 0, |
| 43 | totalPages: 0, |
| 44 | }); |
| 45 | const [isLoading, setIsLoading] = useState(enabled); |
| 46 | const [error, setError] = useState(null); |
| 47 | |
| 48 | const start = dateParams?.start || ""; |
| 49 | const end = dateParams?.end || ""; |
| 50 | const userKey = userId ?? ""; |
| 51 | |
| 52 | // Reset to page 1 when scope changes — a previous page index may not |
| 53 | // exist in the new result set. |
| 54 | useEffect(() => { |
| 55 | setPage(1); |
| 56 | }, [userKey, start, end]); |
| 57 | |
| 58 | useEffect(() => { |
| 59 | if (!enabled || !start || !end) { |
| 60 | setIsLoading(false); |
| 61 | return undefined; |
| 62 | } |
| 63 | |
| 64 | const controller = new AbortController(); |
| 65 | |
| 66 | (async () => { |
| 67 | try { |
| 68 | setIsLoading(true); |
| 69 | const args = { start, end, page, per_page: perPage }; |
| 70 | if (userKey !== "") { |
| 71 | args.user_id = userKey; |
| 72 | } |
| 73 | const response = await apiFetch({ |
| 74 | path: addQueryArgs(window.prestoPlayer.api.analyticsTopVideos, args), |
| 75 | parse: false, |
| 76 | signal: controller.signal, |
| 77 | }); |
| 78 | const totalItems = parseInt( |
| 79 | response.headers.get("X-WP-Total") || "0", |
| 80 | 10 |
| 81 | ); |
| 82 | const totalPages = parseInt( |
| 83 | response.headers.get("X-WP-TotalPages") || "0", |
| 84 | 10 |
| 85 | ); |
| 86 | const json = await response.json(); |
| 87 | |
| 88 | setTopMedia((json || []).map(transformMediaItem)); |
| 89 | setPagination({ totalItems, totalPages }); |
| 90 | setError(null); |
| 91 | } catch (err) { |
| 92 | if (err.name === "AbortError") return; |
| 93 | setError(err.message || "Failed to load top videos"); |
| 94 | setTopMedia([]); |
| 95 | setPagination({ totalItems: 0, totalPages: 0 }); |
| 96 | } finally { |
| 97 | setIsLoading(false); |
| 98 | } |
| 99 | })(); |
| 100 | |
| 101 | return () => controller.abort(); |
| 102 | }, [enabled, userKey, start, end, page, perPage]); |
| 103 | |
| 104 | return { |
| 105 | topMedia, |
| 106 | isLoading, |
| 107 | error, |
| 108 | page, |
| 109 | setPage, |
| 110 | pagination, |
| 111 | perPage, |
| 112 | }; |
| 113 | }; |
| 114 | |
| 115 | export default useTopVideosPaginated; |
| 116 |