@@ -61,6 +61,8 @@ import { hide_ads_window, init_ads_window } from '@/helpers/ads.js'
61
61
import FriendsList from ' @/components/ui/friends/FriendsList.vue'
62
62
import { openUrl } from ' @tauri-apps/plugin-opener'
63
63
import QuickInstanceSwitcher from ' @/components/ui/QuickInstanceSwitcher.vue'
64
+ import { list } from ' @/helpers/profile.js'
65
+ import { $fetch } from ' ofetch'
64
66
65
67
const themeStore = useTheming ()
66
68
@@ -188,6 +190,7 @@ async function setupApp() {
188
190
get_opening_command ().then (handleCommand)
189
191
checkUpdates ()
190
192
fetchCredentials ()
193
+ processPendingSurveys ()
191
194
}
192
195
193
196
const stateFailed = ref (false )
@@ -361,6 +364,85 @@ function handleAuxClick(e) {
361
364
e .target .dispatchEvent (event )
362
365
}
363
366
}
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
+ }
364
446
< / script>
365
447
366
448
< template>
0 commit comments