admin/hooks/useAdminAction.ts
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

42 lines
1.4 KiB
TypeScript

'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<string | null>(null)
const runAction = useCallback(
async (id: string, action: () => Promise<unknown>, 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 }
}