Add moderation queue tables

This commit is contained in:
gpt-engineer-app[bot]
2025-09-28 18:06:00 +00:00
parent 64c29348ce
commit ff5d7ebea6
11 changed files with 1542 additions and 3 deletions

54
src/hooks/useUserRole.ts Normal file
View File

@@ -0,0 +1,54 @@
import { useState, useEffect } from 'react';
import { supabase } from '@/integrations/supabase/client';
import { useAuth } from '@/hooks/useAuth';
export type UserRole = 'admin' | 'moderator' | 'user';
export function useUserRole() {
const { user } = useAuth();
const [roles, setRoles] = useState<UserRole[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
if (!user) {
setRoles([]);
setLoading(false);
return;
}
const fetchRoles = async () => {
try {
const { data, error } = await supabase
.from('user_roles')
.select('role')
.eq('user_id', user.id);
if (error) {
console.error('Error fetching user roles:', error);
setRoles([]);
} else {
setRoles(data?.map(r => r.role as UserRole) || []);
}
} catch (error) {
console.error('Error fetching user roles:', error);
setRoles([]);
} finally {
setLoading(false);
}
};
fetchRoles();
}, [user]);
const hasRole = (role: UserRole) => roles.includes(role);
const isModerator = () => hasRole('admin') || hasRole('moderator');
const isAdmin = () => hasRole('admin');
return {
roles,
loading,
hasRole,
isModerator,
isAdmin
};
}