Skip to content

Commit 68f32e7

Browse files
committed
fix: better subscription handling and fixed toast
1 parent d6214b8 commit 68f32e7

File tree

1 file changed

+32
-78
lines changed

1 file changed

+32
-78
lines changed

src/components/ui/ai-chat.tsx

Lines changed: 32 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
ChevronRight,
2020
} from "lucide-react";
2121
import { useRouter } from "next/navigation";
22-
import { toast } from "@/components/ui/use-toast";
2322
import { AnalysisMode } from "@/types/search";
2423
import {
2524
Command,
@@ -408,11 +407,9 @@ export function AIChatInterface({
408407
});
409408

410409
if (!updatedSubscription.ok) {
411-
toast({
412-
title: "Error",
413-
description: "Failed to update query count. Please try again later.",
414-
variant: "destructive",
415-
});
410+
toastSonner.error(
411+
"Failed to update query count. Please try again later."
412+
);
416413
}
417414
};
418415

@@ -439,86 +436,48 @@ export function AIChatInterface({
439436

440437
const handleSubmit = async () => {
441438
if (!value.trim()) {
442-
toast({
443-
title: "Error",
444-
description: `Please enter a query or keyword to ${
439+
toastSonner.error(
440+
`Please enter a query or keyword to ${
445441
isMonitoringMode ? "monitor" : "search"
446-
}.`,
447-
variant: "destructive",
448-
});
442+
}.`
443+
);
449444
return;
450445
}
451446

452447
if (!user?.id) {
453-
toast({
454-
title: "Error",
455-
description: "Please sign in to continue",
456-
variant: "destructive",
457-
});
448+
toastSonner.error("Please sign in to continue");
458449
return;
459450
}
460451

461452
if (!subscription) {
462-
toast({
463-
title: "Error",
464-
description:
465-
"Subscription plan could not be found. Please contact support or try again later.",
466-
variant: "destructive",
467-
});
468-
return;
469-
}
470-
471-
if (subscription.status !== "active") {
472-
toast({
473-
title: "Error",
474-
description:
475-
"Your subscription is not active or has expired. Please upgrade to continue.",
476-
variant: "destructive",
477-
});
478-
return;
479-
}
480-
481-
if (!product) {
482-
toast({
483-
title: "Error",
484-
description:
485-
"Subscription plan could not be found. Please contact support or try again later.",
486-
variant: "destructive",
487-
});
453+
toastSonner.error(
454+
"Subscription could not be found. Please contact support or try again later."
455+
);
488456
return;
489457
}
490458

491459
if (selectedModels.length === 0) {
492-
toast({
493-
title: "Error",
494-
description: "Please select at least one AI model for analysis.",
495-
variant: "destructive",
496-
});
460+
toastSonner.error("Please select at least one AI model for analysis.");
497461
return;
498462
}
499463

500-
const userConstraint = getConstraints(product.name);
464+
const userConstraint = getConstraints(product?.name || "");
501465

502-
if (subscription.query_count >= userConstraint.max_credits) {
503-
toast({
504-
title: "Error",
505-
description:
506-
"You have reached the maximum number of credits for your plan. Please upgrade to continue.",
507-
variant: "destructive",
508-
});
466+
if (subscription?.query_count >= userConstraint.max_credits && subscription?.payg_credits === 0) {
467+
toastSonner.error(
468+
"You have reached the maximum number of credits for your plan. Please upgrade to continue."
469+
);
509470
return;
510471
}
511472

512473
if (
513474
isMonitoringMode &&
514-
subscription.monitoring_count >= userConstraint.max_scheduled_queries
475+
subscription?.monitoring_count >= userConstraint.max_scheduled_queries &&
476+
subscription?.payg_credits === 0
515477
) {
516-
toast({
517-
title: "Error",
518-
description:
519-
"You have reached the maximum number of scheduled queries for your plan. Please upgrade to continue.",
520-
variant: "destructive",
521-
});
478+
toastSonner.error(
479+
"You have reached the maximum number of scheduled queries for your plan. Please upgrade to continue."
480+
);
522481
return;
523482
}
524483

@@ -568,10 +527,9 @@ export function AIChatInterface({
568527
throw new Error(error.error || "Failed to schedule query");
569528
}
570529

571-
toast({
572-
title: "Query Scheduled",
573-
description: `Your query has been scheduled for ${monitorFrequency} monitoring with ${selectedModels.length} AI models (${creditsRequired} credits per run). Check the Monitoring tab for details.`,
574-
});
530+
toastSonner.success(
531+
`Your query has been scheduled for ${monitorFrequency} monitoring with ${selectedModels.length} AI models (${creditsRequired} credits per run). Check the Monitoring tab for details.`
532+
);
575533
updateQueryCount();
576534
setTimeout(() => {
577535
window.location.assign(`/dashboard/library`);
@@ -629,10 +587,9 @@ export function AIChatInterface({
629587

630588
const { mode_id, credits_used } = JSON.parse(result);
631589

632-
toast({
633-
title: "Analysis started",
634-
description: `Your ${mode} analysis is processing using ${selectedModels.length} AI models (${credits_used} credits used). You'll be redirected to results when complete.`,
635-
});
590+
toastSonner.success(
591+
`Your ${mode} analysis is processing using ${selectedModels.length} AI models (${credits_used} credits used). You'll be redirected to results when complete.`
592+
);
636593

637594
setIsAnalyzing(false);
638595
setTimeout(() => {
@@ -645,12 +602,9 @@ export function AIChatInterface({
645602
} catch (error) {
646603
console.error("Error submitting request:", error);
647604
setIsAnalyzing(false);
648-
toast({
649-
title: "Error",
650-
description:
651-
error instanceof Error ? error.message : "An unknown error occurred",
652-
variant: "destructive",
653-
});
605+
toastSonner.error(
606+
error instanceof Error ? error.message : "An unknown error occurred"
607+
);
654608
} finally {
655609
setLoading(false);
656610
// Ensure analyzing is false unless explicitly set during submission
@@ -1140,7 +1094,7 @@ export function AIChatInterface({
11401094
loading || !value.trim() || selectedModels.length === 0
11411095
}
11421096
className={cn(
1143-
"p-2 active:scale-95 rounded-full text-sm -rotate-45 cursor-pointer hover:rotate-0 transition-all ease-in-out duration-300 border hover:bg-muted flex items-center justify-center", // Centered icon
1097+
"p-2 active:scale-95 disabled:opacity-50 rounded-full text-sm -rotate-45 cursor-pointer hover:rotate-0 transition-all ease-in-out duration-300 border hover:bg-muted flex items-center justify-center", // Centered icon
11441098
value.trim() && selectedModels.length > 0
11451099
? "bg-foreground text-background dark:bg-white dark:text-black border-foreground dark:border-zinc-700 hover:border-foreground/80 dark:hover:border-zinc-600"
11461100
: "text-muted-foreground dark:text-zinc-400 border-border dark:border-zinc-700"

0 commit comments

Comments
 (0)