mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Refactor: Use RPC for user data
This commit is contained in:
@@ -69,10 +69,10 @@ export function ReassignDialog({
|
|||||||
|
|
||||||
const userIds = roles.map((r) => r.user_id);
|
const userIds = roles.map((r) => r.user_id);
|
||||||
|
|
||||||
const { data: profiles, error: profilesError } = await supabase
|
const { data: allProfiles, error: profilesError } = await supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails');
|
||||||
.select('user_id, username, display_name')
|
|
||||||
.in('user_id', userIds);
|
const profiles = allProfiles?.filter(p => userIds.includes(p.user_id));
|
||||||
|
|
||||||
if (profilesError) throw profilesError;
|
if (profilesError) throw profilesError;
|
||||||
|
|
||||||
|
|||||||
@@ -190,11 +190,11 @@ export const ReportsQueue = forwardRef<ReportsQueueRef>((props, ref) => {
|
|||||||
// Get unique reporter IDs
|
// Get unique reporter IDs
|
||||||
const reporterIds = [...new Set((data || []).map(r => r.reporter_id))];
|
const reporterIds = [...new Set((data || []).map(r => r.reporter_id))];
|
||||||
|
|
||||||
// Fetch reporter profiles
|
// Fetch reporter profiles with emails (for admins)
|
||||||
const { data: profiles } = await supabase
|
const { data: allProfiles } = await supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails');
|
||||||
.select('user_id, username, display_name')
|
|
||||||
.in('user_id', reporterIds);
|
const profiles = allProfiles?.filter(p => reporterIds.includes(p.user_id));
|
||||||
|
|
||||||
const profileMap = new Map(profiles?.map(p => [p.user_id, p]) || []);
|
const profileMap = new Map(profiles?.map(p => [p.user_id, p]) || []);
|
||||||
|
|
||||||
@@ -219,10 +219,8 @@ export const ReportsQueue = forwardRef<ReportsQueueRef>((props, ref) => {
|
|||||||
|
|
||||||
profileIds.length > 0
|
profileIds.length > 0
|
||||||
? supabase
|
? supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails')
|
||||||
.select('user_id, username, display_name')
|
.then(({ data }) => data?.filter(p => profileIds.includes(p.user_id)) || [])
|
||||||
.in('user_id', profileIds)
|
|
||||||
.then(({ data }) => data || [])
|
|
||||||
: Promise.resolve([]),
|
: Promise.resolve([]),
|
||||||
|
|
||||||
submissionIds.length > 0
|
submissionIds.length > 0
|
||||||
|
|||||||
@@ -85,10 +85,12 @@ export function UserRoleManager() {
|
|||||||
// Get unique user IDs
|
// Get unique user IDs
|
||||||
const userIds = [...new Set((data || []).map(r => r.user_id))];
|
const userIds = [...new Set((data || []).map(r => r.user_id))];
|
||||||
|
|
||||||
// Fetch user profiles
|
// Fetch user profiles with emails (for admins)
|
||||||
const {
|
const {
|
||||||
data: profiles
|
data: allProfiles
|
||||||
} = await supabase.from('profiles').select('user_id, username, display_name').in('user_id', userIds);
|
} = await supabase.rpc('get_users_with_emails');
|
||||||
|
|
||||||
|
const profiles = allProfiles?.filter(p => userIds.includes(p.user_id));
|
||||||
const profileMap = new Map(profiles?.map(p => [p.user_id, p]) || []);
|
const profileMap = new Map(profiles?.map(p => [p.user_id, p]) || []);
|
||||||
|
|
||||||
// Combine data with profiles
|
// Combine data with profiles
|
||||||
@@ -113,9 +115,15 @@ export function UserRoleManager() {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
data,
|
data: allUsers,
|
||||||
error
|
error
|
||||||
} = await supabase.from('profiles').select('user_id, username, display_name').or(`username.ilike.%${search}%,display_name.ilike.%${search}%`).limit(10);
|
} = await supabase.rpc('get_users_with_emails');
|
||||||
|
|
||||||
|
// Filter by search term
|
||||||
|
const data = allUsers?.filter(user =>
|
||||||
|
user.username.toLowerCase().includes(search.toLowerCase()) ||
|
||||||
|
user.display_name?.toLowerCase().includes(search.toLowerCase())
|
||||||
|
).slice(0, 10);
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
|
|
||||||
// Filter out users who already have roles
|
// Filter out users who already have roles
|
||||||
|
|||||||
@@ -768,10 +768,10 @@ export async function fetchSystemActivities(
|
|||||||
const uniqueUserIds = [...new Set(filteredActivities.map(a => a.actor_id).filter(Boolean))] as string[];
|
const uniqueUserIds = [...new Set(filteredActivities.map(a => a.actor_id).filter(Boolean))] as string[];
|
||||||
|
|
||||||
if (uniqueUserIds.length > 0) {
|
if (uniqueUserIds.length > 0) {
|
||||||
const { data: profiles } = await supabase
|
const { data: allProfiles } = await supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails');
|
||||||
.select('user_id, username, display_name, avatar_url')
|
|
||||||
.in('user_id', uniqueUserIds);
|
const profiles = allProfiles?.filter(p => uniqueUserIds.includes(p.user_id));
|
||||||
|
|
||||||
if (profiles) {
|
if (profiles) {
|
||||||
const profileMap = new Map(profiles.map(p => [p.user_id, p]));
|
const profileMap = new Map(profiles.map(p => [p.user_id, p]));
|
||||||
@@ -797,10 +797,10 @@ export async function fetchSystemActivities(
|
|||||||
.filter(Boolean) as string[];
|
.filter(Boolean) as string[];
|
||||||
|
|
||||||
if (targetUserIds.length > 0) {
|
if (targetUserIds.length > 0) {
|
||||||
const { data: targetProfiles } = await supabase
|
const { data: allTargetProfiles } = await supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails');
|
||||||
.select('user_id, username')
|
|
||||||
.in('user_id', targetUserIds);
|
const targetProfiles = allTargetProfiles?.filter(p => targetUserIds.includes(p.user_id));
|
||||||
|
|
||||||
if (targetProfiles) {
|
if (targetProfiles) {
|
||||||
const targetProfileMap = new Map(targetProfiles.map(p => [p.user_id, p]));
|
const targetProfileMap = new Map(targetProfiles.map(p => [p.user_id, p]));
|
||||||
@@ -826,10 +826,10 @@ export async function fetchSystemActivities(
|
|||||||
.filter(Boolean) as string[];
|
.filter(Boolean) as string[];
|
||||||
|
|
||||||
if (accountUserIds.length > 0) {
|
if (accountUserIds.length > 0) {
|
||||||
const { data: accountProfiles } = await supabase
|
const { data: allAccountProfiles } = await supabase
|
||||||
.from('profiles')
|
.rpc('get_users_with_emails');
|
||||||
.select('user_id, username')
|
|
||||||
.in('user_id', accountUserIds);
|
const accountProfiles = allAccountProfiles?.filter(p => accountUserIds.includes(p.user_id));
|
||||||
|
|
||||||
if (accountProfiles) {
|
if (accountProfiles) {
|
||||||
const accountProfileMap = new Map(accountProfiles.map(p => [p.user_id, p]));
|
const accountProfileMap = new Map(accountProfiles.map(p => [p.user_id, p]));
|
||||||
|
|||||||
Reference in New Issue
Block a user