All files / src/components/feedback FeedbackButton.tsx

94.87% Statements 37/39
94.44% Branches 17/18
92.3% Functions 12/13
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                    19x 19x 19x   19x 4x 4x   4x 4x 4x               3x 3x 3x 3x   1x   4x       19x 5x                                                     1x                               14x                                   1x                 3x                                   85x 85x   85x 36x 36x 36x       85x 10x 3x       75x       3x           6x                    
import { useState, useEffect } from "react";
import { ArrowLeft, MessageSquare, X } from "lucide-react";
import { proApi } from "../../lib/api";
import { notify } from "../../lib/notify";
import { useIsMobile } from "../../hooks/useIsMobile";
 
function FeedbackForm({
	onClose,
	fullScreen,
}: { onClose: () => void; fullScreen?: boolean }) {
	const [category, setCategory] = useState("general");
	const [message, setMessage] = useState("");
	const [isSubmitting, setIsSubmitting] = useState(false);
 
	const handleSubmit = async (e: React.FormEvent) => {
		e.preventDefault();
		Iif (!message.trim()) return;
 
		setIsSubmitting(true);
		try {
			await proApi.submitFeedback({
				category,
				message: message.trim(),
				pageUrl: window.location.href,
				browser: navigator.userAgent,
				screenResolution: `${window.screen.width}x${window.screen.height}`,
				location: Intl.DateTimeFormat().resolvedOptions().timeZone,
			});
			notify.success("Thank you for your feedback!");
			setMessage("");
			setCategory("general");
			onClose();
		} catch {
			notify.error("Failed to submit feedback. Please try again.");
		} finally {
			setIsSubmitting(false);
		}
	};
 
	if (fullScreen) {
		return (
			<div className="fixed inset-0 z-[60] flex flex-col bg-background-base animate-page-in">
				<div className="flex h-14 items-center gap-3 border-b border-border-default bg-background-elevated px-4 flex-shrink-0">
					<button
						type="button"
						onClick={onClose}
						className="p-2 -ml-2 text-foreground-muted hover:text-foreground-default rounded-md"
						aria-label="Go back"
					>
						<ArrowLeft className="h-5 w-5" />
					</button>
					<h2 className="text-lg font-semibold text-foreground-default">
						Send Feedback
					</h2>
				</div>
				<form onSubmit={handleSubmit} className="flex-1 flex flex-col p-4">
					<select
						value={category}
						onChange={(e) => setCategory(e.target.value)}
						className="w-full px-3 py-2 border border-border-default rounded-md bg-background-base text-foreground-default text-sm mb-3"
					>
						<option value="general">General Feedback</option>
						<option value="bug">Bug Report</option>
						<option value="feature_request">Feature Request</option>
					</select>
					<textarea
						value={message}
						onChange={(e) => setMessage(e.target.value)}
						placeholder="What's on your mind?"
						className="w-full flex-1 px-3 py-2 border border-border-default rounded-md bg-background-base text-foreground-default text-sm resize-none min-h-32"
					/>
					<button
						type="submit"
						disabled={isSubmitting || !message.trim()}
						className="w-full mt-4 px-4 py-3 bg-primary-500 text-white rounded-md font-semibold text-sm hover:bg-primary-600 disabled:opacity-50 disabled:cursor-not-allowed"
					>
						{isSubmitting ? "Submitting..." : "Submit Feedback"}
					</button>
				</form>
			</div>
		);
	}
 
	return (
		<div className="absolute bottom-full right-0 mb-3 w-80 bg-background-elevated border border-border-default rounded-xl p-5 shadow-xl">
			<div className="flex items-center justify-between mb-3">
				<h3 className="text-base font-bold text-foreground-default">
					Send Feedback
				</h3>
				<button
					type="button"
					onClick={onClose}
					className="text-foreground-muted hover:text-foreground-default"
					aria-label="Close feedback form"
				>
					<X size={18} />
				</button>
			</div>
			<form onSubmit={handleSubmit}>
				<select
					value={category}
					onChange={(e) => setCategory(e.target.value)}
					className="w-full px-3 py-2 border border-border-default rounded-md bg-background-base text-foreground-default text-sm mb-2.5"
				>
					<option value="general">General Feedback</option>
					<option value="bug">Bug Report</option>
					<option value="feature_request">Feature Request</option>
				</select>
				<textarea
					value={message}
					onChange={(e) => setMessage(e.target.value)}
					placeholder="What's on your mind?"
					className="w-full px-3 py-2 border border-border-default rounded-md bg-background-base text-foreground-default text-sm mb-2.5 resize-y min-h-20"
					rows={3}
				/>
				<button
					type="submit"
					disabled={isSubmitting || !message.trim()}
					className="w-full px-4 py-2 bg-primary-500 text-white rounded-md font-semibold text-sm hover:bg-primary-600 disabled:opacity-50 disabled:cursor-not-allowed"
				>
					{isSubmitting ? "Submitting..." : "Submit Feedback"}
				</button>
			</form>
		</div>
	);
}
 
export function FeedbackButton() {
	const [isFormOpen, setIsFormOpen] = useState(false);
	const isMobile = useIsMobile();
 
	useEffect(() => {
		const handler = () => setIsFormOpen(true);
		window.addEventListener("open-feedback", handler);
		return () => window.removeEventListener("open-feedback", handler);
	}, []);
 
	// Mobile: only render when form is open (triggered via More sheet event)
	if (isMobile) {
		if (!isFormOpen) return null;
		return <FeedbackForm onClose={() => setIsFormOpen(false)} fullScreen />;
	}
 
	// Desktop: compact icon button + popover
	return (
		<div className="fixed bottom-4 right-4 z-40">
			{/* Feedback form popover */}
			{isFormOpen && (
				<FeedbackForm onClose={() => setIsFormOpen(false)} />
			)}
 
			{/* Compact icon button */}
			<button
				type="button"
				onClick={() => setIsFormOpen(!isFormOpen)}
				aria-label="Send Feedback"
				title="Send Feedback"
				className="flex h-8 w-8 items-center justify-center rounded-full border border-border-default bg-background-elevated text-foreground-subtle shadow-sm transition-colors hover:bg-background-muted hover:text-foreground-muted opacity-60 hover:opacity-100"
			>
				<MessageSquare size={14} />
			</button>
		</div>
	);
}