mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 09:31:13 -05:00
Implement admin settings system
This commit is contained in:
134
src/hooks/useAdminSettings.ts
Normal file
134
src/hooks/useAdminSettings.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { useAuth } from './useAuth';
|
||||
import { useToast } from './use-toast';
|
||||
|
||||
interface AdminSetting {
|
||||
id: string;
|
||||
setting_key: string;
|
||||
setting_value: any;
|
||||
category: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export function useAdminSettings() {
|
||||
const { user } = useAuth();
|
||||
const { toast } = useToast();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const {
|
||||
data: settings,
|
||||
isLoading,
|
||||
error
|
||||
} = useQuery({
|
||||
queryKey: ['admin-settings'],
|
||||
queryFn: async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('admin_settings')
|
||||
.select('*')
|
||||
.order('category', { ascending: true });
|
||||
|
||||
if (error) throw error;
|
||||
return data as AdminSetting[];
|
||||
},
|
||||
enabled: !!user
|
||||
});
|
||||
|
||||
const updateSettingMutation = useMutation({
|
||||
mutationFn: async ({ key, value }: { key: string; value: any }) => {
|
||||
const { error } = await supabase
|
||||
.from('admin_settings')
|
||||
.update({
|
||||
setting_value: value,
|
||||
updated_by: user?.id,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('setting_key', key);
|
||||
|
||||
if (error) throw error;
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin-settings'] });
|
||||
toast({
|
||||
title: "Setting Updated",
|
||||
description: "The setting has been saved successfully.",
|
||||
});
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: error.message || "Failed to update setting",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const getSetting = (key: string) => {
|
||||
return settings?.find(s => s.setting_key === key);
|
||||
};
|
||||
|
||||
const getSettingValue = (key: string, defaultValue: any = null) => {
|
||||
const setting = getSetting(key);
|
||||
return setting ? setting.setting_value : defaultValue;
|
||||
};
|
||||
|
||||
const getSettingsByCategory = (category: string) => {
|
||||
return settings?.filter(s => s.category === category) || [];
|
||||
};
|
||||
|
||||
const updateSetting = async (key: string, value: any) => {
|
||||
return updateSettingMutation.mutateAsync({ key, value });
|
||||
};
|
||||
|
||||
// Helper functions for common settings
|
||||
const getAutoFlagThreshold = () => {
|
||||
return parseInt(getSettingValue('moderation.auto_flag_threshold', '3'));
|
||||
};
|
||||
|
||||
const getRequireApproval = () => {
|
||||
const value = getSettingValue('moderation.require_approval', 'true');
|
||||
return value === true || value === 'true';
|
||||
};
|
||||
|
||||
const getBanDurations = () => {
|
||||
const value = getSettingValue('moderation.ban_durations', ['1d', '7d', '30d', 'permanent']);
|
||||
return Array.isArray(value) ? value : JSON.parse(value || '[]');
|
||||
};
|
||||
|
||||
const getEmailAlertsEnabled = () => {
|
||||
const value = getSettingValue('notifications.email_alerts', 'true');
|
||||
return value === true || value === 'true';
|
||||
};
|
||||
|
||||
const getReportThreshold = () => {
|
||||
return parseInt(getSettingValue('notifications.report_threshold', '5'));
|
||||
};
|
||||
|
||||
const getAuditRetentionDays = () => {
|
||||
return parseInt(getSettingValue('system.audit_retention_days', '365'));
|
||||
};
|
||||
|
||||
const getAutoCleanupEnabled = () => {
|
||||
const value = getSettingValue('system.auto_cleanup', 'false');
|
||||
return value === true || value === 'true';
|
||||
};
|
||||
|
||||
return {
|
||||
settings,
|
||||
isLoading,
|
||||
error,
|
||||
updateSetting,
|
||||
isUpdating: updateSettingMutation.isPending,
|
||||
getSetting,
|
||||
getSettingValue,
|
||||
getSettingsByCategory,
|
||||
// Helper functions
|
||||
getAutoFlagThreshold,
|
||||
getRequireApproval,
|
||||
getBanDurations,
|
||||
getEmailAlertsEnabled,
|
||||
getReportThreshold,
|
||||
getAuditRetentionDays,
|
||||
getAutoCleanupEnabled,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user