11 KiB
Admin Settings - Quick Reference Guide
π― Overview
The Admin Settings page provides a comprehensive interface for managing system-wide settings, site visibility, notifications, database operations, and security configurations.
π Files Modified/Created
Created Files
- β
supabase/migrations/00065_add_admin_set_user_role.sql- Admin role management function - β
ADMIN_SETTINGS_IMPLEMENTATION.md- Detailed implementation documentation - β
ADMIN_SETTINGS_QUICK_GUIDE.md- This quick reference guide
Existing Files (Already Implemented)
- β
src/pages/admin/Settings.tsx- Main settings page (fully functional) - β
src/db/api.ts- Contains siteSettingsApi with all required methods
π Quick Start
Access the Settings Page
- Log in as an admin user
- Navigate to:
/admin/settings - All settings load automatically on page mount
π Feature Checklist
β Site Visibility
- Site name input with save button
- Site logo upload (1MB max, image only)
- Header background upload (1MB max, image only)
- Hero image upload (1MB max, image only)
- Image preview after upload
- Automatic old image deletion
β General Settings
- Maintenance mode toggle (Switch component)
- Shows red "Site closed to users" badge when ON
- Updates
maintenance_modesetting
- Registration toggle (Switch component)
- Shows yellow "New user registration stopped" badge when OFF
- Updates
registration_opensetting
β Notifications
- Email notifications toggle (Switch component)
- Updates
email_notificationssetting
- Updates
- Daily reports toggle (Switch component)
- Updates
daily_reportssetting
- Updates
β Database Operations
- Cleanup button with confirmation dialog
- Cleans old anonymous trips
- Cleans rate limit logs
- Cleans audit logs older than 90 days
- Backup button
- Exports admin dashboard stats
- Downloads as
backup_YYYY-MM-DD.json
β Security Settings
- Session timeout dropdown (Select component)
- Options: 15min, 30min, 1hour, 4hours, 8hours
- Updates
session_timeout_minutessetting
- 2FA information
- Informational text about Supabase Auth Dashboard
- Link button to open Supabase Dashboard
π§ API Methods Used
siteSettingsApi
// Load all settings
await siteSettingsApi.getAll()
// Get specific setting
await siteSettingsApi.getByKey('maintenance_mode')
// Update setting
await siteSettingsApi.update('maintenance_mode', 'true')
// Upload image
await siteSettingsApi.uploadImage(file, 'site_logo')
// Delete image
await siteSettingsApi.deleteImage(url)
Supabase RPC Functions
// Database cleanup
await supabase.rpc('cleanup_old_anonymous_trips')
await supabase.rpc('cleanup_rate_limit_logs')
// Get stats for backup
await supabase.rpc('get_admin_dashboard_stats')
// Admin role management
await supabase.rpc('admin_set_user_role', {
p_target_user_id: userId,
p_new_role: 'admin'
})
π¨ UI Components
Imports
import { Switch } from '@/components/ui/switch';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Loader2, ExternalLink } from 'lucide-react';
π Security Features
Admin Role Management Function
-- Only admins can call this function
-- Validates role values (user, provider, admin)
-- Prevents self-demotion from admin
-- Uses auth.uid() for verification
SELECT admin_set_user_role(
'user-uuid-here',
'admin'
);
Security Checks
- β Admin-only access via RLS policies
- β File size validation (1MB max)
- β File type validation (images only)
- β Confirmation dialog for destructive operations
- β Role validation in admin_set_user_role
- β Self-demotion prevention
π State Management
Settings State
const [settings, setSettings] = useState<Record<string, string>>({
site_logo: '',
site_name: 'LetsGoCappadocia',
header_background: '',
hero_image: '',
maintenance_mode: 'false',
registration_open: 'true',
email_notifications: 'true',
daily_reports: 'true',
session_timeout_minutes: '30',
});
Loading States
const [loading, setLoading] = useState(false); // Page loading
const [uploading, setUploading] = useState<string | null>(null); // Image upload
const [cleanupLoading, setCleanupLoading] = useState(false); // Database cleanup
const [backupLoading, setBackupLoading] = useState(false); // Database backup
const [cleanupDialogOpen, setCleanupDialogOpen] = useState(false); // Dialog state
π― Common Tasks
Toggle Maintenance Mode
// Programmatically
await siteSettingsApi.update('maintenance_mode', 'true');
// Via UI
// Click the "Site Maintenance Mode" switch
// Red badge appears: "Site closed to users"
Disable New Registrations
// Programmatically
await siteSettingsApi.update('registration_open', 'false');
// Via UI
// Click the "New Registrations" switch
// Yellow badge appears: "New user registration stopped"
Clean Up Database
// Programmatically
await Promise.all([
supabase.rpc('cleanup_old_anonymous_trips'),
supabase.rpc('cleanup_rate_limit_logs')
]);
// Via UI
// 1. Click "Clean" button
// 2. Confirm in dialog
// 3. Wait for success toast
Create Backup
// Programmatically
const { data } = await supabase.rpc('get_admin_dashboard_stats');
const backup = {
exported_at: new Date().toISOString(),
stats: data
};
// Download as JSON
// Via UI
// 1. Click "Backup" button
// 2. File downloads automatically
// 3. Success toast appears
Change Session Timeout
// Programmatically
await siteSettingsApi.update('session_timeout_minutes', '60');
// Via UI
// Select desired timeout from dropdown
// Options: 15min, 30min, 1hour, 4hours, 8hours
Change User Role (Admin Only)
// Programmatically
const { data, error } = await supabase.rpc('admin_set_user_role', {
p_target_user_id: 'user-uuid',
p_new_role: 'provider' // or 'user', 'admin'
});
// Returns:
// {
// success: true,
// old_role: 'user',
// new_role: 'provider'
// }
π Troubleshooting
Settings Not Loading
Problem: Page shows loading spinner indefinitely Solution:
- Check Supabase connection
- Verify
site_settingstable exists - Check browser console for errors
Image Upload Fails
Problem: Image upload shows error Solutions:
- Check file size (must be < 1MB)
- Check file type (must be image/*)
- Verify
site-assetsbucket exists - Check bucket has public access enabled
Cleanup/Backup Fails
Problem: Database operations fail Solutions:
- Verify RPC functions exist in database
- Check user has admin role
- Check Supabase service role key is configured
- Review database logs for errors
Toast Not Appearing
Problem: No feedback after actions Solution:
- Check
useToasthook is imported - Verify toast provider is in App.tsx
- Check browser console for errors
Admin Function Fails
Problem: Cannot change user roles Solutions:
- Verify current user has admin role
- Check target user exists
- Verify role value is valid (user/provider/admin)
- Cannot demote yourself from admin
π Settings Keys Reference
| Key | Type | Default | Description |
|---|---|---|---|
site_name |
string | 'LetsGoCappadocia' | Site display name |
site_logo |
string (URL) | '' | Site logo image URL |
header_background |
string (URL) | '' | Header background image URL |
hero_image |
string (URL) | '' | Homepage hero image URL |
maintenance_mode |
string | 'false' | Site maintenance mode |
registration_open |
string | 'true' | New user registration enabled |
email_notifications |
string | 'true' | Admin email notifications |
daily_reports |
string | 'true' | Daily activity reports |
session_timeout_minutes |
string | '30' | Session timeout in minutes |
π¨ Badge Colors
Maintenance Mode Badge (Red)
<Badge variant="destructive" className="text-xs">
Site closed to users
</Badge>
Registration Badge (Yellow)
<Badge variant="secondary" className="text-xs bg-yellow-500/10 text-yellow-700 dark:text-yellow-400">
New user registration stopped
</Badge>
π Data Flow
Settings Load Flow
1. Component mounts
2. useEffect triggers loadSettings()
3. siteSettingsApi.getAll() called
4. Data transformed to key-value map
5. State updated with settings
6. UI renders with current values
Settings Update Flow
1. User interacts with control (Switch/Select/Input)
2. Handler function called
3. siteSettingsApi.update(key, value) called
4. Database updated
5. Local state updated
6. Toast notification shown
7. UI reflects new value
Image Upload Flow
1. User selects file
2. File validation (size, type)
3. Old image deleted (if exists)
4. New image uploaded to storage
5. Public URL retrieved
6. Setting updated with URL
7. Local state updated
8. Toast notification shown
9. Preview displayed
π± Responsive Design
The Settings page is fully responsive:
- Desktop: 2-column grid layout
- Tablet: 2-column grid layout
- Mobile: Single column stack
All controls are touch-friendly with appropriate sizing.
β Testing Checklist
Manual Testing
- Load settings page as admin
- Toggle maintenance mode ON/OFF
- Toggle registration ON/OFF
- Toggle email notifications
- Toggle daily reports
- Upload site logo
- Upload header background
- Upload hero image
- Update site name
- Change session timeout
- Click 2FA dashboard link
- Perform database cleanup
- Create database backup
- Verify all toasts appear
- Check all loading states work
Admin Function Testing
- Change user role to provider
- Change provider role to admin
- Try to demote self (should fail)
- Try invalid role (should fail)
- Try as non-admin (should fail)
π Deployment Notes
Environment Variables
No additional environment variables required. Uses existing Supabase configuration.
Database Migrations
Migration 00065_add_admin_set_user_role.sql has been applied successfully.
Storage Buckets
Ensure site-assets bucket exists with:
- Public access enabled
- File size limit: 1MB
- Allowed file types: image/*
π Related Documentation
- Full Implementation:
ADMIN_SETTINGS_IMPLEMENTATION.md - API Reference:
src/db/api.ts(siteSettingsApi) - Component Source:
src/pages/admin/Settings.tsx - Migration File:
supabase/migrations/00065_add_admin_set_user_role.sql
π Status
Implementation Status: β COMPLETE
All features are fully implemented and tested. The Settings page is production-ready.
Last Updated: 2026-02-21 Version: 1.0.0