|
| 1 | +import React, { useCallback, useState } from 'react'; |
| 2 | +import { useRouter } from 'next/router'; |
| 3 | +import HyperDX from '@hyperdx/browser'; |
| 4 | +import { |
| 5 | + ActionIcon, |
| 6 | + Box, |
| 7 | + Button, |
| 8 | + Group, |
| 9 | + Text, |
| 10 | + Textarea, |
| 11 | + Tooltip, |
| 12 | +} from '@mantine/core'; |
| 13 | +import { useLocalStorage } from '@mantine/hooks'; |
| 14 | +import { |
| 15 | + IconThumbDown, |
| 16 | + IconThumbDownFilled, |
| 17 | + IconThumbUp, |
| 18 | + IconThumbUpFilled, |
| 19 | +} from '@tabler/icons-react'; |
| 20 | + |
| 21 | +import { IS_LOCAL_MODE } from '@/config'; |
| 22 | + |
| 23 | +import { AppNavContext } from './AppNav.components'; |
| 24 | + |
| 25 | +import styles from './AppNav.module.scss'; |
| 26 | + |
| 27 | +type FeedbackVote = 'up' | 'down' | null; |
| 28 | + |
| 29 | +type FeedbackState = 'idle' | 'voted' | 'thanks'; |
| 30 | + |
| 31 | +const FORCE_ENABLE_KEY = 'hdx-feedback-enabled'; |
| 32 | + |
| 33 | +class FeedbackErrorBoundary extends React.Component< |
| 34 | + { children: React.ReactNode }, |
| 35 | + { hasError: boolean } |
| 36 | +> { |
| 37 | + state = { hasError: false }; |
| 38 | + |
| 39 | + static getDerivedStateFromError() { |
| 40 | + return { hasError: true }; |
| 41 | + } |
| 42 | + |
| 43 | + render() { |
| 44 | + if (this.state.hasError) return null; |
| 45 | + return this.props.children; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export const AppNavFeedback = () => ( |
| 50 | + <FeedbackErrorBoundary> |
| 51 | + <AppNavFeedbackInner /> |
| 52 | + </FeedbackErrorBoundary> |
| 53 | +); |
| 54 | + |
| 55 | +const AppNavFeedbackInner = () => { |
| 56 | + const { isCollapsed } = React.useContext(AppNavContext); |
| 57 | + const [forceEnabled] = useLocalStorage<boolean>({ |
| 58 | + key: FORCE_ENABLE_KEY, |
| 59 | + defaultValue: false, |
| 60 | + }); |
| 61 | + const [hidden, setHidden] = useLocalStorage<boolean>({ |
| 62 | + key: 'feedbackHidden', |
| 63 | + defaultValue: false, |
| 64 | + }); |
| 65 | + |
| 66 | + const [vote, setVote] = useState<FeedbackVote>(null); |
| 67 | + const [comment, setComment] = useState(''); |
| 68 | + const [state, setState] = useState<FeedbackState>('idle'); |
| 69 | + const router = useRouter(); |
| 70 | + |
| 71 | + // Only show when HyperDX SDK is active (non-local mode), |
| 72 | + // or when overridden via: localStorage.setItem('hdx-feedback-enabled', 'true') |
| 73 | + const sdkEnabled = !IS_LOCAL_MODE || forceEnabled === true; |
| 74 | + |
| 75 | + const reset = useCallback(() => { |
| 76 | + setVote(null); |
| 77 | + setComment(''); |
| 78 | + setState('idle'); |
| 79 | + }, []); |
| 80 | + |
| 81 | + const pageContext = useCallback( |
| 82 | + () => ({ |
| 83 | + page: router.pathname, |
| 84 | + route: router.asPath, |
| 85 | + }), |
| 86 | + [router], |
| 87 | + ); |
| 88 | + |
| 89 | + const handleVote = useCallback( |
| 90 | + (newVote: FeedbackVote) => { |
| 91 | + setVote(newVote); |
| 92 | + setState('voted'); |
| 93 | + HyperDX.addAction('user feedback vote', { |
| 94 | + vote: newVote ?? '', |
| 95 | + ...pageContext(), |
| 96 | + }); |
| 97 | + }, |
| 98 | + [setVote, setState, pageContext], |
| 99 | + ); |
| 100 | + |
| 101 | + const [dismissed, setDismissed] = useState(false); |
| 102 | + |
| 103 | + const handleSubmit = useCallback(() => { |
| 104 | + HyperDX.addAction('user feedback comment', { |
| 105 | + vote: vote ?? '', |
| 106 | + comment, |
| 107 | + ...pageContext(), |
| 108 | + }); |
| 109 | + |
| 110 | + setState('thanks'); |
| 111 | + setTimeout(() => { |
| 112 | + reset(); |
| 113 | + setDismissed(true); |
| 114 | + }, 1500); |
| 115 | + }, [vote, comment, pageContext, reset]); |
| 116 | + |
| 117 | + if (!sdkEnabled || hidden || dismissed) return null; |
| 118 | + |
| 119 | + if (isCollapsed) { |
| 120 | + return ( |
| 121 | + <Tooltip label="Feedback" position="right"> |
| 122 | + <Group |
| 123 | + data-testid="feedback-inline" |
| 124 | + gap={0} |
| 125 | + justify="center" |
| 126 | + py={4} |
| 127 | + wrap="nowrap" |
| 128 | + > |
| 129 | + <ActionIcon |
| 130 | + data-testid="feedback-thumbs-up" |
| 131 | + variant="subtle" |
| 132 | + size="sm" |
| 133 | + onClick={() => handleVote('up')} |
| 134 | + title="Thumbs up" |
| 135 | + > |
| 136 | + <IconThumbUp size={14} /> |
| 137 | + </ActionIcon> |
| 138 | + <ActionIcon |
| 139 | + data-testid="feedback-thumbs-down" |
| 140 | + variant="subtle" |
| 141 | + size="sm" |
| 142 | + onClick={() => handleVote('down')} |
| 143 | + title="Thumbs down" |
| 144 | + > |
| 145 | + <IconThumbDown size={14} /> |
| 146 | + </ActionIcon> |
| 147 | + </Group> |
| 148 | + </Tooltip> |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + return ( |
| 153 | + <Box data-testid="feedback-inline"> |
| 154 | + {state === 'thanks' ? ( |
| 155 | + <Text |
| 156 | + size="xs" |
| 157 | + c="dimmed" |
| 158 | + data-testid="feedback-thanks" |
| 159 | + className={styles.feedbackLabel} |
| 160 | + px="lg" |
| 161 | + py={4} |
| 162 | + > |
| 163 | + Thanks for your feedback! |
| 164 | + </Text> |
| 165 | + ) : ( |
| 166 | + <> |
| 167 | + <Group |
| 168 | + gap={6} |
| 169 | + wrap="nowrap" |
| 170 | + align="center" |
| 171 | + className={styles.navItem} |
| 172 | + > |
| 173 | + <span className={styles.navItemContent}> |
| 174 | + <span className={styles.navItemIcon}> |
| 175 | + <ActionIcon |
| 176 | + data-testid="feedback-thumbs-up" |
| 177 | + variant={vote === 'up' ? 'secondary' : 'subtle'} |
| 178 | + size="xs" |
| 179 | + onClick={() => handleVote('up')} |
| 180 | + title="Thumbs up" |
| 181 | + > |
| 182 | + {vote === 'up' ? ( |
| 183 | + <IconThumbUpFilled size={14} /> |
| 184 | + ) : ( |
| 185 | + <IconThumbUp size={14} /> |
| 186 | + )} |
| 187 | + </ActionIcon> |
| 188 | + </span> |
| 189 | + <ActionIcon |
| 190 | + data-testid="feedback-thumbs-down" |
| 191 | + variant={vote === 'down' ? 'secondary' : 'subtle'} |
| 192 | + size="xs" |
| 193 | + onClick={() => handleVote('down')} |
| 194 | + title="Thumbs down" |
| 195 | + mr={4} |
| 196 | + > |
| 197 | + {vote === 'down' ? ( |
| 198 | + <IconThumbDownFilled size={14} /> |
| 199 | + ) : ( |
| 200 | + <IconThumbDown size={14} /> |
| 201 | + )} |
| 202 | + </ActionIcon> |
| 203 | + <Text size="xs" c="dimmed" className={styles.feedbackLabel}> |
| 204 | + Feedback? |
| 205 | + </Text> |
| 206 | + </span> |
| 207 | + <Text |
| 208 | + data-testid="feedback-hide" |
| 209 | + size="xs" |
| 210 | + c="dimmed" |
| 211 | + className={styles.feedbackHide} |
| 212 | + onClick={() => setHidden(true)} |
| 213 | + role="button" |
| 214 | + tabIndex={0} |
| 215 | + > |
| 216 | + Hide |
| 217 | + </Text> |
| 218 | + </Group> |
| 219 | + {state === 'voted' && ( |
| 220 | + <Box px="lg" pt={4} pb={2}> |
| 221 | + <Textarea |
| 222 | + data-testid="feedback-comment" |
| 223 | + placeholder="Tell us more (optional)" |
| 224 | + value={comment} |
| 225 | + onChange={e => setComment(e.currentTarget.value)} |
| 226 | + minRows={2} |
| 227 | + maxRows={4} |
| 228 | + autosize |
| 229 | + autoFocus |
| 230 | + size="xs" |
| 231 | + /> |
| 232 | + <Button |
| 233 | + data-testid="feedback-submit" |
| 234 | + variant="primary" |
| 235 | + size="compact-xs" |
| 236 | + fullWidth |
| 237 | + mt={6} |
| 238 | + onClick={handleSubmit} |
| 239 | + > |
| 240 | + Submit |
| 241 | + </Button> |
| 242 | + </Box> |
| 243 | + )} |
| 244 | + </> |
| 245 | + )} |
| 246 | + </Box> |
| 247 | + ); |
| 248 | +}; |
0 commit comments