mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 09:31:13 -05:00
feat: Add automatic email signature
This commit is contained in:
@@ -54,6 +54,7 @@ const AdminUsers = lazy(() => import("./pages/AdminUsers"));
|
||||
const AdminBlog = lazy(() => import("./pages/AdminBlog"));
|
||||
const AdminSettings = lazy(() => import("./pages/AdminSettings"));
|
||||
const AdminContact = lazy(() => import("./pages/admin/AdminContact"));
|
||||
const AdminEmailSettings = lazy(() => import("./pages/admin/AdminEmailSettings"));
|
||||
|
||||
// User routes (lazy-loaded)
|
||||
const Profile = lazy(() => import("./pages/Profile"));
|
||||
@@ -136,6 +137,7 @@ function AppContent() {
|
||||
<Route path="/admin/blog" element={<AdminBlog />} />
|
||||
<Route path="/admin/settings" element={<AdminSettings />} />
|
||||
<Route path="/admin/contact" element={<AdminContact />} />
|
||||
<Route path="/admin/email-settings" element={<AdminEmailSettings />} />
|
||||
|
||||
{/* Utility routes - lazy loaded */}
|
||||
<Route path="/force-logout" element={<ForceLogout />} />
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { LayoutDashboard, FileText, Flag, Users, Settings, ArrowLeft, ScrollText, BookOpen, Inbox } from 'lucide-react';
|
||||
import { LayoutDashboard, FileText, Flag, Users, Settings, ArrowLeft, ScrollText, BookOpen, Inbox, Mail } from 'lucide-react';
|
||||
import { NavLink } from 'react-router-dom';
|
||||
import { useUserRole } from '@/hooks/useUserRole';
|
||||
import { useSidebar } from '@/hooks/useSidebar';
|
||||
@@ -62,6 +62,10 @@ export function AdminSidebar() {
|
||||
title: 'Settings',
|
||||
url: '/admin/settings',
|
||||
icon: Settings,
|
||||
}, {
|
||||
title: 'Email Settings',
|
||||
url: '/admin/email-settings',
|
||||
icon: Mail,
|
||||
}] : []),
|
||||
];
|
||||
|
||||
|
||||
201
src/pages/admin/AdminEmailSettings.tsx
Normal file
201
src/pages/admin/AdminEmailSettings.tsx
Normal file
@@ -0,0 +1,201 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Save, Loader2, Mail } from 'lucide-react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card';
|
||||
import { AdminLayout } from '@/components/layout/AdminLayout';
|
||||
import { useUserRole } from '@/hooks/useUserRole';
|
||||
import { handleError, handleSuccess } from '@/lib/errorHandler';
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
|
||||
export default function AdminEmailSettings() {
|
||||
const queryClient = useQueryClient();
|
||||
const { isSuperuser, loading: rolesLoading } = useUserRole();
|
||||
const [signature, setSignature] = useState('');
|
||||
|
||||
// Fetch email signature
|
||||
const { data: signatureSetting, isLoading } = useQuery({
|
||||
queryKey: ['admin-email-signature'],
|
||||
queryFn: async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('admin_settings')
|
||||
.select('setting_value')
|
||||
.eq('setting_key', 'email.signature')
|
||||
.single();
|
||||
|
||||
if (error && error.code !== 'PGRST116') { // PGRST116 = no rows returned
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Type guard for the setting value
|
||||
const settingValue = data?.setting_value as { signature?: string } | null;
|
||||
return settingValue?.signature || '';
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (signatureSetting !== undefined) {
|
||||
setSignature(signatureSetting);
|
||||
}
|
||||
}, [signatureSetting]);
|
||||
|
||||
// Update email signature mutation
|
||||
const updateSignatureMutation = useMutation({
|
||||
mutationFn: async (newSignature: string) => {
|
||||
const { data: existing } = await supabase
|
||||
.from('admin_settings')
|
||||
.select('id')
|
||||
.eq('setting_key', 'email.signature')
|
||||
.single();
|
||||
|
||||
if (existing) {
|
||||
// Update existing
|
||||
const { error } = await supabase
|
||||
.from('admin_settings')
|
||||
.update({
|
||||
setting_value: { signature: newSignature },
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('setting_key', 'email.signature');
|
||||
|
||||
if (error) throw error;
|
||||
} else {
|
||||
// Insert new
|
||||
const { error } = await supabase
|
||||
.from('admin_settings')
|
||||
.insert({
|
||||
setting_key: 'email.signature',
|
||||
setting_value: { signature: newSignature },
|
||||
category: 'email',
|
||||
description: 'Email signature automatically appended to all contact submission replies',
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin-email-signature'] });
|
||||
handleSuccess('Saved', 'Email signature has been updated successfully');
|
||||
},
|
||||
onError: (error) => {
|
||||
handleError(error, { action: 'update_email_signature' });
|
||||
},
|
||||
});
|
||||
|
||||
const handleSave = () => {
|
||||
updateSignatureMutation.mutate(signature);
|
||||
};
|
||||
|
||||
// Show loading state while roles are being fetched
|
||||
if (rolesLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Superuser-only access check
|
||||
if (!isSuperuser()) {
|
||||
return (
|
||||
<AdminLayout>
|
||||
<div className="flex items-center justify-center min-h-[60vh]">
|
||||
<Card className="max-w-md">
|
||||
<CardContent className="pt-6 text-center">
|
||||
<Mail className="h-12 w-12 text-destructive mx-auto mb-4" />
|
||||
<h2 className="text-xl font-semibold mb-2">Access Denied</h2>
|
||||
<p className="text-muted-foreground">
|
||||
Email settings can only be managed by superusers.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</AdminLayout>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<AdminLayout>
|
||||
<div className="mb-8">
|
||||
<h1 className="text-4xl font-bold mb-2">Email Settings</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Configure automatic email signature for contact submission replies
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Mail className="h-5 w-5" />
|
||||
Email Signature
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
This signature will be automatically appended to all email replies sent to users from the Contact Submissions page.
|
||||
The signature will be added after a separator line.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<Alert>
|
||||
<AlertDescription>
|
||||
<strong>Preview:</strong> The signature will appear as:
|
||||
<div className="mt-2 p-3 bg-muted rounded-md text-sm font-mono">
|
||||
[Your reply message]
|
||||
<br />
|
||||
<br />
|
||||
---
|
||||
<br />
|
||||
{signature || '[No signature set]'}
|
||||
</div>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="signature">Signature Text</Label>
|
||||
<Textarea
|
||||
id="signature"
|
||||
value={signature}
|
||||
onChange={(e) => setSignature(e.target.value)}
|
||||
placeholder="Best regards, The ThrillWiki Team Need help? Reply to this email or visit https://thrillwiki.com/contact"
|
||||
rows={8}
|
||||
disabled={isLoading}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Use plain text. Line breaks will be preserved. You can include team name, contact info, or helpful links.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 pt-4">
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={updateSignatureMutation.isPending || isLoading}
|
||||
>
|
||||
{updateSignatureMutation.isPending && (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
<Save className="mr-2 h-4 w-4" />
|
||||
Save Signature
|
||||
</Button>
|
||||
{signature !== signatureSetting && (
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setSignature(signatureSetting || '')}
|
||||
disabled={updateSignatureMutation.isPending}
|
||||
>
|
||||
Reset Changes
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</AdminLayout>
|
||||
);
|
||||
}
|
||||
@@ -78,6 +78,20 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
return createErrorResponse({ message: 'Submission not found' }, 404, corsHeaders);
|
||||
}
|
||||
|
||||
// Fetch email signature from admin settings
|
||||
const { data: signatureSetting } = await supabase
|
||||
.from('admin_settings')
|
||||
.select('setting_value')
|
||||
.eq('setting_key', 'email.signature')
|
||||
.single();
|
||||
|
||||
const emailSignature = signatureSetting?.setting_value?.signature || '';
|
||||
|
||||
// Append signature to reply body if it exists
|
||||
const finalReplyBody = emailSignature
|
||||
? `${replyBody}\n\n---\n${emailSignature}`
|
||||
: replyBody;
|
||||
|
||||
// Rate limiting: max 10 replies per hour
|
||||
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000).toISOString();
|
||||
const { count } = await supabase
|
||||
@@ -124,7 +138,7 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
from: 'ThrillWiki Admin <admin@thrillwiki.com>',
|
||||
to: `${submission.name} <${submission.email}>`,
|
||||
subject: finalSubject,
|
||||
text: replyBody,
|
||||
text: finalReplyBody,
|
||||
headers: {
|
||||
'Message-ID': messageId,
|
||||
'In-Reply-To': inReplyTo,
|
||||
@@ -156,7 +170,7 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
from_email: 'admin@thrillwiki.com',
|
||||
to_email: submission.email,
|
||||
subject: finalSubject,
|
||||
body_text: replyBody,
|
||||
body_text: finalReplyBody,
|
||||
direction: 'outbound',
|
||||
sent_by: user.id,
|
||||
metadata: {
|
||||
|
||||
Reference in New Issue
Block a user