Random
Random
if (!user) {
return (
<Layout>
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
<div className="flex flex-col items-center justify-center">
<AlertCircle className="h-12 w-12 text-crisis-600 mb-4" />
<h1 className="text-2xl font-semibold mb-2">Authentication
Required</h1>
<p className="text-gray-600 mb-6">Please log in to create drafts.</p>
<Button onClick={() => navigate("/login")}>Log In</Button>
</div>
</div>
</Layout>
);
}
if (user.credits < 1) {
toast({
title: "Insufficient Draft Credits",
description: "You need at least 1 draft credit to generate a draft. Please
purchase more draft credits.",
variant: "destructive",
});
navigate("/pricing");
return;
}
// Generate draft
setIsGenerating(true);
try {
console.log("Submitting form values:", values);
// Call the Supabase Edge Function to generate the draft with OpenAI
const { data, error } = await supabase.functions.invoke('generate-draft', {
body: values,
});
toast({
title: "Draft Generated Successfully",
description: "Your crisis communication draft has been created.",
});
navigate("/drafts");
} catch (error) {
console.error("Error generating draft:", error);
toast({
title: "Generation Failed",
description: error instanceof Error ? error.message : "An unexpected error
occurred.",
variant: "destructive",
});
} finally {
setIsGenerating(false);
}
};
const previousStep = () => {
if (currentStep > 1) {
setCurrentStep(currentStep - 1);
}
};
if (currentStep === 1) {
isValid = await form.trigger(['industry', 'organization', 'situation']);
} else if (currentStep === 2) {
isValid = await form.trigger(['audience', 'urgency', 'additionalInfo']);
} else if (currentStep === 3) {
isValid = await form.trigger(['format']);
}
if (isValid) {
if (currentStep < totalSteps) {
setCurrentStep(currentStep + 1);
} else {
await form.handleSubmit(onSubmit)();
}
}
};
return (
<Layout>
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<Button
variant="ghost"
onClick={() => navigate(-1)}
className="mb-8 flex items-center"
>
<ArrowLeft className="mr-2 h-4 w-4" />
Back
</Button>
<div className="mb-8">
<h1 className="text-3xl font-bold">Create Crisis Communication</h1>
<p className="text-gray-600 mt-2">
Fill out the form below to generate a draft crisis communication
</p>
</div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
{currentStep === 1 && (
<Card>
<CardContent className="pt-6">
<div className="space-y-6">
<h2 className="text-xl font-semibold">Crisis Context</h2>
<FormField
control={form.control}
name="industry"
render={({ field }) => (
<FormItem>
<FormLabel>Industry</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select your industry" />
</SelectTrigger>
</FormControl>
<SelectContent>
{INDUSTRIES.map((industry) => (
<SelectItem key={industry} value={industry}>
{industry}
</SelectItem>
))}
</SelectContent>
</Select>
<FormDescription>
Select the industry that best matches your
organization.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="organization"
render={({ field }) => (
<FormItem>
<FormLabel>Organization Name</FormLabel>
<FormControl>
<Input placeholder="e.g., Acme Corporation"
{...field} />
</FormControl>
<FormDescription>
The name of your company or organization.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="situation"
render={({ field }) => (
<FormItem>
<FormLabel>Crisis Situation</FormLabel>
<FormControl>
<Textarea
placeholder="Describe the crisis situation in detail"
className="min-h-[120px]"
{...field}
/>
</FormControl>
<FormDescription>
Provide a detailed description of the crisis situation
your organization is facing.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
</CardContent>
</Card>
)}
<FormField
control={form.control}
name="urgency"
render={({ field }) => (
<FormItem className="space-y-3">
<FormLabel>Urgency Level</FormLabel>
<FormControl>
<RadioGroup
onValueChange={field.onChange}
defaultValue={field.value}
className="flex flex-col space-y-1"
>
<FormItem className="flex items-center space-x-3
space-y-0">
<FormControl>
<RadioGroupItem value="low" />
</FormControl>
<FormLabel className="font-normal">
Low - Informational update
</FormLabel>
</FormItem>
<FormItem className="flex items-center space-x-3
space-y-0">
<FormControl>
<RadioGroupItem value="medium" />
</FormControl>
<FormLabel className="font-normal">
Medium - Important but not critical
</FormLabel>
</FormItem>
<FormItem className="flex items-center space-x-3
space-y-0">
<FormControl>
<RadioGroupItem value="high" />
</FormControl>
<FormLabel className="font-normal">
High - Urgent situation requiring immediate
action
</FormLabel>
</FormItem>
</RadioGroup>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="additionalInfo"
render={({ field }) => (
<FormItem>
<FormLabel>Additional Information (Optional)</FormLabel>
<FormControl>
<Textarea
placeholder="Any other relevant details about the
situation"
className="min-h-[100px]"
{...field}
/>
</FormControl>
<FormDescription>
Include any additional context that might help in
crafting your message.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
</CardContent>
</Card>
)}
<FormField
control={form.control}
name="format"
render={({ field }) => (
<FormItem>
<FormLabel>Communication Format</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a format" />
</SelectTrigger>
</FormControl>
<SelectContent>
{availableFormats.map((format) => (
<SelectItem key={format.id} value={format.id}>
{format.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormDescription>
Select the type of communication you need to create.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>