BugReportBubble.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { useState, useRef, useCallback, useEffect } from 'react';
  2. import { Bug, X, Loader2, CheckCircle, AlertCircle, Trash2, Upload } from 'lucide-react';
  3. import { useTranslation } from 'react-i18next';
  4. import { bugReportApi } from '../api/client';
  5. type ViewState = 'form' | 'collecting' | 'submitting' | 'success' | 'error';
  6. const LOG_COLLECTION_SECONDS = 30;
  7. const MAX_DIMENSION = 1920;
  8. const JPEG_QUALITY = 0.7;
  9. function compressImage(file: File): Promise<string> {
  10. return new Promise((resolve, reject) => {
  11. const img = new Image();
  12. img.onload = () => {
  13. let { width, height } = img;
  14. if (width > MAX_DIMENSION || height > MAX_DIMENSION) {
  15. const scale = MAX_DIMENSION / Math.max(width, height);
  16. width = Math.round(width * scale);
  17. height = Math.round(height * scale);
  18. }
  19. const canvas = document.createElement('canvas');
  20. canvas.width = width;
  21. canvas.height = height;
  22. const ctx = canvas.getContext('2d');
  23. if (!ctx) { reject(new Error('No canvas context')); return; }
  24. ctx.drawImage(img, 0, 0, width, height);
  25. const dataUrl = canvas.toDataURL('image/jpeg', JPEG_QUALITY);
  26. resolve(dataUrl.replace(/^data:[^;]+;base64,/, ''));
  27. };
  28. img.onerror = reject;
  29. img.src = URL.createObjectURL(file);
  30. });
  31. }
  32. export function BugReportBubble() {
  33. const { t } = useTranslation();
  34. const [isOpen, setIsOpen] = useState(false);
  35. const [viewState, setViewState] = useState<ViewState>('form');
  36. const [description, setDescription] = useState('');
  37. const [email, setEmail] = useState('');
  38. const [screenshot, setScreenshot] = useState<string | null>(null);
  39. const [isDragging, setIsDragging] = useState(false);
  40. const [issueUrl, setIssueUrl] = useState<string | null>(null);
  41. const [issueNumber, setIssueNumber] = useState<number | null>(null);
  42. const [errorMessage, setErrorMessage] = useState('');
  43. const [countdown, setCountdown] = useState(0);
  44. const modalRef = useRef<HTMLDivElement>(null);
  45. const fileInputRef = useRef<HTMLInputElement>(null);
  46. // Countdown timer for log collection phase
  47. useEffect(() => {
  48. if (viewState !== 'collecting') return;
  49. if (countdown <= 0) {
  50. setViewState('submitting');
  51. return;
  52. }
  53. const timer = setTimeout(() => setCountdown((c) => c - 1), 1000);
  54. return () => clearTimeout(timer);
  55. }, [viewState, countdown]);
  56. const handleOpen = () => {
  57. setIsOpen(true);
  58. setViewState('form');
  59. setDescription('');
  60. setEmail('');
  61. setScreenshot(null);
  62. setIssueUrl(null);
  63. setIssueNumber(null);
  64. setErrorMessage('');
  65. };
  66. const handleClose = () => {
  67. setIsOpen(false);
  68. };
  69. const handleFile = useCallback(async (file: File) => {
  70. if (!file.type.startsWith('image/')) return;
  71. try {
  72. const b64 = await compressImage(file);
  73. setScreenshot(b64);
  74. } catch {
  75. // Ignore read errors
  76. }
  77. }, []);
  78. const handlePaste = useCallback((e: React.ClipboardEvent) => {
  79. const items = e.clipboardData?.items;
  80. if (!items) return;
  81. for (const item of items) {
  82. if (item.type.startsWith('image/')) {
  83. const file = item.getAsFile();
  84. if (file) handleFile(file);
  85. break;
  86. }
  87. }
  88. }, [handleFile]);
  89. const handleDragOver = useCallback((e: React.DragEvent) => {
  90. e.preventDefault();
  91. setIsDragging(true);
  92. }, []);
  93. const handleDragLeave = useCallback((e: React.DragEvent) => {
  94. e.preventDefault();
  95. setIsDragging(false);
  96. }, []);
  97. const handleDrop = useCallback((e: React.DragEvent) => {
  98. e.preventDefault();
  99. setIsDragging(false);
  100. const file = e.dataTransfer.files?.[0];
  101. if (file) handleFile(file);
  102. }, [handleFile]);
  103. const handleSubmit = async () => {
  104. if (!description.trim()) return;
  105. setCountdown(LOG_COLLECTION_SECONDS);
  106. setViewState('collecting');
  107. try {
  108. const result = await bugReportApi.submit({
  109. description: description.trim(),
  110. email: email.trim() || undefined,
  111. screenshot_base64: screenshot || undefined,
  112. include_support_info: true,
  113. });
  114. if (result.success) {
  115. setIssueUrl(result.issue_url || null);
  116. setIssueNumber(result.issue_number || null);
  117. setViewState('success');
  118. } else {
  119. setErrorMessage(result.message);
  120. setViewState('error');
  121. }
  122. } catch (err) {
  123. setErrorMessage(err instanceof Error ? err.message : t('bugReport.unexpectedError'));
  124. setViewState('error');
  125. }
  126. };
  127. return (
  128. <>
  129. {/* Floating bubble */}
  130. <button
  131. onClick={handleOpen}
  132. className="fixed bottom-4 right-4 z-40 w-12 h-12 rounded-full bg-red-500 hover:bg-red-600 text-white shadow-lg hover:shadow-xl transition-all duration-200 hover:scale-110 flex items-center justify-center"
  133. title={t('bugReport.title')}
  134. >
  135. <Bug className="w-5 h-5" />
  136. </button>
  137. {/* Slide-in panel anchored to bottom-right */}
  138. {isOpen && (
  139. <div
  140. id="bug-report-modal"
  141. className="fixed bottom-20 right-4 z-50 w-full max-w-md"
  142. onPaste={handlePaste}
  143. >
  144. <div
  145. ref={modalRef}
  146. className="bg-white dark:bg-gray-800 rounded-lg shadow-2xl border border-gray-200 dark:border-gray-700 max-h-[80vh] overflow-y-auto"
  147. >
  148. {/* Header */}
  149. <div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700 sticky top-0 bg-white dark:bg-gray-800 z-10">
  150. <h2 className="text-lg font-semibold text-gray-900 dark:text-white flex items-center gap-2">
  151. <Bug className="w-5 h-5 text-red-500" />
  152. {t('bugReport.title')}
  153. </h2>
  154. <button
  155. onClick={handleClose}
  156. className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
  157. >
  158. <X className="w-5 h-5" />
  159. </button>
  160. </div>
  161. <div className="p-4 space-y-4">
  162. {viewState === 'form' && (
  163. <>
  164. {/* Description */}
  165. <div>
  166. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  167. {t('bugReport.description')} *
  168. </label>
  169. <textarea
  170. value={description}
  171. onChange={(e) => setDescription(e.target.value)}
  172. placeholder={t('bugReport.descriptionPlaceholder')}
  173. rows={3}
  174. className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-vertical"
  175. />
  176. </div>
  177. {/* Email (optional) */}
  178. <div>
  179. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  180. {t('bugReport.email')}
  181. </label>
  182. <input
  183. type="email"
  184. value={email}
  185. onChange={(e) => setEmail(e.target.value)}
  186. placeholder={t('bugReport.emailPlaceholder')}
  187. className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
  188. />
  189. <p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
  190. {t('bugReport.emailPrivacy')}
  191. </p>
  192. </div>
  193. {/* Screenshot — upload, paste, or drag */}
  194. <div>
  195. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  196. {t('bugReport.screenshot')}
  197. </label>
  198. {screenshot ? (
  199. <div className="relative">
  200. <img
  201. src={`data:image/jpeg;base64,${screenshot}`}
  202. alt={t('bugReport.screenshot')}
  203. className="w-full max-h-40 object-contain rounded-lg border border-gray-200 dark:border-gray-600"
  204. />
  205. <button
  206. onClick={() => setScreenshot(null)}
  207. className="absolute top-2 right-2 p-1 bg-red-500 hover:bg-red-600 text-white rounded-full shadow"
  208. title={t('common.delete')}
  209. >
  210. <Trash2 className="w-3 h-3" />
  211. </button>
  212. </div>
  213. ) : (
  214. <button
  215. type="button"
  216. onClick={() => fileInputRef.current?.click()}
  217. onDragOver={handleDragOver}
  218. onDragLeave={handleDragLeave}
  219. onDrop={handleDrop}
  220. className={`w-full flex flex-col items-center gap-2 px-4 py-4 border-2 border-dashed rounded-lg transition-colors cursor-pointer ${
  221. isDragging
  222. ? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20 text-blue-500'
  223. : 'border-gray-300 dark:border-gray-600 text-gray-500 dark:text-gray-400 hover:border-gray-400 dark:hover:border-gray-500 hover:text-gray-600 dark:hover:text-gray-300'
  224. }`}
  225. >
  226. <Upload className="w-5 h-5" />
  227. <span className="text-sm">{t('bugReport.uploadOrPaste')}</span>
  228. </button>
  229. )}
  230. <input
  231. ref={fileInputRef}
  232. type="file"
  233. accept="image/*"
  234. className="hidden"
  235. onChange={(e) => {
  236. const file = e.target.files?.[0];
  237. if (file) handleFile(file);
  238. e.target.value = '';
  239. }}
  240. />
  241. </div>
  242. {/* Data collection notice */}
  243. <details className="text-xs bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg p-3">
  244. <summary className="cursor-pointer font-medium text-amber-700 dark:text-amber-300 hover:text-amber-800 dark:hover:text-amber-200">
  245. {t('bugReport.dataCollectedSummary')}
  246. </summary>
  247. <div className="mt-2 space-y-2 pl-2 border-l-2 border-amber-300 dark:border-amber-700 text-amber-800 dark:text-amber-200">
  248. <p className="font-medium">{t('bugReport.dataIncluded')}</p>
  249. <p>{t('bugReport.dataIncludedList')}</p>
  250. <p className="font-medium">{t('bugReport.dataNeverIncluded')}</p>
  251. <p>{t('bugReport.dataNeverIncludedList')}</p>
  252. </div>
  253. </details>
  254. {/* Buttons */}
  255. <div className="flex justify-end gap-2 pt-2">
  256. <button
  257. onClick={handleClose}
  258. className="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-lg transition-colors"
  259. >
  260. {t('common.cancel')}
  261. </button>
  262. <button
  263. onClick={handleSubmit}
  264. disabled={!description.trim()}
  265. className="px-4 py-2 text-sm font-medium text-white bg-red-500 hover:bg-red-600 disabled:opacity-50 disabled:cursor-not-allowed rounded-lg transition-colors"
  266. >
  267. {t('bugReport.submit')}
  268. </button>
  269. </div>
  270. </>
  271. )}
  272. {(viewState === 'collecting' || viewState === 'submitting') && (
  273. <div className="flex flex-col items-center justify-center py-8 gap-3">
  274. <Loader2 className="w-8 h-8 animate-spin text-blue-500" />
  275. {viewState === 'collecting' ? (
  276. <>
  277. <p className="text-sm font-medium text-gray-900 dark:text-white">{t('bugReport.collectingLogs')}</p>
  278. <p className="text-xs text-gray-500 dark:text-gray-400">{t('bugReport.collectingLogsHint')}</p>
  279. {countdown > 0 && (
  280. <p className="text-lg font-mono text-blue-500">{t('bugReport.countdownSeconds', { seconds: countdown })}</p>
  281. )}
  282. </>
  283. ) : (
  284. <p className="text-sm text-gray-600 dark:text-gray-400">{t('bugReport.submitting')}</p>
  285. )}
  286. </div>
  287. )}
  288. {viewState === 'success' && (
  289. <div className="flex flex-col items-center justify-center py-8 gap-3">
  290. <CheckCircle className="w-12 h-12 text-green-500" />
  291. <p className="text-lg font-semibold text-gray-900 dark:text-white">{t('bugReport.thankYou')}</p>
  292. <p className="text-sm text-gray-600 dark:text-gray-400">{t('bugReport.submitted')}</p>
  293. {issueUrl && (
  294. <a
  295. href={issueUrl}
  296. target="_blank"
  297. rel="noopener noreferrer"
  298. className="text-sm text-blue-500 hover:text-blue-600 underline"
  299. >
  300. {t('bugReport.viewIssue')} #{issueNumber}
  301. </a>
  302. )}
  303. <button
  304. onClick={handleClose}
  305. className="mt-4 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-lg transition-colors"
  306. >
  307. {t('common.close')}
  308. </button>
  309. </div>
  310. )}
  311. {viewState === 'error' && (
  312. <div className="flex flex-col items-center justify-center py-8 gap-3">
  313. <AlertCircle className="w-12 h-12 text-red-500" />
  314. <p className="text-lg font-semibold text-gray-900 dark:text-white">{t('bugReport.submitFailed')}</p>
  315. <p className="text-sm text-gray-600 dark:text-gray-400 text-center">{errorMessage}</p>
  316. <div className="flex gap-2 mt-4">
  317. <button
  318. onClick={() => setViewState('form')}
  319. className="px-4 py-2 text-sm font-medium text-white bg-red-500 hover:bg-red-600 rounded-lg transition-colors"
  320. >
  321. {t('bugReport.submit')}
  322. </button>
  323. <button
  324. onClick={handleClose}
  325. className="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-lg transition-colors"
  326. >
  327. {t('common.close')}
  328. </button>
  329. </div>
  330. </div>
  331. )}
  332. </div>
  333. </div>
  334. </div>
  335. )}
  336. </>
  337. );
  338. }