|
| 1 | +import { Text } from "@components/text"; |
| 2 | +import type { SignalReport } from "@posthog/shared/domain-types"; |
| 3 | +import * as Haptics from "expo-haptics"; |
| 4 | +import { ThumbsDown, ThumbsUp } from "phosphor-react-native"; |
| 5 | +import { useCallback, useState } from "react"; |
| 6 | +import { Pressable, TextInput, View } from "react-native"; |
| 7 | +import { |
| 8 | + ANALYTICS_EVENTS, |
| 9 | + computeReportAgeHours, |
| 10 | + type InboxReportFeedbackSentiment, |
| 11 | + useAnalytics, |
| 12 | +} from "@/lib/analytics"; |
| 13 | +import { useThemeColors } from "@/lib/theme"; |
| 14 | + |
| 15 | +/** Bounded to keep the note within the analytics client's per-property limit. */ |
| 16 | +const FEEDBACK_NOTE_MAX_LENGTH = 4000; |
| 17 | + |
| 18 | +/** |
| 19 | + * Thumbs rating at the end of the report body. The rating submits on the first |
| 20 | + * tap; only once it's recorded does an optional note appear, so the note can |
| 21 | + * never gate the rating. Feedback is analytics-only — it never changes the |
| 22 | + * report's state. Mirrors the desktop/cloud feedback footer so ranking |
| 23 | + * analysis is comparable across clients. |
| 24 | + */ |
| 25 | +export function ReportFeedbackFooter({ report }: { report: SignalReport }) { |
| 26 | + const analytics = useAnalytics(); |
| 27 | + const themeColors = useThemeColors(); |
| 28 | + |
| 29 | + const [sentiment, setSentiment] = |
| 30 | + useState<InboxReportFeedbackSentiment | null>(null); |
| 31 | + const [noteOpen, setNoteOpen] = useState(false); |
| 32 | + const [noteDraft, setNoteDraft] = useState(""); |
| 33 | + const [noteSent, setNoteSent] = useState(false); |
| 34 | + |
| 35 | + // Re-tapping the chosen thumb is a no-op rather than a second identical |
| 36 | + // feedback event; switching thumbs records the new sentiment. |
| 37 | + const chooseSentiment = useCallback( |
| 38 | + (next: InboxReportFeedbackSentiment) => { |
| 39 | + if (sentiment === next) return; |
| 40 | + Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light); |
| 41 | + setSentiment(next); |
| 42 | + analytics.track(ANALYTICS_EVENTS.INBOX_REPORT_FEEDBACK, { |
| 43 | + report_id: report.id, |
| 44 | + report_age_hours: computeReportAgeHours(report.created_at), |
| 45 | + priority: report.priority ?? null, |
| 46 | + actionability: report.actionability ?? null, |
| 47 | + sentiment: next, |
| 48 | + has_pr: !!report.implementation_pr_url, |
| 49 | + surface: "detail_footer", |
| 50 | + }); |
| 51 | + }, |
| 52 | + [sentiment, analytics, report], |
| 53 | + ); |
| 54 | + |
| 55 | + const submitNote = useCallback(() => { |
| 56 | + const trimmed = noteDraft.trim(); |
| 57 | + if (!trimmed || !sentiment) return; |
| 58 | + analytics.track(ANALYTICS_EVENTS.INBOX_REPORT_FEEDBACK_NOTE, { |
| 59 | + report_id: report.id, |
| 60 | + report_age_hours: computeReportAgeHours(report.created_at), |
| 61 | + priority: report.priority ?? null, |
| 62 | + actionability: report.actionability ?? null, |
| 63 | + sentiment, |
| 64 | + has_pr: !!report.implementation_pr_url, |
| 65 | + surface: "detail_footer", |
| 66 | + note: trimmed.slice(0, FEEDBACK_NOTE_MAX_LENGTH), |
| 67 | + }); |
| 68 | + setNoteDraft(""); |
| 69 | + setNoteOpen(false); |
| 70 | + setNoteSent(true); |
| 71 | + }, [noteDraft, sentiment, analytics, report]); |
| 72 | + |
| 73 | + const isPositive = sentiment === "positive"; |
| 74 | + const isNegative = sentiment === "negative"; |
| 75 | + |
| 76 | + return ( |
| 77 | + <View className="mt-2 border-gray-4 border-t pt-4"> |
| 78 | + <View className="flex-row flex-wrap items-center gap-2"> |
| 79 | + <Text className="text-[13px] text-gray-11"> |
| 80 | + {sentiment ? "Thanks for the feedback" : "Was this report useful?"} |
| 81 | + </Text> |
| 82 | + <View className="flex-row items-center gap-1.5"> |
| 83 | + <Pressable |
| 84 | + onPress={() => chooseSentiment("positive")} |
| 85 | + accessibilityRole="button" |
| 86 | + accessibilityLabel="This report was useful" |
| 87 | + accessibilityState={{ selected: isPositive }} |
| 88 | + hitSlop={6} |
| 89 | + className={`rounded-full border px-3 py-2 active:opacity-70 ${ |
| 90 | + isPositive |
| 91 | + ? "border-accent-9 bg-accent-9" |
| 92 | + : "border-gray-6 bg-background" |
| 93 | + }`} |
| 94 | + > |
| 95 | + <ThumbsUp |
| 96 | + size={16} |
| 97 | + weight={isPositive ? "fill" : "regular"} |
| 98 | + color={isPositive ? "#ffffff" : themeColors.gray[11]} |
| 99 | + /> |
| 100 | + </Pressable> |
| 101 | + <Pressable |
| 102 | + onPress={() => chooseSentiment("negative")} |
| 103 | + accessibilityRole="button" |
| 104 | + accessibilityLabel="This report was not useful" |
| 105 | + accessibilityState={{ selected: isNegative }} |
| 106 | + hitSlop={6} |
| 107 | + className={`rounded-full border px-3 py-2 active:opacity-70 ${ |
| 108 | + isNegative |
| 109 | + ? "border-accent-9 bg-accent-9" |
| 110 | + : "border-gray-6 bg-background" |
| 111 | + }`} |
| 112 | + > |
| 113 | + <ThumbsDown |
| 114 | + size={16} |
| 115 | + weight={isNegative ? "fill" : "regular"} |
| 116 | + color={isNegative ? "#ffffff" : themeColors.gray[11]} |
| 117 | + /> |
| 118 | + </Pressable> |
| 119 | + </View> |
| 120 | + {sentiment && !noteOpen && !noteSent && ( |
| 121 | + <Pressable |
| 122 | + onPress={() => setNoteOpen(true)} |
| 123 | + hitSlop={6} |
| 124 | + className="rounded-full px-2 py-1 active:opacity-60" |
| 125 | + > |
| 126 | + <Text className="text-[13px] text-accent-11">Add a note</Text> |
| 127 | + </Pressable> |
| 128 | + )} |
| 129 | + {noteSent && ( |
| 130 | + <Text className="text-[13px] text-gray-9">Note added</Text> |
| 131 | + )} |
| 132 | + </View> |
| 133 | + {noteOpen && ( |
| 134 | + <View className="mt-3"> |
| 135 | + <TextInput |
| 136 | + value={noteDraft} |
| 137 | + onChangeText={setNoteDraft} |
| 138 | + placeholder="What was useful or off?" |
| 139 | + placeholderTextColor={themeColors.gray[9]} |
| 140 | + multiline |
| 141 | + maxLength={FEEDBACK_NOTE_MAX_LENGTH} |
| 142 | + autoFocus |
| 143 | + className="min-h-[80px] rounded-xl bg-gray-2 px-3 py-3 text-[14px] text-gray-12" |
| 144 | + style={{ textAlignVertical: "top" }} |
| 145 | + /> |
| 146 | + <View className="mt-2 flex-row items-center justify-end"> |
| 147 | + <Pressable |
| 148 | + onPress={submitNote} |
| 149 | + disabled={noteDraft.trim().length === 0} |
| 150 | + accessibilityRole="button" |
| 151 | + accessibilityLabel="Send note" |
| 152 | + className={`rounded-full px-4 py-2.5 active:opacity-80 ${ |
| 153 | + noteDraft.trim().length === 0 ? "bg-gray-4" : "bg-accent-9" |
| 154 | + }`} |
| 155 | + > |
| 156 | + <Text className="font-semibold text-[14px] text-white">Send</Text> |
| 157 | + </Pressable> |
| 158 | + </View> |
| 159 | + </View> |
| 160 | + )} |
| 161 | + </View> |
| 162 | + ); |
| 163 | +} |
0 commit comments