'use client' import { useCallback, useState } from 'react' import { addToast } from '@/lib/toast' import { getServiceErrorCopy } from '@/helpers/serviceErrorCopy' import { handleServiceError, resolveServiceErrorDisplayMessage } from '@/services/errorHandler' interface UseAdminActionOptions { errorMessage?: string onSuccess?: () => unknown } export default function useAdminAction({ errorMessage = 'عملیات با خطا مواجه شد', onSuccess }: UseAdminActionOptions = {}) { const [pendingId, setPendingId] = useState(null) const runAction = useCallback( async (id: string, action: () => Promise, successMessage: string) => { try { setPendingId(id) await action() addToast({ title: successMessage, color: 'success' }) await onSuccess?.() } catch (error: unknown) { const normalizedError = handleServiceError(error, { errorMode: 'silent' }) const copy = getServiceErrorCopy() const serviceMessage = resolveServiceErrorDisplayMessage(normalizedError, { apiError: errorMessage || copy.apiError, networkError: copy.networkError, }) addToast({ title: serviceMessage || errorMessage, color: 'danger' }) } finally { setPendingId(null) } }, [errorMessage, onSuccess] ) return { pendingId, runAction } }