Line data Source code
1 : <template>
2 0 : <AppDialogPanel unpadded class="w-lg max-w-full" @close="onClose">
3 : <template #title>
4 : <i class="icon-[comfy--extensions-blocks]" />
5 : {{ $t('manager.survey.title') }}
6 : </template>
7 :
8 : <main class="px-5 py-4">
9 : <p
10 : v-if="status !== 'error'"
11 : class="mt-1 mb-4 text-sm text-muted-foreground"
12 : >
13 : {{ $t('manager.survey.intro') }}
14 : </p>
15 : <div class="relative">
16 : <iframe
17 : v-if="surveyUrl && status !== 'error'"
18 : :src="surveyUrl"
19 : :title="$t('manager.survey.title')"
20 : :style="{ height: `${iframeHeight}px` }"
21 : sandbox="allow-scripts allow-same-origin allow-forms"
22 : data-testid="manager-survey-iframe"
23 : class="w-full rounded-lg border-0"
24 : @load="onIframeLoad"
25 : />
26 : <div
27 : v-if="status === 'loading'"
28 : data-testid="manager-survey-loading"
29 : class="pointer-events-none absolute inset-0 flex items-center justify-center"
30 : >
31 : <i
32 : class="icon-[lucide--loader-2] size-6 animate-spin text-muted-foreground"
33 : />
34 : </div>
35 : <div
36 : v-else-if="status === 'error'"
37 : data-testid="manager-survey-error"
38 : class="flex min-h-56 flex-col items-center justify-center gap-2 text-center text-muted-foreground"
39 : >
40 : <i class="icon-[lucide--triangle-alert] size-6" />
41 : <p class="m-0 text-sm">
42 : {{ $t('manager.survey.error') }}
43 : </p>
44 : </div>
45 : </div>
46 : </main>
47 : </AppDialogPanel>
48 : </template>
49 :
50 : <script setup lang="ts">
51 : import { useEventListener, useTimeoutFn } from '@vueuse/core'
52 : import { computed, ref, watch } from 'vue'
53 :
54 : import AppDialogPanel from '@/components/dialog/AppDialogPanel.vue'
55 : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
56 : import { remoteConfig } from '@/platform/remoteConfig/remoteConfig'
57 :
58 2355 : const DEFAULT_IFRAME_HEIGHT = 460
59 2355 : const MAX_IFRAME_HEIGHT = 10000
60 2355 : const SURVEY_LOAD_TIMEOUT_MS = 8000
61 :
62 : defineProps<{
63 : onClose: () => void
64 : }>()
65 :
66 0 : const { resolvedUserInfo } = useCurrentUser()
67 :
68 : // The hosted (external) survey URL is provided per environment by cloud config.
69 0 : const surveyUrl = computed(() => {
70 0 : const base = remoteConfig.value.manager_survey_url
71 0 : if (!base) return undefined
72 0 : try {
73 0 : const url = new URL(base)
74 : // Link responses to the logged-in user; omit for anonymous responses.
75 0 : const distinctId = resolvedUserInfo.value?.id
76 0 : if (distinctId) url.searchParams.set('distinct_id', distinctId)
77 0 : return url.toString()
78 : } catch {
79 : // A malformed configured URL falls back to the error state.
80 0 : return undefined
81 : }
82 : })
83 :
84 0 : const parsedSurveyUrl = computed(() =>
85 : surveyUrl.value ? new URL(surveyUrl.value) : undefined
86 : )
87 0 : const surveyOrigin = computed(() => parsedSurveyUrl.value?.origin)
88 : // filter(Boolean) tolerates a trailing slash in the configured URL.
89 0 : const surveyId = computed(() =>
90 : parsedSurveyUrl.value?.pathname.split('/').filter(Boolean).pop()
91 : )
92 :
93 0 : const status = ref<'loading' | 'ready' | 'error'>('loading')
94 0 : const iframeHeight = ref(DEFAULT_IFRAME_HEIGHT)
95 :
96 0 : const { start: startLoadTimeout, stop: stopLoadTimeout } = useTimeoutFn(
97 : () => {
98 0 : if (status.value === 'loading') status.value = 'error'
99 : },
100 : SURVEY_LOAD_TIMEOUT_MS,
101 : { immediate: false }
102 : )
103 :
104 : // Re-derive load state whenever the survey URL changes — e.g. cloud config or
105 : // the user identity arriving after the dialog opens — so a late URL recovers
106 : // from the error state instead of staying stuck.
107 0 : watch(
108 : surveyUrl,
109 : (url) => {
110 0 : if (!url) {
111 0 : stopLoadTimeout()
112 0 : status.value = 'error'
113 0 : return
114 : }
115 0 : status.value = 'loading'
116 0 : startLoadTimeout()
117 : },
118 : { immediate: true }
119 : )
120 :
121 0 : const onIframeLoad = () => {
122 0 : if (status.value === 'loading') status.value = 'ready'
123 0 : stopLoadTimeout()
124 : }
125 :
126 : // PostHog's embedded survey posts its content height for auto-resizing.
127 0 : useEventListener(window, 'message', (event: MessageEvent) => {
128 0 : if (event.origin !== surveyOrigin.value) return
129 0 : const data = event.data
130 0 : if (
131 : data?.type === 'posthog:survey:height' &&
132 : data?.surveyId === surveyId.value
133 : ) {
134 0 : const height = Number.parseInt(data.height, 10)
135 0 : if (height > 0 && height < MAX_IFRAME_HEIGHT) iframeHeight.value = height
136 : }
137 : })
138 : </script>
|