diff --git a/apps/app-frontend/index.html b/apps/app-frontend/index.html index 891f575f2..e5549f28a 100644 --- a/apps/app-frontend/index.html +++ b/apps/app-frontend/index.html @@ -11,6 +11,7 @@
+ diff --git a/apps/app-frontend/src/App.vue b/apps/app-frontend/src/App.vue index 760c8d47e..21653186a 100644 --- a/apps/app-frontend/src/App.vue +++ b/apps/app-frontend/src/App.vue @@ -68,6 +68,8 @@ import { hide_ads_window, init_ads_window } from '@/helpers/ads.js' import FriendsList from '@/components/ui/friends/FriendsList.vue' import { openUrl } from '@tauri-apps/plugin-opener' import QuickInstanceSwitcher from '@/components/ui/QuickInstanceSwitcher.vue' +import { list } from '@/helpers/profile.js' +import { $fetch } from 'ofetch' const formatRelativeTime = useRelativeTime() @@ -197,6 +199,7 @@ async function setupApp() { get_opening_command().then(handleCommand) checkUpdates() fetchCredentials() + await processPendingSurveys() } const stateFailed = ref(false) @@ -370,6 +373,91 @@ function handleAuxClick(e) { e.target.dispatchEvent(event) } } + +function cleanupOldSurveyDisplayData() { + const threeWeeksAgo = new Date() + threeWeeksAgo.setDate(threeWeeksAgo.getDate() - 21) + + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i) + + if (key.startsWith('survey-') && key.endsWith('-display')) { + const dateValue = new Date(localStorage.getItem(key)) + if (dateValue < threeWeeksAgo) { + localStorage.removeItem(key) + } + } + } +} + +async function processPendingSurveys() { + function isWithinLastTwoWeeks(date) { + const twoWeeksAgo = new Date() + twoWeeksAgo.setDate(twoWeeksAgo.getDate() - 14) + return date >= twoWeeksAgo + } + + cleanupOldSurveyDisplayData() + + const creds = await getCreds().catch(handleError) + const userId = creds?.user_id + + const instances = await list().catch(handleError) + const isActivePlayer = + instances.findIndex( + (instance) => + isWithinLastTwoWeeks(instance.last_played) && !isWithinLastTwoWeeks(instance.created), + ) >= 0 + + let surveys = [] + try { + surveys = await $fetch('https://api.modrinth.com/v2/surveys') + } catch (e) { + console.error('Error fetching surveys:', e) + } + + const surveyToShow = surveys.find( + (survey) => + localStorage.getItem(`survey-${survey.id}-display`) === null && + survey.type === 'tally_app' && + ((survey.condition === 'active_player' && isActivePlayer) || + (survey.assigned_users.includes(userId) && !survey.dismissed_users.includes(userId))), + ) + + if (surveyToShow) { + const formId = surveyToShow.tally_id + + const popupOptions = { + layout: 'modal', + width: 700, + autoClose: 2000, + hideTitle: true, + hiddenFields: { + user_id: userId, + }, + onOpen: () => console.info('Opened user survey'), + onClose: () => console.info('Closed user survey'), + onSubmit: () => console.info('Active user survey submitted'), + } + + try { + if (window.Tally?.openPopup) { + console.info(`Opening Tally popup for user survey (form ID: ${formId})`) + localStorage.setItem(`survey-${surveyToShow.id}-display`, new Date()) + window.Tally.openPopup(formId, popupOptions) + } else { + console.warn('Tally script not yet loaded') + } + } catch (e) { + console.error('Error opening Tally popup:', e) + } + + console.info(`Found user survey to show with tally_id: ${formId}`) + window.Tally.openPopup(formId, popupOptions) + } else { + console.info('No user survey to show') + } +}