PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.3
Presto Player v4.3.3
4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / src / admin / dashboard / hooks / useTopVideosPaginated.js
presto-player / src / admin / dashboard / hooks Last commit date
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