1
1
'use client'
2
2
3
+ import { useAppPostHogProvider } from '@/features/posthog-provider'
3
4
import { l } from '@/lib/clients/logger/logger'
4
- import { useToast } from '@/lib/hooks/use-toast'
5
5
import { Popover , PopoverContent } from '@/ui/primitives/popover'
6
6
import { SurveyContent } from '@/ui/survey'
7
7
import { PopoverTrigger } from '@radix-ui/react-popover'
8
- import { Survey } from 'posthog-js'
9
8
import { usePostHog } from 'posthog-js/react'
10
9
import { useCallback , useState } from 'react'
11
- import useSWR from 'swr '
10
+ import { toast } from 'sonner '
12
11
13
12
interface DashboardSurveyPopoverProps {
14
13
trigger : React . ReactNode
15
14
}
16
15
17
16
function DashboardSurveyPopover ( { trigger } : DashboardSurveyPopoverProps ) {
18
17
const posthog = usePostHog ( )
19
- const { toast } = useToast ( )
20
18
const [ isOpen , setIsOpen ] = useState ( false )
21
19
const [ wasSubmitted , setWasSubmitted ] = useState ( false )
22
20
23
- const { data : survey , isLoading } = useSWR < Survey | undefined > (
24
- [ 'dashboard-feedback-survey' , posthog . __loaded ] ,
25
- ( ) => {
26
- return new Promise < Survey | undefined > ( ( resolve ) => {
27
- posthog . getSurveys ( ( surveys ) => {
28
- for ( const survey of surveys ) {
29
- if (
30
- survey . id ===
31
- process . env . NEXT_PUBLIC_POSTHOG_DASHBOARD_FEEDBACK_SURVEY_ID
32
- ) {
33
- resolve ( survey )
34
- return
35
- }
36
- }
37
- resolve ( undefined )
38
- } )
39
- } )
40
- } ,
41
- {
42
- revalidateOnFocus : false ,
43
- revalidateOnReconnect : false ,
44
- revalidateIfStale : false ,
45
- dedupingInterval : Infinity ,
46
- shouldRetryOnError : false ,
47
- }
48
- )
21
+ const { dashboardFeedbackSurvey, isInitialized } = useAppPostHogProvider ( )
49
22
50
23
const handleSubmit = useCallback (
51
24
( responses : Record < number , string > ) => {
52
- if ( ! survey ) return
25
+ if ( ! dashboardFeedbackSurvey ) return
53
26
54
27
const responseData = Object . entries ( responses ) . reduce (
55
28
( acc , [ index , response ] ) => ( {
@@ -60,31 +33,34 @@ function DashboardSurveyPopover({ trigger }: DashboardSurveyPopoverProps) {
60
33
)
61
34
62
35
posthog . capture ( 'survey sent' , {
63
- $survey_id : survey . id ,
36
+ $survey_id : dashboardFeedbackSurvey . id ,
64
37
...responseData ,
65
38
} )
66
39
67
40
setWasSubmitted ( true )
68
41
69
- toast ( {
70
- title : 'Thank you!' ,
71
- description : 'Your feedback has been recorded.' ,
72
- } )
42
+ toast . success ( 'Thank you! Your feedback has been recorded.' )
73
43
74
44
// reset states
75
45
setIsOpen ( false )
76
46
setTimeout ( ( ) => {
77
47
setWasSubmitted ( false )
78
48
} , 100 )
79
49
} ,
80
- [ survey , posthog , toast ]
50
+ [ dashboardFeedbackSurvey , posthog ]
81
51
)
82
52
53
+ // we will optimistically render the button on first render.
54
+ // if we can't resolve the survey on the client side, we hide the button.
55
+ if ( ! dashboardFeedbackSurvey && isInitialized ) {
56
+ return null
57
+ }
58
+
83
59
return (
84
60
< Popover
85
61
open = { isOpen }
86
62
onOpenChange = { ( open ) => {
87
- if ( ! survey ) {
63
+ if ( ! dashboardFeedbackSurvey ) {
88
64
l . error (
89
65
{
90
66
key : 'dashboard_survey_popover:survey_not_found' ,
@@ -98,14 +74,14 @@ function DashboardSurveyPopover({ trigger }: DashboardSurveyPopoverProps) {
98
74
return
99
75
}
100
76
101
- if ( ! open && ! wasSubmitted && survey ) {
77
+ if ( ! open && ! wasSubmitted && dashboardFeedbackSurvey ) {
102
78
posthog . capture ( 'survey dismissed' , {
103
- $survey_id : survey . id ,
79
+ $survey_id : dashboardFeedbackSurvey . id ,
104
80
} )
105
81
}
106
- if ( open && survey ) {
82
+ if ( open && dashboardFeedbackSurvey ) {
107
83
posthog . capture ( 'survey shown' , {
108
- $survey_id : survey . id ,
84
+ $survey_id : dashboardFeedbackSurvey . id ,
109
85
} )
110
86
}
111
87
setIsOpen ( open )
@@ -118,10 +94,10 @@ function DashboardSurveyPopover({ trigger }: DashboardSurveyPopoverProps) {
118
94
collisionPadding = { 20 }
119
95
sideOffset = { 25 }
120
96
>
121
- { survey && (
97
+ { dashboardFeedbackSurvey && (
122
98
< SurveyContent
123
- survey = { survey }
124
- isLoading = { isLoading }
99
+ survey = { dashboardFeedbackSurvey }
100
+ isLoading = { ! isInitialized }
125
101
onSubmit = { handleSubmit }
126
102
/>
127
103
) }
0 commit comments