From 8193040f2919086f13c79275ae15ef7d034928eb Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Wed, 23 Aug 2023 18:18:44 +0200 Subject: [PATCH] Fixed analytics exports to fetch on demand in Admin X (#17802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://ghost.slack.com/archives/C0568LN2CGJ/p1692804385770949 - Analytics exports was previously retrieved on on page load instead of on demand. - This fixes that to ensure it gets fetched when the user hits the download button. - Renamed the file saving to `post-analytics.yyyy-mm-dd.csv` --- ### 🤖 Generated by Copilot at 2c96e78 Improved the export posts feature in the membership settings by fetching the data only on demand and adding a date to the file name. Modified the `usePostsExports` hook and the `exportPosts` function in `Analytics.tsx`. --- .../components/settings/membership/Analytics.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/apps/admin-x-settings/src/components/settings/membership/Analytics.tsx b/apps/admin-x-settings/src/components/settings/membership/Analytics.tsx index b40e38c377..ccf9c33d3c 100644 --- a/apps/admin-x-settings/src/components/settings/membership/Analytics.tsx +++ b/apps/admin-x-settings/src/components/settings/membership/Analytics.tsx @@ -27,21 +27,22 @@ const Analytics: React.FC<{ keywords: string[] }> = ({keywords}) => { handleEditingChange(true); }; - const {data: postsData} = usePostsExports({ + const {refetch: postsData} = usePostsExports({ searchParams: { limit: '1000' - } + }, + enabled: false }); const exportPosts = async () => { - // it should download posts as csv - if (postsData) { - const blob = new Blob([postsData], {type: 'text/csv'}); + const {data} = await postsData(); + if (data) { + const blob = new Blob([data], {type: 'text/csv'}); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.setAttribute('hidden', ''); a.setAttribute('href', url); - a.setAttribute('download', 'posts.csv'); + a.setAttribute('download', `post-analytics.${new Date().toISOString().split('T')[0]}.csv`); document.body.appendChild(a); a.click(); document.body.removeChild(a);