Skip to content

Commit 180230d

Browse files
committed
feat: surveys
1 parent f71830e commit 180230d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

apps/app-frontend/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<body>
1313
<div id="app"></div>
14+
<script src="https://tally.so/widgets/embed.js" defer></script>
1415
<script type="module" src="/src/main.js"></script>
1516
</body>
1617
</html>

apps/app-frontend/src/App.vue

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import { hide_ads_window, init_ads_window } from '@/helpers/ads.js'
6161
import FriendsList from '@/components/ui/friends/FriendsList.vue'
6262
import { openUrl } from '@tauri-apps/plugin-opener'
6363
import QuickInstanceSwitcher from '@/components/ui/QuickInstanceSwitcher.vue'
64+
import { list } from '@/helpers/profile.js'
65+
import { $fetch } from 'ofetch'
6466
6567
const themeStore = useTheming()
6668
@@ -188,6 +190,7 @@ async function setupApp() {
188190
get_opening_command().then(handleCommand)
189191
checkUpdates()
190192
fetchCredentials()
193+
processPendingSurveys()
191194
}
192195
193196
const stateFailed = ref(false)
@@ -361,6 +364,85 @@ function handleAuxClick(e) {
361364
e.target.dispatchEvent(event)
362365
}
363366
}
367+
368+
function cleanupOldSurveyDisplayData() {
369+
const threeWeeksAgo = new Date()
370+
threeWeeksAgo.setDate(threeWeeksAgo.getDate() - 21)
371+
372+
for (let i = 0; i < localStorage.length; i++) {
373+
const key = localStorage.key(i)
374+
375+
if (key.startsWith('survey-') && key.endsWith('-display')) {
376+
const dateValue = new Date(localStorage.getItem(key))
377+
if (dateValue < threeWeeksAgo) {
378+
localStorage.removeItem(key)
379+
}
380+
}
381+
}
382+
}
383+
384+
async function processPendingSurveys() {
385+
function isWithinLastTwoWeeks(date) {
386+
const twoWeeksAgo = new Date()
387+
twoWeeksAgo.setDate(twoWeeksAgo.getDate() - 14)
388+
return date >= twoWeeksAgo
389+
}
390+
391+
cleanupOldSurveyDisplayData()
392+
393+
const creds = await getCreds().catch(handleError)
394+
const userId = creds?.user_id
395+
396+
const instances = await list().catch(handleError)
397+
const isActivePlayer =
398+
instances.findIndex(
399+
(instance) =>
400+
isWithinLastTwoWeeks(instance.last_played) && !isWithinLastTwoWeeks(instance.created),
401+
) >= 0
402+
403+
const surveys = await $fetch('https://api.modrinth.com/v2/surveys')
404+
405+
const surveyToShow = surveys.find(
406+
(survey) =>
407+
localStorage.getItem(`survey-${survey.id}-display`) === null &&
408+
((survey.condition === 'active_player' && isActivePlayer) ||
409+
(survey.assigned_users.includes(userId) && !survey.dismissed_users.includes(userId))),
410+
)
411+
412+
if (surveyToShow) {
413+
const formId = surveyToShow.tally_id
414+
415+
const popupOptions = {
416+
layout: 'modal',
417+
width: 700,
418+
autoClose: 2000,
419+
hideTitle: true,
420+
hiddenFields: {
421+
user_id: userId,
422+
},
423+
onOpen: () => console.info('Opened user survey'),
424+
onClose: () => console.info('Closed user survey'),
425+
onSubmit: () => console.info('Active user submitted'),
426+
}
427+
428+
try {
429+
if (window.Tally?.openPopup) {
430+
console.info(`Opening Tally popup for user survey (form ID: ${formId})`)
431+
localStorage.setItem(`survey-${surveyToShow.id}-display`, new Date())
432+
window.Tally.openPopup(formId, popupOptions)
433+
} else {
434+
console.warn('Tally script not yet loaded')
435+
}
436+
} catch (e) {
437+
console.error('Error opening Tally popup:', e)
438+
}
439+
440+
console.info(`Found user survey to show with tally_id: ${tallyId}`)
441+
window.Tally.openPopup(tallyId, popupOptions)
442+
} else {
443+
console.info('No user survey to show')
444+
}
445+
}
364446
</script>
365447
366448
<template>

0 commit comments

Comments
 (0)