mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 16:31:12 -05:00
Refactor: Complete type safety migration
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useRef, useCallback } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { createTableQuery } from '@/lib/supabaseHelpers';
|
||||
import { logger } from '@/lib/logger';
|
||||
import { MODERATION_CONSTANTS } from '@/lib/moderation/constants';
|
||||
|
||||
@@ -104,32 +105,40 @@ export function useEntityCache() {
|
||||
return ids.map(id => getCached(type, id)).filter(Boolean);
|
||||
}
|
||||
|
||||
// Determine table name and select fields based on entity type
|
||||
let tableName: string;
|
||||
let selectFields: string;
|
||||
|
||||
switch (type) {
|
||||
case 'rides':
|
||||
tableName = 'rides';
|
||||
selectFields = 'id, name, park_id';
|
||||
break;
|
||||
case 'parks':
|
||||
tableName = 'parks';
|
||||
selectFields = 'id, name';
|
||||
break;
|
||||
case 'companies':
|
||||
tableName = 'companies';
|
||||
selectFields = 'id, name';
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown entity type: ${type}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from(tableName as any)
|
||||
.select(selectFields)
|
||||
.in('id', uncachedIds);
|
||||
let data: any[] | null = null;
|
||||
let error: any = null;
|
||||
|
||||
// Use type-safe table queries
|
||||
switch (type) {
|
||||
case 'rides':
|
||||
const ridesResult = await createTableQuery('rides')
|
||||
.select('id, name, slug, park_id')
|
||||
.in('id', uncachedIds);
|
||||
data = ridesResult.data;
|
||||
error = ridesResult.error;
|
||||
break;
|
||||
|
||||
case 'parks':
|
||||
const parksResult = await createTableQuery('parks')
|
||||
.select('id, name, slug')
|
||||
.in('id', uncachedIds);
|
||||
data = parksResult.data;
|
||||
error = parksResult.error;
|
||||
break;
|
||||
|
||||
case 'companies':
|
||||
const companiesResult = await createTableQuery('companies')
|
||||
.select('id, name, slug, company_type')
|
||||
.in('id', uncachedIds);
|
||||
data = companiesResult.data;
|
||||
error = companiesResult.error;
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.error(`Unknown entity type: ${type}`);
|
||||
return [];
|
||||
}
|
||||
|
||||
if (error) {
|
||||
logger.error(`Error fetching ${type}:`, error);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { useAuth } from './useAuth';
|
||||
import { useToast } from './use-toast';
|
||||
import { getErrorMessage } from '@/lib/errorHandler';
|
||||
import { getSubmissionTypeLabel } from '@/lib/moderation/entities';
|
||||
|
||||
interface QueuedSubmission {
|
||||
@@ -163,11 +164,11 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error('Error extending lock:', error);
|
||||
toast({
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to extend lock',
|
||||
description: getErrorMessage(error),
|
||||
variant: 'destructive',
|
||||
});
|
||||
return false;
|
||||
@@ -226,13 +227,13 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error('Error releasing lock:', error);
|
||||
|
||||
// Always show error toasts even in silent mode
|
||||
toast({
|
||||
title: 'Failed to Release Lock',
|
||||
description: error.message || 'An error occurred',
|
||||
description: getErrorMessage(error),
|
||||
variant: 'destructive',
|
||||
});
|
||||
return false;
|
||||
@@ -272,11 +273,11 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
||||
|
||||
fetchStats();
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error('Error escalating submission:', error);
|
||||
toast({
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to escalate submission',
|
||||
description: getErrorMessage(error),
|
||||
variant: 'destructive',
|
||||
});
|
||||
return false;
|
||||
@@ -342,11 +343,11 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error('Error claiming submission:', error);
|
||||
toast({
|
||||
title: 'Failed to Claim Submission',
|
||||
description: error.message || 'Could not claim this submission. Try again.',
|
||||
description: getErrorMessage(error),
|
||||
variant: 'destructive',
|
||||
});
|
||||
return false;
|
||||
@@ -387,11 +388,11 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
||||
|
||||
fetchStats();
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error('Error reassigning submission:', error);
|
||||
toast({
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to reassign submission',
|
||||
description: getErrorMessage(error),
|
||||
variant: 'destructive',
|
||||
});
|
||||
return false;
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { useEffect, useState, useRef, useCallback } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
|
||||
// Type for submission realtime payload
|
||||
interface SubmissionPayload {
|
||||
status?: string;
|
||||
assigned_to?: string | null;
|
||||
locked_until?: string | null;
|
||||
escalated?: boolean;
|
||||
}
|
||||
|
||||
interface ModerationStats {
|
||||
pendingSubmissions: number;
|
||||
openReports: number;
|
||||
@@ -118,12 +126,14 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
|
||||
schema: 'public',
|
||||
table: 'content_submissions'
|
||||
}, (payload) => {
|
||||
const oldStatus = (payload.old as any)?.status;
|
||||
const newStatus = (payload.new as any)?.status;
|
||||
const oldAssignedTo = (payload.old as any)?.assigned_to;
|
||||
const newAssignedTo = (payload.new as any)?.assigned_to;
|
||||
const oldLockedUntil = (payload.old as any)?.locked_until;
|
||||
const newLockedUntil = (payload.new as any)?.locked_until;
|
||||
const oldData = payload.old as SubmissionPayload;
|
||||
const newData = payload.new as SubmissionPayload;
|
||||
const oldStatus = oldData?.status;
|
||||
const newStatus = newData?.status;
|
||||
const oldAssignedTo = oldData?.assigned_to;
|
||||
const newAssignedTo = newData?.assigned_to;
|
||||
const oldLockedUntil = oldData?.locked_until;
|
||||
const newLockedUntil = newData?.locked_until;
|
||||
|
||||
// Only refresh if change affects pending count or assignments
|
||||
if (
|
||||
|
||||
@@ -3,6 +3,17 @@ import { useAuth } from '@/hooks/useAuth';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { logger } from '@/lib/logger';
|
||||
import { UnitPreferences, getMeasurementSystemFromCountry } from '@/lib/units';
|
||||
import type { Json } from '@/integrations/supabase/types';
|
||||
|
||||
// Type guard for unit preferences
|
||||
function isValidUnitPreferences(obj: unknown): obj is UnitPreferences {
|
||||
return (
|
||||
typeof obj === 'object' &&
|
||||
obj !== null &&
|
||||
'measurement_system' in obj &&
|
||||
['metric', 'imperial'].includes((obj as any).measurement_system)
|
||||
);
|
||||
}
|
||||
|
||||
const DEFAULT_PREFERENCES: UnitPreferences = {
|
||||
measurement_system: 'metric',
|
||||
@@ -38,8 +49,9 @@ export function useUnitPreferences() {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (data?.unit_preferences && typeof data.unit_preferences === 'object') {
|
||||
setPreferences({ ...DEFAULT_PREFERENCES, ...(data.unit_preferences as unknown as UnitPreferences) });
|
||||
if (data?.unit_preferences && isValidUnitPreferences(data.unit_preferences)) {
|
||||
const validPrefs = data.unit_preferences as UnitPreferences;
|
||||
setPreferences({ ...DEFAULT_PREFERENCES, ...validPrefs });
|
||||
} else {
|
||||
await autoDetectPreferences();
|
||||
}
|
||||
@@ -85,7 +97,7 @@ export function useUnitPreferences() {
|
||||
.from('user_preferences')
|
||||
.upsert({
|
||||
user_id: user.id,
|
||||
unit_preferences: newPreferences as any,
|
||||
unit_preferences: newPreferences as unknown as Json,
|
||||
updated_at: new Date().toISOString()
|
||||
});
|
||||
|
||||
@@ -124,7 +136,7 @@ export function useUnitPreferences() {
|
||||
await supabase
|
||||
.from('user_preferences')
|
||||
.update({
|
||||
unit_preferences: updated as any,
|
||||
unit_preferences: updated as unknown as Json,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('user_id', user.id);
|
||||
|
||||
Reference in New Issue
Block a user