Compare commits

...

3 Commits

Author SHA1 Message Date
Alejandro González
c152229b0c fix: set required CSP sources for Tally forms to show up 2025-07-21 00:40:48 +02:00
Prospector
fbbe6c70e9 make assigned and dismissed users fields optional 2025-07-21 00:40:48 +02:00
Alejandro González
2989d6e937 feat: surveys 2025-07-21 00:40:48 +02:00
3 changed files with 92 additions and 2 deletions

View File

@ -11,6 +11,7 @@
<body>
<div id="app"></div>
<script src="https://tally.so/widgets/embed.js" async></script>
<script type="module" src="/src/main.js"></script>
</body>
</html>

View File

@ -71,6 +71,8 @@ import { openUrl } from '@tauri-apps/plugin-opener'
import QuickInstanceSwitcher from '@/components/ui/QuickInstanceSwitcher.vue'
import { get_available_capes, get_available_skins } from './helpers/skins'
import { generateSkinPreviews } from './helpers/rendering/batch-skin-renderer'
import { list } from '@/helpers/profile.js'
import { $fetch } from 'ofetch'
const themeStore = useTheming()
@ -219,6 +221,8 @@ async function setupApp() {
} catch (error) {
console.warn('Failed to generate skin previews in app setup.', error)
}
await processPendingSurveys()
}
const stateFailed = ref(false)
@ -393,6 +397,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')
}
}
</script>
<template>

View File

@ -90,8 +90,8 @@
"font-src": ["https://cdn-raw.modrinth.com/fonts/"],
"img-src": "https: 'unsafe-inline' 'self' asset: http://asset.localhost http://textures.minecraft.net blob: data:",
"style-src": "'unsafe-inline' 'self'",
"script-src": "https://*.posthog.com 'self'",
"frame-src": "https://www.youtube.com https://www.youtube-nocookie.com https://discord.com 'self'",
"script-src": "https://*.posthog.com https://tally.so/widgets/embed.js 'self'",
"frame-src": "https://www.youtube.com https://www.youtube-nocookie.com https://discord.com https://tally.so/popup/ 'self'",
"media-src": "https://*.githubusercontent.com"
}
}