Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
42 lines
1.4 KiB
TypeScript
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 }
|
|
}
|