Skip to content

App surveys #3605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/app-frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
88 changes: 88 additions & 0 deletions apps/app-frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -197,6 +199,7 @@ async function setupApp() {
get_opening_command().then(handleCommand)
checkUpdates()
fetchCredentials()
await processPendingSurveys()
}

const stateFailed = ref(false)
Expand Down Expand Up @@ -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')
}
}
</script>

<template>
Expand Down
Loading