useFlagExercise.ts
1 import { useState, useCallback } from 'react' 2 import { useAccount } from 'wagmi' 3 import { moderationService } from '@/services/database/gundb/moderation' 4 import type { FlagReason, ExerciseFlag } from '@/services/database/gundb/moderation' 5 6 interface UseFlagExerciseReturn { 7 flagExercise: (exerciseKey: string, reason: FlagReason, details?: string) => Promise<void> 8 isSubmitting: boolean 9 error: string | null 10 success: boolean 11 } 12 13 export function useFlagExercise(): UseFlagExerciseReturn { 14 const { address } = useAccount() 15 const [isSubmitting, setIsSubmitting] = useState(false) 16 const [error, setError] = useState<string | null>(null) 17 const [success, setSuccess] = useState(false) 18 19 const flagExercise = useCallback(async ( 20 exerciseKey: string, 21 reason: FlagReason, 22 details?: string 23 ) => { 24 if (!address) { 25 setError('Please connect your wallet to flag exercises') 26 return 27 } 28 29 setIsSubmitting(true) 30 setError(null) 31 setSuccess(false) 32 33 try { 34 // Create flag data 35 const flag: ExerciseFlag = { 36 exerciseKey, 37 walletAddress: address.toLowerCase(), 38 reason, 39 timestamp: Date.now(), 40 ...(details && { details }) 41 } 42 43 // Submit flag to GunDB 44 await moderationService.submitFlag(flag) 45 46 setSuccess(true) 47 console.log('✅ Flag submitted successfully') 48 49 // Reset success after 3 seconds 50 setTimeout(() => setSuccess(false), 3000) 51 } catch (err) { 52 const errorMessage = err instanceof Error ? err.message : 'Failed to submit flag' 53 setError(errorMessage) 54 console.error('❌ Error submitting flag:', errorMessage) 55 } finally { 56 setIsSubmitting(false) 57 } 58 }, [address]) 59 60 return { 61 flagExercise, 62 isSubmitting, 63 error, 64 success 65 } 66 }