diff --git a/src/features/marketplace/components/hooks/useMarketplaceInstall.js b/src/features/marketplace/components/hooks/useMarketplaceInstall.js index acb44d1b..1cf3a9bd 100644 --- a/src/features/marketplace/components/hooks/useMarketplaceInstall.js +++ b/src/features/marketplace/components/hooks/useMarketplaceInstall.js @@ -10,6 +10,10 @@ export const useMarketplaceInstall = () => { const controllerRef = useRef(new AbortController()); const installItem = (type, data) => { + // Check if item is already installed before calling install + const installed = JSON.parse(localStorage.getItem('installed') || '[]'); + const isNewInstall = !installed.some((item) => item.id === data.id || item.name === data.name); + install(type, data); toast(variables.getMessage('toasts.installed')); variables.stats.postEvent('marketplace-item', `${data.display_name || data.name} installed`); diff --git a/src/utils/marketplace/install.js b/src/utils/marketplace/install.js index 03c94a80..852f4a4c 100644 --- a/src/utils/marketplace/install.js +++ b/src/utils/marketplace/install.js @@ -1,5 +1,6 @@ import EventBus from 'utils/eventbus'; import { clearQueuesOnSettingChange } from 'utils/queueOperations'; +import variables from 'config/variables'; // todo: relocate this function function showReminder() { @@ -7,9 +8,29 @@ function showReminder() { localStorage.setItem('showReminder', true); } +/** + * Track download count in the API + */ +async function trackDownload(itemId) { + if (!itemId) return; + + try { + await fetch(`${variables.constants.API_URL}/marketplace/item/${itemId}/download`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.debug('Failed to track download:', error); + } +} + export function install(type, input, sideload, collection) { let refreshEvent = null; + // Check if item is already installed to determine if we should track download + const installed = JSON.parse(localStorage.getItem('installed') || '[]'); + const isNewInstall = !installed.some((item) => item.id === input.id || item.name === input.name); + switch (type) { case 'settings': { localStorage.removeItem('backup_settings'); @@ -73,8 +94,6 @@ export function install(type, input, sideload, collection) { break; } - const installed = JSON.parse(localStorage.getItem('installed')); - if (sideload) { input.sideload = true; } @@ -83,6 +102,11 @@ export function install(type, input, sideload, collection) { localStorage.setItem('installed', JSON.stringify(installed)); + // Track download for new installs (not re-installs) + if (isNewInstall && input.id) { + trackDownload(input.id); + } + // Emit refresh event after all data is saved if (refreshEvent) { EventBus.emit('refresh', refreshEvent);