"use client";
import { useState } from "react";
import { Star, ExternalLink } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
type Step = "rating" | "positive" | "negative" | "thanks";
const ratingLabels = [
"",
"Very Poor",
"Poor",
"Okay",
"Good",
"Excellent",
];
export const title = "React Dialog Block App Rating";
export default function DialogAppRating() {
const [open, setOpen] = useState(false);
const [step, setStep] = useState<Step>("rating");
const [rating, setRating] = useState(0);
const [hoveredRating, setHoveredRating] = useState(0);
const [feedback, setFeedback] = useState("");
const handleRatingSelect = (selectedRating: number) => {
setRating(selectedRating);
if (selectedRating >= 4) {
setStep("positive");
} else {
setStep("negative");
}
};
const handleStoreRedirect = () => {
console.log("Redirecting to app store with rating:", rating);
setStep("thanks");
};
const handleFeedbackSubmit = () => {
console.log("Feedback submitted:", { rating, feedback });
setStep("thanks");
};
const handleClose = () => {
setOpen(false);
setTimeout(() => {
setStep("rating");
setRating(0);
setHoveredRating(0);
setFeedback("");
}, 200);
};
const handleRemindLater = () => {
console.log("User chose remind later");
handleClose();
};
const displayRating = hoveredRating || rating;
return (
<Dialog open={open} onOpenChange={setOpen}>
<div className="flex min-h-[350px] items-center justify-center">
<DialogTrigger asChild>
<Button variant="outline">
<Star className="mr-2 h-4 w-4" />
Rate Our App
</Button>
</DialogTrigger>
</div>
<DialogContent className="sm:max-w-sm">
{step === "rating" && (
<>
<DialogHeader className="text-center">
<DialogTitle>Enjoying the app?</DialogTitle>
<DialogDescription>
Your feedback helps us improve. How would you rate your experience?
</DialogDescription>
</DialogHeader>
<div className="py-6">
<div className="flex justify-center gap-1">
{[1, 2, 3, 4, 5].map((star) => (
<button
key={star}
onClick={() => handleRatingSelect(star)}
onMouseEnter={() => setHoveredRating(star)}
onMouseLeave={() => setHoveredRating(0)}
className="p-1 transition-transform hover:scale-110"
>
<Star
className={cn(
"h-10 w-10 transition-colors",
star <= displayRating
? "fill-primary text-primary"
: "text-muted-foreground/30"
)}
/>
</button>
))}
</div>
<p className="text-center text-sm text-muted-foreground mt-3 h-5">
{displayRating > 0 ? ratingLabels[displayRating] : "Tap a star to rate"}
</p>
</div>
<DialogFooter className="flex-col gap-2 sm:flex-col">
<Button variant="ghost" onClick={handleRemindLater} className="w-full">
Maybe Later
</Button>
</DialogFooter>
</>
)}
{step === "positive" && (
<>
<DialogHeader className="text-center">
<DialogTitle>We're glad you love it!</DialogTitle>
<DialogDescription>
Would you mind leaving us a review? It really helps other people discover our app.
</DialogDescription>
</DialogHeader>
<div className="flex justify-center py-4">
<div className="flex gap-0.5">
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
className={cn(
"h-6 w-6",
star <= rating
? "fill-primary text-primary"
: "text-muted-foreground/30"
)}
/>
))}
</div>
</div>
<DialogFooter className="flex-col gap-2 sm:flex-col">
<Button onClick={handleStoreRedirect} className="w-full">
<ExternalLink className="mr-2 h-4 w-4" />
Leave a Review
</Button>
<Button variant="ghost" onClick={handleClose} className="w-full">
No Thanks
</Button>
</DialogFooter>
</>
)}
{step === "negative" && (
<>
<DialogHeader className="text-center">
<DialogTitle>We'd love your feedback</DialogTitle>
<DialogDescription>
We're sorry to hear that. What can we do better?
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-3">
<div className="flex justify-center">
<div className="flex gap-0.5">
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
className={cn(
"h-5 w-5",
star <= rating
? "fill-primary text-primary"
: "text-muted-foreground/30"
)}
/>
))}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="feedback">Your feedback</Label>
<Textarea
id="feedback"
placeholder="Tell us what we can improve..."
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
className="h-24 resize-none"
/>
</div>
</div>
<DialogFooter className="flex-col gap-2 sm:flex-col">
<Button
onClick={handleFeedbackSubmit}
disabled={!feedback.trim()}
className="w-full"
>
Submit Feedback
</Button>
<Button variant="ghost" onClick={handleClose} className="w-full">
Skip
</Button>
</DialogFooter>
</>
)}
{step === "thanks" && (
<>
<DialogHeader className="text-center">
<DialogTitle>Thank you!</DialogTitle>
<DialogDescription>
{rating >= 4
? "Your support means the world to us. Thank you for helping others discover our app!"
: "Your feedback helps us build a better product. We'll work hard to improve your experience."}
</DialogDescription>
</DialogHeader>
<div className="flex justify-center py-4">
<div className="flex gap-0.5">
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
className={cn(
"h-6 w-6",
star <= rating
? "fill-primary text-primary"
: "text-muted-foreground/30"
)}
/>
))}
</div>
</div>
<DialogFooter>
<Button onClick={handleClose} className="w-full">
Done
</Button>
</DialogFooter>
</>
)}
</DialogContent>
</Dialog>
);
}